「IP アドレス; ホスト名」という形式の 2 つの列を持つ CSV があります。ホストファイルを開いてスイッチ/ルーターにログインし、構成をtftpサーバーにバックアップするためのこのスクリプトがあります。
スイッチにホスト名を設定できるように、csv を開くスクリプトを調整する必要があります。知っておく必要があるのは、CSV を開いて各行から IP アドレスとホスト名の 2 つの変数を取得する方法だけです。残りは問題ありません。
#! /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