簡體   English   中英

期望腳本和ssh

[英]Expect scripts and ssh

我已經實現了Expect腳本,以在某些服務器上自動進行ssh連接。

ssh第一次嘗試打開與服務器的連接時出現問題,我得到一個是/否的問題。

我必須首先手動執行連接,然后它不會再出現並且腳本運行良好。

1st:我需要您的幫助才能在Expect腳本中找到跳過該問題的步驟,這可能嗎?

  1. 這是一個例子:

     #!/usr/bin/env expect spawn ssh root@127.0.0.1 while {1} { expect "(yes/no)?" { send "yes\\r" } "password: " { send "xxxxxxxx\\r" interact exit 0 } } 
  2. 如果您的根本目的只是自動ssh,請嘗試使用sshpass,但sshpass通常不是默認安裝:

     `sshpass -p xxxxxxxx ssh root@127.0.0.1` 
  3. 這里還提供了設置公用/專用密鑰對的過程,這使您無需密碼即可訪問目標服務器:

     Generate a key pair, id_dsa.pub id_dsa with following command: ssh-keygen -t dsa -b 1024 Move id_dsa to client:/home/xxxx/.ssh/ Move id_dsa.pub to server:/home/xxxx/.ssh/, and "cat id_dsa.pub >> authorized_keys" chmod 600 /home/xxxx/.ssh/* (both the id_dsa and remote authorized_keys) 
set timeout 30
spawn ssh -o "StrictHostKeyChecking no" "$usr@$ip"
expect {
    "RSA key fingerprint" {send "yes\r"; exp_continue}
    -re "<regex for whatever the yes/no question is>" {send "yes\r"; exp_continue}
    "assword:" {send "$password\r"}
}
sleep 5
interact

它將使所有內容保持循環,直到超時時間(30s)。 然后它將僅移至腳本的下一行。 我還增加了對RSA的期望,以備您需要時使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM