我正在使用 Bash 脚本自动化一些与 telnet
相关的任务。
一旦自动化,用户将不会与 telnet
进行交互(即完全自动化)
脚本看起来像这样:
# execute some commands on the local system
# access a remote system with an IP address: 10.1.1.1 (for example)
telnet 10.1.1.1
# execute some commands on the remote system
# log all the activity (in a file) on the Local system
# exit telnet
# continue on with executing the rest of the script.
我在这里面临两个问题:
如何通过脚本在远程系统上执行命令(无需人工干预)?
根据我对一些测试代码的经验,我能够推断,当执行 telnet 10.1.1.1
时,telnet 进入交互式 session 并执行脚本中的后续代码行在本地系统上。如何在远程系统而不是本地系统上运行代码行?
我无法获取本地系统上 telnet
session 中事件的日志文件。我使用的标准输出重定向在远程系统上进行了复制(我不想执行复制操作将日志复制到本地系统)。我怎样才能实现这个功能?
最佳答案
虽然我也建议使用 expect
,但对于非交互式使用,普通的 shell 命令可能就足够了。 telnet
在标准输入上接受它的命令,所以你只需要通过 heredoc 将命令通过管道或写入它。 :
telnet 10.1.1.1 <<EOF
remotecommand 1
remotecommand 2
EOF
(编辑:从评论来看,远程命令需要一些时间来处理输入,或者早期的 SIGHUP 没有被 telnet
优雅地采用。在这些情况下,您可以尝试在输入上短暂 sleep :)
{ echo "remotecommand 1"; echo "remotecommand 2"; sleep 1; } | telnet 10.1.1.1
在任何情况下,如果它是交互式的或任何东西,请使用 expect
。
https://stackoverflow.com/questions/7013137/