如何在脚本中使用telnet



我需要在系统中首先进行SSH然后进行连接。然后,我可以开始执行一些命令。我正在为Telnet部分挣扎。你能告诉我我怎么能做到吗?请问还有其他选择吗?谢谢你

#!/bin/bash
cat command.sh | sshpass -p 'passowrd' ssh -q -o StrictHostKeyChecking=no root@pc1;

然后我的命令.sh

#!/bin/bash
spawn telnet pc_modem
expect "login:"
send "root"
expect "Password:"
send "youyou"
cliclient GetMonitoringData;

在此用途中,shebang没有效果*。您正在将脚本文件的内容传递给SSH,以按行执行行,就好像每行都是单独的命令,这些命令将由Shell而不是expect解释。

尝试将command.sh更改为:

# no shebang here
/bin/expect -f - <<<'spawn telnet pc_modem
expect "login:"
send "root"
expect "Password:"
send "youyou"
cliclient GetMonitoringData;'

这将expect脚本以A 的方式发送到此处的字符串期望的stdin。如果您在expect脚本中使用变量,则可能需要根据它们是外壳还是TCL变量来更改引号或逃脱的情况。

*当文件标记为可执行文件时,内核用于选择程序来解释文件的内容,并通过用其名称调用文件来运行。当文件通过明确命名解释器(例如sh run_messh user@host run_me_there(运行时,Shebang不会播放。

我找到了答案并完美工作:

/bin/expect <<<'spawn telnet pc_modem 
expect "login:"
send "rootr"
expect "Password: ";
send "youyour"
send "yourcommand1r"
send "yourcommand2r"
expect eof
'

最新更新