使用 Bash 脚本自动化 telnet 会话
- 2024-10-18 09:00:00
- admin 原创
- 182
问题描述:
我正在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
我在这里面临两个问题:
如何从脚本执行远程系统上的命令(无需人工交互)?
根据我对一些测试代码的经验,我能够推断出,telnet 10.1.1.1
执行时telnet
会进入交互式会话,并且脚本中的后续代码行将在本地系统上执行。我如何在远程系统而不是本地系统上运行这些代码行?
我无法
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)"
使用命令“screen -ls”确保后台没有运行屏幕。
阅读
http://www.gnu.org/software/screen/manual/screen.html#Stuff了解有关屏幕及其选项的更多信息。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:
尝试一下tcpdump
或wireshark
看看哪些命令被发送到服务器本身
尝试一下
printf (printf "$username
$password
whoami
exit
") | ncat $target 23
有些服务器需要延迟输入密码,因为密码不会在堆栈上保留行
printf (printf "$username
";sleep 1;printf "$password
whoami
exit
") | ncat $target 23**
- 2024年20款好用的项目管理软件推荐,项目管理提效的20个工具和技巧
- 2024年开源项目管理软件有哪些?推荐5款好用的项目管理工具
- 2024年常用的项目管理软件有哪些?推荐这10款国内外好用的项目管理工具
- 项目管理软件有哪些?推荐7款超好用的项目管理工具
- 项目管理软件有哪些最好用?推荐6款好用的项目管理工具
- 项目管理软件哪个最好用?盘点推荐5款好用的项目管理工具
- 项目管理软件排行榜:2024年项目经理必备5款开源项目管理软件汇总
- 项目管理软件有哪些,盘点推荐国内外超好用的7款项目管理工具
- 项目管理必备:盘点2024年13款好用的项目管理软件
- 2024项目管理软件排行榜(10类常用的项目管理工具全推荐)