shell腳本自動輸入用戶名和密碼的實現(xiàn)
更新時間:2023年02月20日 10:04:03 作者:hnht1989
本文主要介紹了shell腳本自動輸入用戶名和密碼的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
場景:
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 腳本自動輸入密碼的三種方式小結的文章就介紹到這了,更多相關Shell 自動輸入密碼內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
編寫shell腳本將VPS上的數(shù)據備份到Dropbox網盤的方法
這篇文章主要介紹了編寫shell腳本將VPS上的數(shù)據備份到Dropbox網盤的方法,注意Dropbox在國內訪問的網絡相關問題,需要的朋友可以參考下2015-07-07
iredmail下安裝腳本分析(一)---get_all.sh 文件所在目錄為PKGS
這篇文章主要介紹了iredmail下安裝腳本分析(一)---get_all.sh 文件所在目錄為PKGS的相關資料,需要的朋友可以參考下2015-10-10
Linux?Shell如何用ssh命令統(tǒng)計分布式集群信息詳解
作為命令的ssh,作用就是在一臺linux機器上去登錄到另一臺linux機器,下面這篇文章主要給大家介紹了關于Linux?Shell如何用ssh命令統(tǒng)計分布式集群信息的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-07-07

