1. 程式人生 > >Linux學習之自動配置部署——初用expect

Linux學習之自動配置部署——初用expect

pro 出現 expec 用戶 部署 inux 臨時文件 spa 內容

主機A連接主機B
      免密登陸 + 自動部署

expect實現自動的交互式任務

    ——— send
        向進程發送字符串(輸入)
    ——— expect
        從進程接受字符串
    ——— spawn
        啟動新進程
    ——— interact
        允許用戶交互

SSH 實現自動登陸,並停留在登錄服務器上

shell腳本


#!/usr/bin/expect -f

set ip [lindex $argv 0 ] //接收第一個參數,並設置IP
set password [lindex $argv 1 ] //接收第二個參數,並設置密碼
set timeout 10 //設置超時時間


spawn ssh [email protected]$ip //發送ssh請滶
expect { //返回信息匹配
"*yes/no" { send "yes\r"; exp_continue}    //第一次ssh連接會提示yes/no,回車,繼續
"*password:" { send "$password\r" }     //出現密碼提示,發送密碼

}
interact                 //交互模式,用戶會停留在遠程服務器上面.



————創建臨時文件

cat >> /etc/profile << EOF\
export ...

EOF


—————————————
創建臨時文件,內容是exprot...
>> 重定向追加到目錄文件中
<< 將EOF後的內容重定向追加到文件中



Linux學習之自動配置部署——初用expect