从 shell 脚本生成交互式 telnet 会话

发布于 2024-07-05 05:06:47 字数 635 浏览 7 评论 0原文

我正在尝试编写一个脚本来允许我登录到控制台服务器的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

恬淡成诗 2024-07-12 05:06:47

尝试这些链接。

http://planetozh.com/blog/2004/02/telnet-script/

http://www.unix.com/unix -dummies-questions-answers/193-telnet-script.html

#!/bin/sh
( echo open hostname
sleep 5
echo username
sleep 1
echo password
sleep 1
echo some more output, etc. ) | telnet

他们为我工作:D

Try these links.

http://planetozh.com/blog/2004/02/telnet-script/

http://www.unix.com/unix-dummies-questions-answers/193-telnet-script.html

#!/bin/sh
( echo open hostname
sleep 5
echo username
sleep 1
echo password
sleep 1
echo some more output, etc. ) | telnet

They worked for me :D

海螺姑娘 2024-07-12 05:06:47

也许您可以尝试 bash -i 强制会话处于交互模式。

Perhaps you could try bash -i to force the session to be in interactive mode.

失退 2024-07-12 05:06:47

您的示例中的问题是您将脚本的输入(以及间接的 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 the echo. So after echo is done and telnet is started, there is no more input to read. A simple fix could be to replace echo "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.

心安伴我暖 2024-07-12 05:06:47

如果您的环境是基于 X11 的,则可以打开一个运行 telnet 的 xterm:

xterm -e telnet $host $port

xterm 中的操作是交互式的,并且 shell 脚本会停止,直到 xterm 终止。

If your environment is X11-based, a possibility is to open an xterm running telnet:

xterm -e telnet $host $port

Operations in xterm are interactive and shell script is halted until xterm termination.

醉态萌生 2024-07-12 05:06:47

@muz
我有一个 ssh 设置,没有 telnet,所以我无法测试您的问题是否与 telnet 相关,但运行以下脚本将我连续记录到不同的机器,要求输入密码。

for i in adele betty
do
ssh all@$i
done

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

for i in adele betty
do
ssh all@$i
done
删除→记忆 2024-07-12 05:06:47

我认为你应该看看expect程序。 它存在于所有现代 Linux 发行版中。 下面是一些示例脚本:

#!/usr/bin/expect -f
spawn telnet $host_name
expect {
   "T0>"                {}
   -re "Connection refused|No route to host|Invalid argument|lookup failure"
                        {send_user "\r******* connection error, bye.\n";exit}
   default              {send_user "\r******* connection error (telnet timeout),
 bye.\n";exit}
}
send "command\n"
expect -timeout 1 "something"

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:

#!/usr/bin/expect -f
spawn telnet $host_name
expect {
   "T0>"                {}
   -re "Connection refused|No route to host|Invalid argument|lookup failure"
                        {send_user "\r******* connection error, bye.\n";exit}
   default              {send_user "\r******* connection error (telnet timeout),
 bye.\n";exit}
}
send "command\n"
expect -timeout 1 "something"

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

抚你发端 2024-07-12 05:06:47

您需要将终端输入重定向到 telnet 进程。 这应该是/dev/tty。 所以你的脚本看起来像这样:

#!/bin/bash

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

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
著墨染雨君画夕 2024-07-12 05:06:47

使用 Shell 脚本 Telnet 到服务器 示例:

Test3.sh 文件:

#!/bin/sh

#SSG_details is file from which script will read ip adress and uname/password
#to telnet.

SSG_detail=/opt/Telnet/SSG_detail.txt

cat $SSG_detail | while read ssg_det ; do

   ssg_ip=`echo $ssg_det|awk '{print $1}'`
   ssg_user=`echo $ssg_det|awk '{print $2}'`
   ssg_pwd=`echo $ssg_det|awk '{print $3}'`


   echo " IP to telnet:" $ssg_ip
   echo " ssg_user:" $ssg_user
   echo " ssg_pwd:" $ssg_pwd

   sh /opt/Telnet/Call_Telenet.sh $ssg_ip $ssg_user $ssg_pwd 

done


exit 0

Call_Telenet.sh 脚本如下:

#!/bin/sh

DELAY=1 
COMM1='config t'                 #/* 1st commands to be run*/
COMM2='show run'
COMM3=''
COMM4=''
COMM5='exit'
COMM6='wr'
COMM7='ssg service-cache refresh all'
COMM8='exit'                     #/* 8th command to be run */


telnet $1 >> $logfile 2>> $logfile |&
sleep $DELAY
echo -p $2 >> $logfile 2>> $logfile
sleep $DELAY
echo -p $3 >> $logfile 2>> $logfile
sleep $DELAY
echo -p $4 >> $logfile 2>> $logfile
sleep $DELAY
echo -p $5 >> $logfile 2>> $logfile
sleep $DELAY

sleep $DELAY
sleep $DELAY
sleep $DELAY
echo -p $COMM7 >> $logfile 2>> $logfile
sleep $DELAY
echo -p $COMM8 >> $logfile 2>> $logfile
sleep $DELAY

exit 0

按如下方式运行上述文件:

gt; ./test3.sh 

Telnet to Server using Shell Script Example:

Test3.sh File:

#!/bin/sh

#SSG_details is file from which script will read ip adress and uname/password
#to telnet.

SSG_detail=/opt/Telnet/SSG_detail.txt

cat $SSG_detail | while read ssg_det ; do

   ssg_ip=`echo $ssg_det|awk '{print $1}'`
   ssg_user=`echo $ssg_det|awk '{print $2}'`
   ssg_pwd=`echo $ssg_det|awk '{print $3}'`


   echo " IP to telnet:" $ssg_ip
   echo " ssg_user:" $ssg_user
   echo " ssg_pwd:" $ssg_pwd

   sh /opt/Telnet/Call_Telenet.sh $ssg_ip $ssg_user $ssg_pwd 

done


exit 0

The Call_Telenet.sh script is as follows:

#!/bin/sh

DELAY=1 
COMM1='config t'                 #/* 1st commands to be run*/
COMM2='show run'
COMM3=''
COMM4=''
COMM5='exit'
COMM6='wr'
COMM7='ssg service-cache refresh all'
COMM8='exit'                     #/* 8th command to be run */


telnet $1 >> $logfile 2>> $logfile |&
sleep $DELAY
echo -p $2 >> $logfile 2>> $logfile
sleep $DELAY
echo -p $3 >> $logfile 2>> $logfile
sleep $DELAY
echo -p $4 >> $logfile 2>> $logfile
sleep $DELAY
echo -p $5 >> $logfile 2>> $logfile
sleep $DELAY

sleep $DELAY
sleep $DELAY
sleep $DELAY
echo -p $COMM7 >> $logfile 2>> $logfile
sleep $DELAY
echo -p $COMM8 >> $logfile 2>> $logfile
sleep $DELAY

exit 0

Run the above file as follows:

gt; ./test3.sh 
浅暮の光 2024-07-12 05:06:47

谢谢戴夫 - 这是我缺少的 TTY 重定向。

我使用的完整解决方案,对于那些感兴趣的人:

#!/bin/bash

TTY=`tty` # Find out what tty we have been invoked from.
for i in `cat hostnames.csv` # List of hosts/ports
do
        # Separate port/host into separate variables
        host=`echo $i | awk -F, '{ print $1 }'`
        port=`echo $i | awk -F, '{ print $2 }'`
        telnet $host $port < $TTY # Connect to the current device
done

Thanks Dave - it was the TTY redirection that I was missing.

The complete solution I used, for those who are interested:

#!/bin/bash

TTY=`tty` # Find out what tty we have been invoked from.
for i in `cat hostnames.csv` # List of hosts/ports
do
        # Separate port/host into separate variables
        host=`echo $i | awk -F, '{ print $1 }'`
        port=`echo $i | awk -F, '{ print $2 }'`
        telnet $host $port < $TTY # Connect to the current device
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文