expect在執行命令返回結果中,有時候只返回一些結果,並卡住
在bash中調用expect登錄遠程主機執行命令。發現expect在執行命令find時,有時候返回正常結果,有時候只返回一些結果,並卡住,然後就不退出。
登錄的是同一個遠程主機。同一時間執行腳本10次,基本上有5次是卡住的。
將 |xargs -I {} last -f {} 改為 -exec last -f \; 也是一樣的情況。請指教下原因。謝謝。
expect <<EOF
set timeout -1
log_file ${tmpdir}/${atype}_tmp.txt
spawn ssh ${user}@${ip}
expect {
"yes/no" { send "yes\r";exp_continue}
"assword" { send "${pw}\r"}
}
expect "#"
send "touch -t ${last_time} last_time.txt \r"
expect "#"
send "find /var/log -type f -name \"wtmp*\" -newer \"last_time.txt\" |xargs -I {} last -f {} \r"
expect "#"
send " rm -f last_time.txt \r"
expect "#"
send "exit\r"
expect eof
EOF
《解決方案》
expect次數太多了,為什麼不把腳本放到遠程主機呢?
用spawn ssh 執行遠程主機shell腳本,
這樣只需expect一次就行了。
expect {
"yes/no" {
send "yes\n"
expect "password:"
send "$passwd\n"
}
"password:" {send "$passwd\n"}
}
《解決方案》
另外 linux下 回車換行 是 \n.
《解決方案》
set timeout -1
《解決方案》
回復 2# salmon5
因為涉及的主機較多,不方便都上傳到遠程主機上操作,只適宜發送指令到遠程主機。另外,r和n 回車換行測試還是不行。
《解決方案》
試試使用SaltStack吧
《解決方案》
interpreter 或者 exp_internal 1看下。
expect -re
《解決方案》
interpreter 或者 exp_internal 1看下。
expect -re