用于SSH的Bash/Expect脚本

min*_*rix 7 ssh bash expect

我是新手,期待和编写脚本.我正在尝试制作一些脚本,以便在拉动网络设备配置时让我的生活更轻松一些.我设法创建一个基本的期望脚本来SSH到设备并保存配置.

我想扩展这个并允许脚本连接到许多IP地址,而不是像我现在一样.我有一个名为list.txt几个不同IP地址的文件,每个IP在一个单独的行上.

我需要做什么才能让expect脚本连接到每个IP地址并执行脚本中的其余任务?

这是我到目前为止的Expect脚本:

#!/usr/bin/expect -f
#Tells interpreter where the expect program is located.  This may need adjusting according to
#your specific environment.  Type ' which expect ' (without quotes) at a command prompt
#to find where it is located on your system and adjust the following line accordingly.
#
#
#Use the built in telnet program to connect to an IP and port number
spawn ssh 192.168.1.4 -l admin
#
#The first thing we should see is a User Name prompt
#expect "login as:"
#
#Send a valid username to the device
#send "admin"
#
#The next thing we should see is a Password prompt
expect "Password:"
#
#Send a vaild password to the device
send "password\n"
#
#If the device automatically assigns us to a priviledged level after successful logon,
#then we should be at an enable prompt
expect "Last login:"
#
#Tell the device to turn off paging
#
#After each command issued at the enable prompt, we expect the enable prompt again to tell us the
#command has executed and is ready for another command
expect "admin@"
#
#Turn off the paging
send "set cli pager off\n"
#
#Show us the running configuration on the screen
send "show config running\n"
#
# Set the date.
set date [timestamp -format %C%y%m%d]
#
#Test output sent to file with a timestamp on end
#-noappend will create a new file if one already exists
log_file -noappend /home/test.cfg$date
#
expect "admin@"
#
#Exit out of the network device
send "exit\n"
# 
#The interact command is part of the expect script, which tells the script to hand off control to the user.
#This will allow you to continue to stay in the device for issuing future commands, instead of just closing
#the session after finishing running all the commands.`enter code here`
interact
Run Code Online (Sandbox Code Playgroud)

我是否需要将其与Bash脚本集成?如果是这样,是否可以读取list.txt文件的一行,将其用作IP /主机变量,然后读取下一行并重复?

gle*_*man 3

我会这样做(未经测试):

#!/usr/bin/expect -f

set logfile "/home/text.cfg[clock format [clock seconds] -format %Y%m%d]"
close [open $logfile w]         ;# truncate the logfile if it exists

set ip_file "list.txt"
set fid [open $ip_file r]

while {[gets $fid ip] != -1} {

    spawn ssh $ip -l admin
    expect "Password:"
    send "password\r"

    expect "admin@"
    send "set cli pager off\r"

    log_file $logfile
    send "show config running\r"

    expect "admin@"
    log_file 

    send "exit\r"
    expect eof

}
close $fid
Run Code Online (Sandbox Code Playgroud)

笔记:

  • 为了简洁起见,我删除了您的所有评论
  • 用于\r模拟在send命令时按 Enter 键。
  • 我假设您只想记录“显示配置运行”输出
  • expect eof发送“exit”后使用