我有一个包含两列格式为“IP 地址;主机名”的 CSV。我有这段脚本可以打开一个主机文件并登录到交换机/路由器并将配置备份到 tftp 服务器。
现在我需要调整脚本来打开 csv,这样我就可以在交换机上设置主机名。我唯一需要知道的是如何打开 CSV 并从每行中获取两个变量,即 IP 地址和主机名。其余的都没有问题。
#! /bin/expect
set timeout 20
set hostnamefile [lindex $argv 0]
set tftpip [lindex $argv 1]
set user [lindex $argv 2]
set password [lindex $argv 3]
set prompt "*#"
set send_slow {10 .001}
log_user 0
if {[llength $argv] == 0} {
send_user "Usage: scriptname hostnamefile \'TFTPserver IP\' username \'userpassword\'\n"
exit 1
}
;# -- main activity
proc dostuff { currenthost {tftpserver 1} } {
;# do something with currenthost
send_user "\n#####\n# $currenthost\n#####\n"
send -- "copy running-config tftp:\r"
expect "Address or name of remote host []?"
send "$tftpserver\r"
expect "Destination filename*"
send "\r"
expect {
"*bytes copied*" {
send_user "TFTP copy succeeded\n"
}
"*Error*"{
send_user "TFTP copy failed\n"
}
}
send "\r"
return
}
;# -- start of task
set fd [open ./$hostnamefile r]
set hosts [read -nonewline $fd]
close $fd
foreach host [split $hosts "\n" ] {
spawn /usr/bin/ssh $user@$host
while (1) {
expect {
"no)? " {
send -- "yes\r"
}
"*assword*:" {
send -- "$password\r"
}
"*>" {
send "enable\r"
}
"$prompt" {
dostuff $host $tftpip
break
}
}
}
expect "$prompt"
send -- "exit\r"
}
expect eof