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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/100829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:40:43  来源:igfitidea点击:

Spawning an interactive telnet session from a shell script

bashtelnet

提问by Murali Suriar

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.

我正在尝试编写一个脚本来允许我登录到控制台服务器的 48 个端口,以便我可以快速确定每个串行线路连接了哪些设备。

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.

本质上,我希望能够有一个脚本,在给定主机/端口列表的情况下,telnet 到列表中的第一个设备并让我处于交互模式,以便我可以登录并确认设备,然后当我关闭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.

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

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

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

有谁知道停止自动关闭 telnet 会话的方法?

采纳答案by Dave Webb

You need to redirect the Terminal input to the telnetprocess. This should be /dev/tty. So your script will look something like:

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

#!/bin/bash

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

回答by user11323

I think you should look at expectprogram. It`s present in all modern linux distros. Here is some exmaple script:

我认为你应该看看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"

spawncommand start remote login program (telnet, ssh, netcat etc)

spawn命令启动远程登录程序(telnet、ssh、netcat 等)

expextcommand used to... hm.. expect something from remote session

expext命令用于......嗯.. 期望从远程会话中得到一些东西

send- sending commands

发送- 发送命令

send_user- to print comments to stdout

send_user- 将评论打印到标准输出

回答by Murali Suriar

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

谢谢 Dave - 我错过了 TTY 重定向。

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  }'`
        port=`echo $i | awk -F, '{ print  }'`
        telnet $host $port < $TTY # Connect to the current device
done

回答by Prashant Ghodke

Telnet to Server using Shell Script Example:

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

Test3.shFile:

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 }'`
   ssg_user=`echo $ssg_det|awk '{print }'`
   ssg_pwd=`echo $ssg_det|awk '{print }'`


   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.shscript is as follows:

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  >> $logfile 2>> $logfile |&
sleep $DELAY
echo -p  >> $logfile 2>> $logfile
sleep $DELAY
echo -p  >> $logfile 2>> $logfile
sleep $DELAY
echo -p  >> $logfile 2>> $logfile
sleep $DELAY
echo -p  >> $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:

运行上面的文件如下:

$> ./test3.sh 

回答by mouviciel

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

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

xterm -e telnet $host $port

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

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

回答by Graham

Try these links.

试试这些链接。

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

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

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

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

他们对我来说有效:D

回答by mweerden

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 echois done and telnetis started, there is no more input to read. A simple fix could be to replace echo "testhost"by { echo "testhost"; cat; }.

您的示例中的问题是您将脚本的输入(以及间接的telnet)链接到echo. 所以在echo完成并telnet启动之后,就没有更多的输入可以读取了。一个简单的解决方法是将其替换echo "testhost"{ echo "testhost"; cat; }.

Edit: telnetdoesn't seem to like taking input from a pipe. However, netcatdoes and is probably just suitable in this case.

编辑:telnet似乎不喜欢从管道中获取输入。但是,netcat确实并且可能仅适用于这种情况。

回答by stephanea

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

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

回答by stephanea

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

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

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