我试图创建一个期望脚本,该脚本登录到cisco主机列表并执行包含与主机匹配的IP地址变量的命令。
我有一个文件(host_list),其主机名和IP地址格式如下:
host1.someplace.com 192.168.0.1
host2.otherplace.com 192.168.0.2
host3.thisplace.com 192.168.0.3
etc
.sh脚本如下所示:
#!/bin/bash
# Collect the current user's ssh password file to copy.
echo -n "Enter the SSH password for $(whoami) : "
read -s -e password
echo -ne '\n'
echo -n "Enter the command : "
read -e command
echo -ne '\n'
for device in `cat host_list | awk '{print $1}'`; do
./configure-cisco.exp $device $password "$command" ;
done
.exp脚本:
#!/usr/bin/expect -f
# Set variables
set hostname [lindex $argv 0]
set username $env(USER)
set password [lindex $argv 1]
set command [lindex $argv 2]
set timeout 1
# Log results
log_file -a ~/log/results.log
# Announce which device we are working on and at what time
send_user "\n"
send_user ">>>>> Working on $hostname @ [exec date] <<<<<\n"
send_user "\n"
spawn ssh $hostname
expect "password:"
send "$password\n"
expect "*$"
send "$command\n"
expect "*$"
exit
我需要的是将ip地址与主机匹配,并将其放在一个变量中,以便与send "$command\n"
一起使用。
类似于:send "$command $ipaddress\n"
$command
随着时间的推移会有所不同,但在现场情况下会是如此。
event manager environment PRIMARY_BGP_NEIGHBOR
$ipaddress
必须与脚本所连接的主机匹配。
我使用ssh对不同的linux主机进行测试,然后我将脚本执行到实时cisco环境,我将使用telnet进行连接,但功能应该相同。
答案 0 :(得分:0)
对于.sh:
names=()
ips=()
n=0
while read name ip; do
[[ -z $ip ]] && continue
names[n]=$name
ips[n]=$ip
((++n))
done < hostfile
for ((i=0; i<n; ++i)); do
script.exp ${names[i]} ${ips[i]} $device "$passwd"
done