使用 bash 脚本自动化 telnet 会话 [英] Automating telnet session using bash scripts

查看:41
本文介绍了使用 bash 脚本自动化 telnet 会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Bash 脚本自动化一些与 telnet 相关的任务.一旦自动化,用户将不会与 telnet 交互(也就是说,它将完全自动化)

I am working on automating some telnet related tasks, using Bash scripts. Once automated there will be no interaction of the user with telnet (that is it will be totally automated)

脚本看起来像这样:

# 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.

我在这里面临两个问题:

There are 2 problems I am facing here:

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

  1. How to execute the commands on the remote system from the script (without human interaction)?

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

From my experience with some test codes, I was able to deduce that when the telnet 10.1.1.1 is executed, telnet goes into an interactive session and the subsequent lines of code in the script are executed on the local system. How can I run the lines of code on the remote system rather than the local one?

我无法在本地系统上获取 telnet 会话中活动的日志文件.我使用的 stdout 重定向在远程系统上进行了复制(我不想执行复制操作将日志复制到本地系统).我怎样才能实现这个功能?

I am unable to get a log file for the activity in the telnet session on the local system. The stdout redirect I used makes a copy on the remote system (I do not want to perform a copy operation to copy the log to the local system). How can I achieve this functionality?

推荐答案

写一个 expect 脚本.

Write an expect script.

这是一个例子:

#!/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

这篇关于使用 bash 脚本自动化 telnet 会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆