在 expect 脚本的密码提示符处点击 return 而不是发送密码
Hitting a return at password prompt in expect script instead of sending password
我正在尝试在 bash 中使用 expect 脚本来登录路由器、执行命令并将输出存储在文本文件中。
#!/usr/bin/bash
FQDN=
LogFile=/tmp/Router_${FQDN}.txt
> $LogFile
expect -d <<EOF > $LogFile
set timeout 20
set FQDN [lindex $argv 0]
set Username "user"
set Password "***$$$"
spawn ssh $Username@$FQDN
expect "*assword:"
send "$Password\r"
expect "#"
send "some command\r"
expect "#"
send "exit\r"
sleep 1
exit
expect eof
EOF
cat $LogFile
我收到以下错误消息。
system personnel =\r\r\n= may provide the evidence of such monitoring to law enforcement officials. =\r\r\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==\r\r\npassword: "
send: sending "\n" to { exp6 }
expect: does "" (spawn_id exp6) match glob pattern "#"? no
password:
Enter old password:
根据错误,脚本似乎正在点击 {return} 键“\r”,该键不会在密码提示时发送。
我没有 return 一旦我 ssh。不确定我哪里错了。
这是我期望的脚本,运行良好。只有当我在 bash 脚本中编写代码时才会失败。
#!/usr/bin/expect -f
set timeout 20
set FQDN [lindex $argv 0]
set Username "user"
set Password "***$$$"
spawn ssh -o "StrictHostKeyChecking no" $Username@$FQDN
expect "*assword: "
send "$Password\r"
expect "#"
send "some command\r"
expect "#"
send "exit\r"
sleep 1
exit
-阿比
在 here-doc 中,像 $Username
和 $Password
这样的变量正在被 shell 扩展,因此它们不会被视为 Expect
的文字扩张。由于那些 shell 变量未在任何地方设置,因此它们被扩展为空字符串。结果,它正在执行 ssh @$FQDN
并发送空密码。
您需要转义 $
以便 Expect
可以处理它们。
您也不需要 Expect 脚本中的 set FQDN
行,因为您为此使用了 shell 变量。
#!/usr/bin/bash
FQDN=
LogFile=/tmp/Router_${FQDN}.txt
> $LogFile
expect -d <<EOF > $LogFile
set timeout 20
set Username "user"
set Password "***$$$"
spawn ssh $Username@$FQDN
expect "*assword:"
send "$Password\r"
expect "#"
send "some command\r"
expect "#"
send "exit\r"
sleep 1
exit
expect eof
EOF
cat $LogFile
或者您可以将它们设置为 shell 变量,就像 FQDN
.
#!/usr/bin/bash
FQDN=
Username=user
Password="***$$$"
LogFile=/tmp/Router_${FQDN}.txt
> $LogFile
expect -d <<EOF > $LogFile
set timeout 20
spawn ssh $Username@$FQDN
expect "*assword:"
send "$Password\r"
expect "#"
send "some command\r"
expect "#"
send "exit\r"
sleep 1
exit
expect eof
EOF
cat $LogFile
我正在尝试在 bash 中使用 expect 脚本来登录路由器、执行命令并将输出存储在文本文件中。
#!/usr/bin/bash
FQDN=
LogFile=/tmp/Router_${FQDN}.txt
> $LogFile
expect -d <<EOF > $LogFile
set timeout 20
set FQDN [lindex $argv 0]
set Username "user"
set Password "***$$$"
spawn ssh $Username@$FQDN
expect "*assword:"
send "$Password\r"
expect "#"
send "some command\r"
expect "#"
send "exit\r"
sleep 1
exit
expect eof
EOF
cat $LogFile
我收到以下错误消息。
system personnel =\r\r\n= may provide the evidence of such monitoring to law enforcement officials. =\r\r\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==\r\r\npassword: "
send: sending "\n" to { exp6 }
expect: does "" (spawn_id exp6) match glob pattern "#"? no
password:
Enter old password:
根据错误,脚本似乎正在点击 {return} 键“\r”,该键不会在密码提示时发送。
我没有 return 一旦我 ssh。不确定我哪里错了。
这是我期望的脚本,运行良好。只有当我在 bash 脚本中编写代码时才会失败。
#!/usr/bin/expect -f
set timeout 20
set FQDN [lindex $argv 0]
set Username "user"
set Password "***$$$"
spawn ssh -o "StrictHostKeyChecking no" $Username@$FQDN
expect "*assword: "
send "$Password\r"
expect "#"
send "some command\r"
expect "#"
send "exit\r"
sleep 1
exit
-阿比
在 here-doc 中,像 $Username
和 $Password
这样的变量正在被 shell 扩展,因此它们不会被视为 Expect
的文字扩张。由于那些 shell 变量未在任何地方设置,因此它们被扩展为空字符串。结果,它正在执行 ssh @$FQDN
并发送空密码。
您需要转义 $
以便 Expect
可以处理它们。
您也不需要 Expect 脚本中的 set FQDN
行,因为您为此使用了 shell 变量。
#!/usr/bin/bash
FQDN=
LogFile=/tmp/Router_${FQDN}.txt
> $LogFile
expect -d <<EOF > $LogFile
set timeout 20
set Username "user"
set Password "***$$$"
spawn ssh $Username@$FQDN
expect "*assword:"
send "$Password\r"
expect "#"
send "some command\r"
expect "#"
send "exit\r"
sleep 1
exit
expect eof
EOF
cat $LogFile
或者您可以将它们设置为 shell 变量,就像 FQDN
.
#!/usr/bin/bash
FQDN=
Username=user
Password="***$$$"
LogFile=/tmp/Router_${FQDN}.txt
> $LogFile
expect -d <<EOF > $LogFile
set timeout 20
spawn ssh $Username@$FQDN
expect "*assword:"
send "$Password\r"
expect "#"
send "some command\r"
expect "#"
send "exit\r"
sleep 1
exit
expect eof
EOF
cat $LogFile