MEr*_*ric 0 authentication ssh bash expect
我在bash脚本中包含了简单的Expect命令(我知道我可能只是在编写一个纯Expect脚本,但是我想让它在bash中运行)。
脚本如下:
#!/bin/bash
OUTPUT=$(expect -c '
spawn ssh mihail911@blah.org
expect "password:"
send "dog\r"
')
Run Code Online (Sandbox Code Playgroud)
切换到上述地址后,它将mihail911's password:在提示符下返回某种形式的表单,因此我认为我的期望行是有效的。当我运行此脚本时,我的脚本不会打印任何内容。它甚至不显示password:提示。通常,即使我手动提供了错误的密码,我也会收到Incorrect password-type响应提示。为什么什么都没有打印,如何使脚本正确执行?我已经尝试使用该-d标志进行调试,并且似乎表明至少第一个期望提示符已正确匹配。
另外,我应该在OUTPUT变量中期望什么值?当我使用echo此变量时,它只是简单地先输出脚本期望部分的第一个命令,然后打印mihail911's password:。这是应该打印的内容吗?
#!/bin/bash
OUTPUT=$(expect -c '
# To suppress any other form of output generated by spawned process
log_user 0
spawn ssh dinesh@xxx.xxx.xx.xxx
# To match some common prompts. Update it as per your needs.
# To match literal dollar, it is escaped with backslash
set prompt "#|>|\\$"
expect {
eof {puts "Connection rejected by the host"; exit 0}
timeout {puts "Unable to access the host"; exit 0;}
"password:"
}
send "root\r"
expect {
timeout {puts "Unable to access the host"; exit 0;}
-re $prompt
}
send "date\r"
# Matching only the date cmd output alone
expect {
timeout { puts "Unable to access the host";exit 0}
-re "\n(\[^\r]*)\r"
}
send_user "$expect_out(1,string)\n"
exit 1
')
echo "Expect's return value : $?"; # Printing value returned from 'Expect'
echo "Expect Output : $OUTPUT"
Run Code Online (Sandbox Code Playgroud)
输出:
dinesh@MyPC:~/stackoverflow$ ./Meric
Expect's return value : 1
Expect Output : Wed Sep 2 09:35:14 IST 2015
dinesh@MyPC:~/stackoverflow$
Run Code Online (Sandbox Code Playgroud)