Linux ssh下執行ssh命令遠程登錄其他機器,總是需要輸入密碼,如果人工去登錄,輸入密碼那還可以,但是讓程序自動化登錄遠程ssh服務器,並執行命令着就比較麻煩了
安裝expect
centos安裝: yum install expect -y
#!/usr/bin/expect
set timeout 120
spawn scp -r /tmp/file.txt [email protected]:/mnt/ccc/
expect {
"yes/no" {exec sleep 1
send "yes\r"; exp_continue}
password: {exec sleep 5
send "111111\r" ; exp_continue}
eof exit
}
注意: password: 後要有空格,如果沒有空格會沒有任何提示報錯。
默認情況下 10 秒執行命令就會中斷
set timeout 120 添加這一段時間會長很多