參考來源:http://inguza.com/document/expect-and-tcl-mini-reference-manual
先裝好epxect後 就可以用下列語法透過ssh去控制遠端主機或設備
--------------
#!/usr/bin/expect -f
set HOST "目標IP"
set USER "帳號"
set PASS "密碼"
set SERVER [lindex $argv 0] // 參數一 字串
set TARGET [lindex $argv 1] // 參數二 以空格分隔得字串
append SERVER "_WHITE_LIST" // 在參數一後面加字串
spawn ssh -o StrictHostKeyChecking=no $USER@$HOST // ssh連線到目標主機
expect "password:" { send "$PASS\r";} // 密碼
expect " #" { send "config firewall addrgrp \r"; } // 指令
expect "(addrgrp) #" { send "edit $SERVER \r"; } // 指令
expect "($SERVER) #" {
foreach target [split $TARGET " "] { // foreach 迴圈 變數來源是用以空格分隔得字串
send "unselect member [lindex $target 0] \r"; // 指令
continue;
};
}
expect "($SERVER) #" { send "end \r"; } // 指令
expect " #" { send "config firewall address \r"; }
expect "(address) #" {
foreach target [split $TARGET " "] {
send "delete [lindex $target 0] \r";
continue;
};
}
expect "(address) #" { send "end \r"; }
expect " #" { send "exit \r"; } // 登出主機
----------------
沒有留言:
張貼留言