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)
我在这面面有两个问题:
如何从脚本执行远程系统上的命令(没有人工交互)?
根据我对一些测试代码的经验,我能够推断出当执行telnet 10.1.1.1时,telnet进入交互式会话,脚本中的后续代码行在本地系统上执行.如何在远程系统而不是本地代码上运行代码行?
我无法在本地系统上的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.
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)
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)