關於bash腳本的問題

關於bash腳本的問題

我使用的是 Ubuntu 16.04。我編寫了一個基本的 bash 文件,它會自動登入我的 SSH 伺服器。如果 SSH 無法到達伺服器,是否有辦法新增一則訊息,顯示「連線失敗」?可能是if命令?我已經把文件放上GitHub。

新增了腳本

#!/bin/sh
#I wrote this little bash script for people.
#It's just for people that don't know how to make an auto login script for SSH.
clear
echo "Welcome, $USER !"
echo "Connecting to SSH server..."
sshpass -p "passwordhere" ssh -o StrictHostKeyChecking=no user@host

答案1

而不是sshpass -p "passwordhere" ssh -o StrictHostKeyChecking=no user@host,讓它

if $(sshpass -p "passwordhere" ssh -o StrictHostKeyChecking=no user@host); then echo "Success!"; else echo "Bother.  Didn't work."; fi

相關內容