使用bash脚本自动执行telnet会话

kha*_*han 75 linux bash telnet

我正在使用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.
Run Code Online (Sandbox Code Playgroud)

我在这面面有两个问题:

  1. 如何从脚本执行远程系统上的命令(没有人工交互)?

    根据我对一些测试代码的经验,我能够推断出当执行telnet 10.1.1.1时,telnet进入交互式会话,脚本中的后续代码行在本地系统上执行.如何在远程系统而不是本地代码上运行代码行?

  2. 我无法在本地系统上的telnet会话中获取活动的日志文件.我使用的stdout重定向在远程系统上创建一个副本(我不想执行复制操作将日志复制到本地系统).我该如何实现此功能?

thi*_*ton 78

虽然我建议使用expect,但对于非交互式使用,正常的shell命令可能就足够了.Telnet在stdin上接受它的命令,所以你只需要将命令管道或写入其中:

telnet 10.1.1.1 <<EOF
remotecommand 1
remotecommand 2
EOF
Run Code Online (Sandbox Code Playgroud)

(编辑:从评论来看,远程命令需要一些时间来处理输入,或者telnet没有正常使用早期的SIGHUP.在这些情况下,你可能会尝试在输入上进行短暂的睡眠:)

{ echo "remotecommand 1"; echo "remotecommand 2"; sleep 1; } | telnet 10.1.1.1
Run Code Online (Sandbox Code Playgroud)

无论如何,如果它是交互式或任何东西,请使用expect.

  • "睡眠"和支撑表达式的+1 (11认同)
  • 这不起作用,因为您关闭与EOF的连接. (2认同)
  • +1 `sleep` 就像做梦一样 ;-) 我还添加了 `| tee /dev/tty |` 中间有完整的会话;-) (2认同)

joe*_*ney 73

写一个expect脚本.

这是一个例子:

#!/usr/bin/expect

#If it all goes pear shaped the script will timeout after 20 seconds.
set timeout 20
#First argument is assigned to the variable name
set name [lindex $argv 0]
#Second argument is assigned to the variable user
set user [lindex $argv 1]
#Third argument is assigned to the variable password
set password [lindex $argv 2]
#This spawns the telnet program and connects it to the variable name
spawn telnet $name 
#The script expects login
expect "login:" 
#The script sends the user variable
send "$user "
#The script expects Password
expect "Password:"
#The script sends the password variable
send "$password "
#This hands control of the keyboard over to you (Nice expect feature!)
interact
Run Code Online (Sandbox Code Playgroud)

  • “期望”主要用于交互式会话。我想要的是一个自动化的非交互式会话。我面临的问题是我无法在远程系统上(通过telnet)运行脚本,因为_telnet 10.1.1.1_之后的所有行都在本地计算机上而不是我正在访问的计算机上执行。 (2认同)
  • 正如Thomas Telensky指出的那样,根据您的需求,ssh最终会更容易。但是期望可以是完全非交互的。我假设您的意思是,telnet之后的所有行都是您要在远程计算机上执行的行,在这种情况下,只需将它们作为发送命令添加到期望脚本中,然后删除interact命令。但是ssh更容易,更安全。 (2认同)
  • 很公平。我将使用 expect 来完成这项任务。 (2认同)

bie*_*era 41

学习HTTP协议时经常使用Telnet.我以前使用该脚本作为我的web-scraper的一部分:

echo "open www.example.com 80" 
sleep 2 
echo "GET /index.html HTTP/1.1" 
echo "Host: www.example.com" 
echo 
echo 
sleep 2
Run Code Online (Sandbox Code Playgroud)

假设脚本的名称是get-page.sh然后:

get-page.sh | telnet
Run Code Online (Sandbox Code Playgroud)

会给你一个HTML文件.

希望它对某人有帮助;)


Nik*_*hil 10

这对我有用..

我试图自动化多个需要用户名和密码的telnet登录.因为我将日志从不同的服务器保存到我的机器,所以telnet会话需要无限期地在后台运行.

telnet.sh使用'expect'命令自动执行telnet登录.更多信息可以在这里找到:http://osix.net/modules/article/?id = 30

telnet.sh

#!/usr/bin/expect
set timeout 20
set hostName [lindex $argv 0]
set userName [lindex $argv 1]
set password [lindex $argv 2]

spawn telnet $hostName

expect "User Access Verification"
expect "Username:"
send "$userName\r"
expect "Password:"
send "$password\r";
interact
Run Code Online (Sandbox Code Playgroud)

sample_script.sh用于通过运行telnet.sh为每个telnet会话创建后台进程.可以在代码的注释部分找到更多信息.

sample_script.sh

#!/bin/bash
#start screen in detached mode with session-name 'default_session' 
screen -dmS default_session -t screen_name 
#save the generated logs in a log file 'abc.log' 
screen -S default_session -p screen_name -X stuff "script -f /tmp/abc.log $(printf \\r)"
#start the telnet session and generate logs
screen -S default_session -p screen_name -X stuff "expect telnet.sh hostname username password $(printf \\r)"
Run Code Online (Sandbox Code Playgroud)
  1. 使用命令'screen -ls'确保背景中没有运行屏幕.
  2. 阅读 http://www.gnu.org/software/screen/manual/screen.html#Stuff以阅读有关屏幕及其选项的更多信息.
  3. sample_script.sh中的'-p'选项预先选择并重新连接到特定窗口以通过'-X'选项发送命令,否则会出现'No screen session found'错误.