日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

shell腳本自動輸入用戶名和密碼的實現_linux shell

作者:hnht1989 ? 更新時間: 2023-05-22 編程語言

場景:

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"

原文鏈接:https://blog.csdn.net/hnht1989/article/details/127591909

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新