我是 Expect 和一般脚本的新手。我正在尝试制作一些脚本,以便在拉取网络设备配置时让我的生活更轻松。我设法创建了一个基本的 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 valid password to the device
send "password\n"
#
# If the device automatically assigns us to a privileged 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
我需要将它与 Bash 脚本集成吗?如果是这样,是否可以读取文件的一行list.txt
,将其用作 IP 地址/主机变量,然后读取下一行并重复?