2

「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
4

2 に答える 2

1

トリックを実行するexpectスクリプトを中心に、なんとかbashスクリプトを作成しました。誰かがより良い方法を持っているなら、私に知らせてください。

#!/bin/bash
input=./list.csv
oldifs=$ifs
ifs=,
[ ! -f $input ] && { echo "$input file not found"; exit 99; }

if [ $# -ne 2 ]
then
    echo "Usage: scriptname username password"
    exit 1
fi

username=$1
password=$2

while read line
do
eval $(echo "$line" | awk -F';' '{print "ip="$1";hostname="$2}')
#echo $ip
#echo $hostname

/usr/bin/expect - << EndMark
set prompt "*#"
set cprompt "*(config)#"

spawn /usr/bin/ssh $username@$ip

while (1) {
   expect {
       "no)? " {
          send -- "yes\r"
       }
       "*assword*:" {
          send -- "$password\r"
       }
       "*>" {
          send "enable\r"
       }
       "$prompt" {
          break
       }
   }
}
sleep 2

send -- "conf t\r"
expect "$cprompt"
send -- "hostname $hostname\r"
expect "$cprompt"
send -- "end\r"
expect "$prompt"
send -- "copy running-config startup-config\r"
expect "Destination filename*"
send -- "\r"
expect "$prompt"
send -- "exit\r"

send_user "\n#####\n# hostname $hostname\n# set on $ip\n#####\n"
send -- "exit\r"

expect eof

EndMark

done < $input
ifs=$oldifs
于 2012-12-19T09:24:25.390 に答える