使用 Bash 脚本自动化 telnet 会话

2024-10-18 09:00:00
admin
原创
181
摘要:问题描述:我正在telnet使用 Bash 脚本自动执行一些相关任务。一旦实现自动化,就无需用户进行交互telnet(即脚本将完全自动化)。该脚本看起来像这样:# execute some commands on the local system # access a remote system with a...

问题描述:

我正在telnet使用 Bash 脚本自动执行一些相关任务。

一旦实现自动化,就无需用户进行交互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 with executing the rest of the script

我在这里面临两个问题:

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

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

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


解决方案 1:

虽然我expect也建议使用,但对于非交互式使用,普通的 shell 命令就足够了。telnet在 stdin 上接受它的命令,因此您只需通过heredoc将命令管道化或写入其中:

telnet 10.1.1.1 <<EOF
remotecommand 1
remotecommand 2
EOF

(编辑:从评论来看,远程命令需要一些时间来处理输入,或者早期的SIGHUP不能被正常接收telnet。在这些情况下,您可以尝试在输入时短暂休眠:)

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

无论如何,如果它变得具有交互性或其他什么的,请使用expect

解决方案 2:

编写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

运行:

./myscript.expect name user password

解决方案 3:

学习 HTTP 协议时,经常会用到 Telnet。我曾经将该脚本用作我的网页抓取工具的一部分:

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,那么这将为您提供一个 HTML 文档:

get-page.sh | telnet

我希望这对某些人有帮助;)

解决方案 4:

这对我有用。

我试图自动执行需要用户名和密码的多个 telnet 登录。由于我要将来自不同服务器的日志保存到我的机器上,因此 telnet 会话需要在后台无限期运行。

telnet.sh 使用“expect”命令自动执行 telnet 登录。更多信息可在此处找到:http ://osix.net/modules/article/?id=30

远程登录

#!/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
"
expect "Password:"
send "$password
";
interact

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

示例脚本

#!/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”选项发送命令,否则您会收到“未找到屏幕会话”错误。

解决方案 5:

您可以使用 expect 脚本代替 bash。下面的示例展示了如何在没有密码的情况下通过 telnex 进入嵌入式主板

#!/usr/bin/expect

set ip "<ip>"

spawn "/bin/bash"
send "telnet $ip
"
expect "'^]'."
send "
"
expect "#"
sleep 2

send "ls
"
expect "#"

sleep 2
send -- "^]
"
expect "telnet>"
send  "quit
"
expect eof

解决方案 6:

@thiton 的回答很有帮助,但我想避免使用该命令sleep。也telnet没有退出交互模式,所以我的脚本卡住了。

我通过使用 curl 发送 telnet 命令(似乎等待响应)并明确告诉 telnet 退出来解决这个问题,如下所示:

curl telnet://10.1.1.1:23 <<EOF
remotecommand 1
remotecommand 2
quit
EOF

解决方案 7:

以下对我有用...将所有要远程登录的 IP 放入 IP_sheet.txt 中

while true
read a
do
{
    sleep 3
    echo df -kh
    sleep 3
    echo exit
} | telnet $a
done<IP_sheet.txt

解决方案 8:

#!/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
\"
        expect \"Password:\"
        send \"$pass
\"
        expect \"commands.\"
        send \"ping ya.ru -c $ping_count
\"
        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"

解决方案 9:

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

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

解决方案 10:

以下是在 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 "'^]'."

解决方案 11:

获取CISCO服务器版本的脚本:

#!/bin/sh

servers='
192.168.34.1
192.168.34.3
192.168.34.2
192.168.34.3
'
user='cisco_login'
pass='cisco_password'

show_version() {
host=$1
expect << EOF
set timeout 20
set host $host
set user $user
set pass $pass
spawn telnet $host
expect "Username:"
send "$user
"
expect "Password:"
send "$pass
"
expect -re ".*#"
send "show version
"
expect -re ".*-More-.*"
send " "
expect -re ".*#"
send "exit
"
EOF
}

for ip in $servers; do
  echo '---------------------------------------------'
  echo "$ip"
  show_version $ip | grep -A3 'SW Version'
done

解决方案 12:

这是一个适用于扩展器列表的解决方案。这只需要 bash - 上面的一些答案需要 expect,您可能无法指望安装 expect。

#!/bin/bash

declare -a  Extenders=("192.168.1.48" "192.168.1.50" "192.168.1.51") 
# "192.168.1.52" "192.168.1.56" "192.168.1.58" "192.168.1.59" "192.168.1.143")
sleep 5
# Iterate the string array using for loop
for val in ${Extenders[@]}; do
   { sleep 0.2; echo "root"; sleep 0.2; echo "ls"; sleep 0.2; }  | telnet $val
done

解决方案 13:

尝试一下tcpdumpwireshark看看哪些命令被发送到服务器本身

尝试一下

printf (printf "$username
$password
whoami
exit
") | ncat $target 23

有些服务器需要延迟输入密码,因为密码不会在堆栈上保留行

printf (printf "$username
";sleep 1;printf "$password
whoami
exit
") | ncat $target 23**
相关推荐
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1124  
  IPD(Integrated Product Development,集成产品开发)流程是一种广泛应用于高科技和制造业的产品开发方法论。它通过跨职能团队的紧密协作,将产品开发周期缩短,同时提高产品质量和市场成功率。在IPD流程中,CDCP(Concept Decision Checkpoint,概念决策检查点)是一个关...
IPD培训课程   79  
  研发IPD(集成产品开发)流程作为一种系统化的产品开发方法,已经在许多行业中得到广泛应用。它不仅能够提升产品开发的效率和质量,还能够通过优化流程和资源分配,显著提高客户满意度。客户满意度是企业长期成功的关键因素之一,而IPD流程通过其独特的结构和机制,能够确保产品从概念到市场交付的每个环节都围绕客户需求展开。本文将深入...
IPD流程   70  
  IPD(Integrated Product Development,集成产品开发)流程是一种以跨职能团队协作为核心的产品开发方法,旨在通过优化资源分配、提高沟通效率以及减少返工,从而缩短项目周期并提升产品质量。随着企业对产品上市速度的要求越来越高,IPD流程的应用价值愈发凸显。通过整合产品开发过程中的各个环节,IPD...
IPD项目管理咨询   82  
  跨部门沟通是企业运营中不可或缺的一环,尤其在复杂的产品开发过程中,不同部门之间的协作效率直接影响项目的成败。集成产品开发(IPD)作为一种系统化的项目管理方法,旨在通过优化流程和增强团队协作来提升产品开发的效率和质量。然而,跨部门沟通的复杂性往往成为IPD实施中的一大挑战。部门之间的目标差异、信息不对称以及沟通渠道不畅...
IPD是什么意思   74  
热门文章
项目管理软件有哪些?
云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用