期待 if 条件与 ssh 密码
Expect if condition with ssh password
我目前正在尝试创建一个带有错误处理的脚本。
脚本基本上使用此命令测试 ssh 连接:
ssh -o BatchMode=yes $machine uname -a
我想处理 3 种潜在情况:
- SSH 在没有密码的情况下工作正常
- SSH 被阻止,因为机器不在
.ssh
的 known_hosts
文件中
- SSH 被阻止,因为机器不在
.ssh
中的 known_hosts
文件中 AND 它需要密码才能继续(这意味着 id_rsa.pub
不在 .ssh
的 authorized_keys
文件中
我在主脚本上调用了一个 expect 脚本,这里是主脚本的样子:
ssh -o BatchMode=yes ${machine} uname -a &> temp-file.txt 2>&1
# Here we test the ssh connection just once and we store the output of the command in a temp file
if [ $? -eq 255 ]
# If the ssh didn't work
then
if grep -q "Host key verification failed." temp-file.txt
# If the error message is "Host key verification failed."
then
expect script-expect-knownhosts.exp ${machine} 2>&1 >/dev/null
这里是 script-expect-knownhosts.exp
文件,我试图在其中设置条件:
#!/usr/bin/expect -f
set machine [lindex $argv 0]
# Here we state that the first argument used with the command will be the $machine variable
set prompt "#|%|>|$ $"
set timeout 60
spawn ssh $machine
# We do a ssh on the machine
set prompt "#|%|>|$ $"
expect {
"Are you sure you want to continue connecting (yes/no)? " {send "yes\r";exp_continue}
# If he asks for a yes/no answer, then answer yes to add the machine to the known_hosts file
-exact "Password: " {send -- "^C";exp_continue}
# If he asks for a password, then send a CTRL + C
-re $prompt {send "exit\r";exp_continue}
# If the prompt shows up (if after the yes/no question, we don't need to put a password in) then type exit
}
下面是当我用机器执行 expect 脚本时发生的情况,以防第 2 种情况(工作正常):
spawn ssh machine
Are you sure you want to continue connecting (yes/no)? yes
machine:~ # exit
deconnection
Connection to machine closed.
下面是我在第 3 种情况下用机器执行 expect 脚本时发生的情况:
spawn ssh machine
Are you sure you want to continue connecting (yes/no)? yes
Password:
它一直卡在密码上,直到我手动执行 CTRL + C
在情况 2 和 3 中,您不需要 exp_continue
,因为您正在停止连接过程。
对于情况 2,我不认为你真的想发送 control-C。当您以交互方式执行此操作时,键入 control-C 具有发送信号以终止与您交互的进程的效果。你真正想要的是停止 ssh
进程,所以你应该做 close
.
而不是 send -- "^C";exp_continue
我目前正在尝试创建一个带有错误处理的脚本。
脚本基本上使用此命令测试 ssh 连接:
ssh -o BatchMode=yes $machine uname -a
我想处理 3 种潜在情况:
- SSH 在没有密码的情况下工作正常
- SSH 被阻止,因为机器不在
.ssh
的 - SSH 被阻止,因为机器不在
.ssh
中的known_hosts
文件中 AND 它需要密码才能继续(这意味着id_rsa.pub
不在.ssh
的
known_hosts
文件中
authorized_keys
文件中
我在主脚本上调用了一个 expect 脚本,这里是主脚本的样子:
ssh -o BatchMode=yes ${machine} uname -a &> temp-file.txt 2>&1
# Here we test the ssh connection just once and we store the output of the command in a temp file
if [ $? -eq 255 ]
# If the ssh didn't work
then
if grep -q "Host key verification failed." temp-file.txt
# If the error message is "Host key verification failed."
then
expect script-expect-knownhosts.exp ${machine} 2>&1 >/dev/null
这里是 script-expect-knownhosts.exp
文件,我试图在其中设置条件:
#!/usr/bin/expect -f
set machine [lindex $argv 0]
# Here we state that the first argument used with the command will be the $machine variable
set prompt "#|%|>|$ $"
set timeout 60
spawn ssh $machine
# We do a ssh on the machine
set prompt "#|%|>|$ $"
expect {
"Are you sure you want to continue connecting (yes/no)? " {send "yes\r";exp_continue}
# If he asks for a yes/no answer, then answer yes to add the machine to the known_hosts file
-exact "Password: " {send -- "^C";exp_continue}
# If he asks for a password, then send a CTRL + C
-re $prompt {send "exit\r";exp_continue}
# If the prompt shows up (if after the yes/no question, we don't need to put a password in) then type exit
}
下面是当我用机器执行 expect 脚本时发生的情况,以防第 2 种情况(工作正常):
spawn ssh machine
Are you sure you want to continue connecting (yes/no)? yes
machine:~ # exit
deconnection
Connection to machine closed.
下面是我在第 3 种情况下用机器执行 expect 脚本时发生的情况:
spawn ssh machine
Are you sure you want to continue connecting (yes/no)? yes
Password:
它一直卡在密码上,直到我手动执行 CTRL + C
在情况 2 和 3 中,您不需要 exp_continue
,因为您正在停止连接过程。
对于情况 2,我不认为你真的想发送 control-C。当您以交互方式执行此操作时,键入 control-C 具有发送信号以终止与您交互的进程的效果。你真正想要的是停止 ssh
进程,所以你应该做 close
.
send -- "^C";exp_continue