今天小編給大家分享一下shell腳本自動輸入用戶名和密碼如何實現的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
shell腳本中,scp和ssh連接時,自動輸入用戶名和密碼。
例:
#!/bin/bash
remoteIp=IP
remoteUser=用戶名
remotePw=密碼
function download(){
remoteFile=$1
localDir=$2
expect << EOF
set timeout 1200;
spawn scp -r -p $remoteUser@$remoteIp:"$remoteFile" "$localDir"
expect{
"*yes/no*" {send "yes\n";exp_continue}
"*Permission denied*" {exit 1}
"*password*" {send "$remotePw\n";exp_continue}
"*Killed by signal 1" {exit 1}
}
EOF
}
fucntion remoteCmd(){
cmd=$1
expect << EOF
set timeout 1200;
spawn ssh $remoteUser@$remoteIp
expect{
"*yes/no*" {send "yes\n";exp_continue}
"*Permission denied*" {exit 1}
"*password*" {send "$remotePw\n";exp_continue}
"*\$ " {send "\n"}
}
expect "*\$ " {send "$cmd\n"}
expect "*\$ " {send "exit\n"}
EOF
}
remoteCmd "ls -l"以上就是“shell腳本自動輸入用戶名和密碼如何實現”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。