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 自動輸入密碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
shell中使用expect命令進行遠程執(zhí)行命令腳本
今天小編就為大家分享一篇關于shell中使用expect命令進行遠程執(zhí)行命令腳本,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03Shell之function函數(shù)的定義及調(diào)用示例
本文主要介紹了Shell之function函數(shù)的定義及調(diào)用示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08淺談ctrl+c,ctrl+d,ctrl+z在linux中的意義
下面小編就為大家?guī)硪黄獪\談ctrl+c,ctrl+d,ctrl+z在linux中的意義。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01