liunx之expect操作詳解( 二 )


通過expect嵌套shell使用語句在shell內直接執行,任何這樣可以實現更多的功能
#!/bin/bashuser="mrswhite"host="192.168.37.9"password="test20221007"/usr/bin/expect << EOFset time 20spawn ssh $user@$hostexpect {"*yes/no" { send "yes\r"; exp_continue }"*password:" { send "$password\r" }}expect "*#"send "pwd\r"expect "*#"send "df -h\r"expect "*#"send "exit\r"interactexpect eofEOF

liunx之expect操作詳解

文章插圖
 五、expect相關錯誤處理
1.invalid command name "/usr/bin/expect"
liunx之expect操作詳解

文章插圖
解決方案:此時是使用bash腳本嵌套了expect代碼,所以執行采用以下兩種方式都可以
./expect_demo3.shsh expect_demo3.sh
liunx之expect操作詳解

文章插圖
2.invalid command name ":" 轉義問題
liunx之expect操作詳解

文章插圖
解決方案:send里面的內容中的括號[]有問題,不能使用[],將其去除或者添加轉義字符
send "cat 20221007.txt | awk -F : '{print $2}'"send "cat 20221007.txt | awk -F \[:\] '{print $2}'\r"
liunx之expect操作詳解

文章插圖

推薦閱讀