概述
- 使用shell腳本加expect 實現批量登錄,互信等,免交互式操作,之前主要寫兩個腳本,一個是expect腳本里面主要是接受參數,進行ssh 免交互操作,另一個是shell腳本,裏面調用expect的腳本,傳入參數,
- 現在需要把expect和shell腳本內容全部合併在shell腳本中 可以用shell function 裏面定義expect
例子:
- shell 腳本中定義function, 然後把<< EOF 所有內容,當作expect 標準輸入
#!/bin/bash
function expect_bash(){
/usr/bin/expect << EOF
spawn ssh -o StrictHostKeyChecking=no root@${i} "hostname"
expect {
"*password:" {exp_send "YNETserver1\r"}
}
expect eof
EOF
}
for i in `cat ip`
do
expect_bash
done