从shell脚本产卵一个交互式的telnet会话 [英] Spawning an interactive telnet session from a shell script

查看:721
本文介绍了从shell脚本产卵一个交互式的telnet会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个脚本,让我登录到控制台服务器48端口,这样我就可以快速确定哪些设备连接到每个串行线。

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.

基本上我希望能够有一个脚本,鉴于主机/端口的列表,Telnet方式登录到列表中的第一个设备,并让我在交互模式,这样我可以登录并确认设备,那么当我关闭telnet会话,连接到下一届会议在列表中。

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.

我面临的问题是,如果我从一个可执行bash脚本中启动telnet会话,会话立即终止,而不是等待输入。

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.

例如,下面给出code:

For example, given the following code:

$ cat ./telnetTest.sh
#!/bin/bash

while read line
do
        telnet $line
done
$

当我运行命令'回声主机名| testscript.sh我收到以下输出:

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.
$

有谁知道一种方法来阻止telnet会话被自动关闭?

Does anyone know of a way to stop the telnet session being closed automatically?

推荐答案

您需要的端子输入重定向到远程登录过程。这应该是的/ 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:

#!/bin/bash

for HOST in `cat`
do
  echo Connecting to $HOST...
  telnet $HOST </dev/tty
done

这篇关于从shell脚本产卵一个交互式的telnet会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆