使用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. 

我在这里遇到两个问题:

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

    根据我对某些testing代码的经验,我可以推断出,当执行telnet 10.1.1.1时,telnet进入一个交互式会话,脚本中的后续代码行在本地系统上执行。 我怎样才能在远程系统而不是本地运行代码行?

  2. 我无法获得本地系统上telnet会话中活动的日志文件。 我使用的stdoutredirect在远程系统上创build一个副本(我不想执行复制操作将日志复制到本地系统)。 我怎样才能实现这个function?

编写一个expect脚本。

这是一个例子 。

 #!/usr/bin/expect #Where the script should be run from. #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 two you (Nice expect feature!) interact 

虽然我也build议使用期望,但对于非交互式使用,正常的shell命令可能就足够了。 Telnet在标准input接受它的命令,所以你只需要pipe道或写入命令到它:

 telnet 10.1.1.1 <<EOF remotecommand 1 remotecommand 2 EOF 

(编辑:从评论来看,远程命令需要一些时间来处理input,或者早期的SIGHUP不会被telnet正常使用,在这种情况下,你可以尝试短暂的input:)

 { echo "remotecommand 1"; echo "remotecommand 2"; sleep 1; } | telnet 10.1.1.1 

无论如何,如果互动或任何事情,使用expect

学习HTTP协议时经常使用Telnet。 我曾经使用该脚本作为我的networking刮板的一部分:

 echo "open www.example.com 80" sleep 2 echo "GET /index.html HTTP/1.1" echo "Host: www.example.com" echo echo sleep 2 

假设脚本的名字是get-page.sh,那么:

 get-page.sh | telnet 

会给你一个HTML文件。

希望对某人有所帮助;)

这对我有用..

我试图自动化需要用户名和密码的多个telnetlogin。 远程login会话需要无限期地在后台运行,因为我将来自不同服务器的日志保存到我的机器上。

telnet.sh使用'expect'命令自动执行telnetlogin。 更多信息可以在这里find: 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 

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

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)" 
  1. 通过使用命令“screen -ls”确保背景中没有屏幕运行。
  2. 阅读http://www.gnu.org/software/screen/manual/screen.html#Stuff阅读更多关于屏幕及其选项。;
  3. sample_script.sh中的'-p'选项预选并重新附加到特定的窗口,通过'-X'选项发送一个命令,否则你会得到'没有find屏幕会话'的错误。

你可以使用期望的脚本instashed bash。 下面的例子演示如何将telnex转换成无密码的embedded式板卡

 #!/usr/bin/expect set ip "<ip>" spawn "/bin/bash" send "telnet $ip\r" expect "'^]'." send "\r" expect "#" sleep 2 send "ls\r" expect "#" sleep 2 send -- "^]\r" expect "telnet>" send "quit\r" expect eof 

为此目的使用ssh。 生成密钥而不使用密码,并将其放置在远程计算机上的.authorized_keys。 创build远程运行的脚本,将其复制到另一台机器,然后使用ssh远程运行它。

我多次使用这种方法取得了巨大的成功。 另外请注意,它比telnet更安全。

 #!/bin/bash ping_count="4" avg_max_limit="1500" router="sagemcom-fast-2804-v2" adress="192.168.1.1" user="admin" pass="admin" VAR=$( expect -c " set timeout 3 spawn telnet "$adress" expect \"Login:\" send \"$user\n\" expect \"Password:\" send \"$pass\n\" expect \"commands.\" send \"ping ya.ru -c $ping_count\n\" set timeout 9 expect \"transmitted\" send \"exit\" ") count_ping=$(echo "$VAR" | grep packets | cut -c 1) avg_ms=$(echo "$VAR" | grep round-trip | cut -d '/' -f 4 | cut -d '.' -f 1) echo "1_____ping___$count_ping

____$avg_ms" echo "$VAR"

这里是如何在bash shell / expect中使用telnet

 #!/usr/bin/expect # just do a chmod 755 one the script # ./YOUR_SCRIPT_NAME.sh $YOUHOST $PORT # if you get "Escape character is '^]'" as the output it means got connected otherwise it has failed set ip [lindex $argv 0] set port [lindex $argv 1] set timeout 5 spawn telnet $ip $port expect "'^]'." 

tcpdumpwireshark ,看看哪些命令发送到服务器本身

尝试这个

 printf (printf "$username\r\n$password\r\nwhoami\r\nexit\r\n") | ncat $target 23 

某些服务器需要延迟密码,因为它不包含堆栈中的行

 printf (printf "$username\r\n";sleep 1;printf "$password\r\nwhoami\r\nexit\r\n") | ncat $target 23**