我有以下脚本:
#!/usr/bin/expect -f
set timeout 60
spawn ssh -X user@login.domain.co.uk
expect "Password:"
# Send the password, and then wait for a shell prompt.
send "password\r"
exp_continue
expect "user*"
send "ls -la\r"
但是我得到以下内容:
Password: command returned bad code: -101
while executing
"exp_continue"
(file "./hpclogin.sh" line 10)
如果我删除
exp_continue
,即#!/usr/bin/expect -f
set timeout 60
spawn ssh -X user@login.domain.co.uk
expect "Password:"
# Send the password, and then wait for a shell prompt.
send "password\r"
expect "user*"
send "ls -la\r"
我可以成功登录,但是没有执行
ls -la
命令。我的程序的流程控制有问题吗?
最佳答案
exp_continue
仅在expect
块内有用。例如:
spawn ssh -X user@example.com
expect {
"continue connecting (yes/no)? " {
send "yes\r"
exp_continue"
}
"Password:"
}
send "password\r"
我认为您没有看到
ls
输出,因为您不希望在发送后看到任何东西。根据您的工作流程,这里有两个想法:ssh
的参数spawn ssh -X user@example.com ls -la
expect {
"continue connecting (yes/no)? " {
send "yes\r"
exp_continue"
}
"Password:"
}
send "password\r"
expect eof
spawn ssh -X user@example.com
expect {
"continue connecting (yes/no)? " {
send "yes\r"
exp_continue"
}
"Password:"
}
send "password\r"
expect $theprompt
send "ls -la\r"
expect $theprompt
send "exit\r"
expect eof
当然,使用ssh键会更简单:
ssh-keygen
ssh-copy-id user@example.com
ssh -X user@example.com ls -la
关于bash - 期望登录后无法执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31099103/