在工作中,我每天都必须运行以下函数。当系统提示我输入密码时,该函数可以正常工作。但是,我尝试对密码进行硬编码,这样就不必每天都输入密码。这不起作用。有什么提示吗?
function update()
{
firewalluser=`whoami`
# -s => silent (no echo of characters), -p => prompt user
#read -s -p "Password: " firewallpass
firewallpass="mypassword"
TRUSTED=(
xxx.yyy.com
jenkins.xxx.com
svn.xxx.com
)
for server in ${TRUSTED[*]}
do
echo ""
echo "--> connecting to $server"
expect <<EOF
set timeout 20
spawn telnet $server
expect "Username: "
send "$firewalluser\r"
expect "Password: "
send "$firewallpass\r"
expect "Firewall User Authentication: Accepted"
send "exit"
exit
EOF
done
}
GNU bash, version 3.2.53(1)-release (x86_64-apple-darwin13)
我正在使用我定义的函数运行这个.bash_profile
。
提前致谢。
编辑:我添加了exp_internal 1
,这里是程序的响应:
--> connecting to some.domain.name.com
spawn telnet some.domain.name.com
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {64289}
expect: does "" (spawn_id exp7) match glob pattern "Username: "? no
Trying xx.yy.zz...
expect: does "Trying xx.yy.zz...\r\n" (spawn_id exp7) match glob pattern "Username: "? no
Connected to some.domain.name.com.
Escape character is '^]'.
expect: does "Trying xx.yy.zz...\r\nConnected to some.domain.name.com.\r\nEscape character is '^]'.\r\n" (spawn_id exp7) match glob pattern "Username: "? no
Please Authenticate to VSD DR2
Username:
expect: does "Trying xx.yy.zz...\r\nConnected to some.domain.name.com.\r\nEscape character is '^]'.\r\nPlease Authenticate to VSD DR2\r\nUsername: " (spawn_id exp7) match glob pa\
tern "Username: "? yes
expect: set expect_out(0,string) "Username: "
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) "Trying xx.yy.zz...\r\nConnected to some.domain.name.com.\r\nEscape character is '^]'.\r\nPlease Authenticate to VSD DR2\r\nUsername: "
send: sending "nklosterman\r" to { exp7 }
expect: does "" (spawn_id exp7) match glob pattern "Password: "? no
Password:
expect: does "\r\nPassword: " (spawn_id exp7) match glob pattern "Password: "? yes
expect: set expect_out(0,string) "Password: "
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) "\r\nPassword: "
send: sending "mypassword!\r" to { exp7 }
expect: does "" (spawn_id exp7) match glob pattern "Firewall User Authentication: Accepted"? no
Firewall User Authentication: Failed
expect: does "\nFirewall User Authentication: Failed\r\n" (spawn_id exp7) match glob pattern "Firewall User Authentication: Accepted"? no
Connection closed by foreign host.
expect: does "\nFirewall User Authentication: Failed\r\nConnection closed by foreign host.\r\n" (spawn_id exp7) match glob pattern "Firewall User Authentication: Accepted"? no
expect: read eof
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) "\nFirewall User Authentication: Failed\r\nConnection closed by foreign host.\r\n"
send: sending "exit" to { exp7 send: spawn id exp7 not open
while executing
"send "exit""
我可以看到我的密码被发送时附加了 \r。我不确定它为什么不接受它。我甚至尝试在 heredoc 中硬编码我的密码,而不是设置变量,但这没有用。
答案1
它看起来像是回车符与换行符的问题。
您的活动终端可能与远程系统不匹配,某些自动翻译可能不正确。您可以使用stty(1)命令。
来自预计文档:
在这种情况下,当您按下回车键时,它将被转换为换行符。如果 Expect 随后将其传递给将其终端设置为原始模式的程序(如 telnet),则会出现问题,因为该程序需要真正的返回。
解决方案不是手动用回车符替换换行符,而是使用命令“stty raw”,这将停止翻译。但请注意,这意味着您将不再获得经过处理的行编辑功能。
我建议您尝试在代码中替换。如果这仍然不起作用,则可能需要两者才能工作。如果仍然不起作用\r
,您将不得不排除两个系统的终端模式故障,以及回车符和换行符来回移动时会发生什么。\n
\r\n
此外,这当然假设您有正确的密码!;)