从 shell 脚本生成交互式 telnet 会话
我正在尝试编写一个脚本来允许我登录到控制台服务器的 48 个端口,以便我可以快速确定哪些设备连接到每条串行线路。
本质上,我希望能够有一个脚本,给定主机/端口列表,远程登录到列表中的第一个设备,并使我处于交互模式,以便我可以登录并确认设备,然后当我关闭telnet 会话,连接到列表中的下一个会话。
我面临的问题是,如果我从可执行 bash 脚本中启动 telnet 会话,会话会立即终止,而不是等待输入。
例如,给出以下代码:
$ cat ./telnetTest.sh
#!/bin/bash
while read line
do
telnet $line
done
$
当我运行命令 'echo "hostname" | testscript.sh' 我收到以下输出:
$ echo "testhost" | ./telnetTest.sh
Trying 192.168.1.1...
Connected to testhost (192.168.1.1).
Escape character is '^]'.
Connection closed by foreign host.
$
有谁知道如何阻止 telnet 会话自动关闭?
I'm trying to write a script to allow me to log in to a console servers 48 ports so that I can quickly determine what devices are connected to each serial line.
Essentially I want to be able to have a script that, given a list of hosts/ports, telnets to the first device in the list and leaves me in interactive mode so that I can log in and confirm the device, then when I close the telnet session, connects to the next session in the list.
The problem I'm facing is that if I start a telnet session from within an executable bash script, the session terminates immediately, rather than waiting for input.
For example, given the following code:
$ cat ./telnetTest.sh
#!/bin/bash
while read line
do
telnet $line
done
$
When I run the command 'echo "hostname" | testscript.sh' I receive the following output:
$ echo "testhost" | ./telnetTest.sh
Trying 192.168.1.1...
Connected to testhost (192.168.1.1).
Escape character is '^]'.
Connection closed by foreign host.
$
Does anyone know of a way to stop the telnet session being closed automatically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
尝试这些链接。
http://planetozh.com/blog/2004/02/telnet-script/
http://www.unix.com/unix -dummies-questions-answers/193-telnet-script.html
他们为我工作:D
Try these links.
http://planetozh.com/blog/2004/02/telnet-script/
http://www.unix.com/unix-dummies-questions-answers/193-telnet-script.html
They worked for me :D
也许您可以尝试 bash -i 强制会话处于交互模式。
Perhaps you could try bash -i to force the session to be in interactive mode.
您的示例中的问题是您将脚本的输入(以及间接的
telnet
)链接到echo
的输出。 因此,在echo
完成并启动telnet
后,就不再需要读取任何输入。 一个简单的修复方法是将echo "testhost"
替换为{ echo "testhost"; 猫; }
。编辑: telnet 似乎不喜欢从管道获取输入。 然而,
netcat
确实并且可能恰好适合这种情况。The problem in your example is that you link the input of your script (and indirectly of
telnet
) to the output of theecho
. So afterecho
is done andtelnet
is started, there is no more input to read. A simple fix could be to replaceecho "testhost"
by{ echo "testhost"; cat; }
.Edit:
telnet
doesn't seem to like taking input from a pipe. However,netcat
does and is probably just suitable in this case.如果您的环境是基于 X11 的,则可以打开一个运行 telnet 的 xterm:
xterm 中的操作是交互式的,并且 shell 脚本会停止,直到 xterm 终止。
If your environment is X11-based, a possibility is to open an xterm running telnet:
Operations in xterm are interactive and shell script is halted until xterm termination.
@muz
我有一个 ssh 设置,没有 telnet,所以我无法测试您的问题是否与 telnet 相关,但运行以下脚本将我连续记录到不同的机器,要求输入密码。
@muz
I have a setting with ssh, no telnet, so i can't test if your problem is telnet related, but running the following script logs me successively to the different machines asking for a password.
我认为你应该看看expect程序。 它存在于所有现代 Linux 发行版中。 下面是一些示例脚本:
spawn 命令启动远程登录程序(telnet、ssh、netcat 等)
expext 命令用于...嗯..期望从远程会话中得到一些东西
< strong>send - 发送命令
send_user - 将注释打印到标准输出
I think you should look at expect program. It`s present in all modern linux distros. Here is some exmaple script:
spawn command start remote login program (telnet, ssh, netcat etc)
expext command used to... hm.. expect something from remote session
send - sending commands
send_user - to print comments to stdout
您需要将终端输入重定向到
telnet
进程。 这应该是/dev/tty
。 所以你的脚本看起来像这样:You need to redirect the Terminal input to the
telnet
process. This should be/dev/tty
. So your script will look something like:使用 Shell 脚本 Telnet 到服务器 示例:
Test3.sh
文件:Call_Telenet.sh
脚本如下:按如下方式运行上述文件:
Telnet to Server using Shell Script Example:
Test3.sh
File:The
Call_Telenet.sh
script is as follows:Run the above file as follows:
谢谢戴夫 - 这是我缺少的 TTY 重定向。
我使用的完整解决方案,对于那些感兴趣的人:
Thanks Dave - it was the TTY redirection that I was missing.
The complete solution I used, for those who are interested: