圖文教程自動登入expect指令碼執行個體

來源:互聯網
上載者:User

標籤:expect 自動登入

簡介:

1.expect的講解

2.執行個體操作自動登入指令碼


expect講解


expect可以讓我們實現自動登入遠程機器,並且可以實現自動遠程執行命令。當然若是使用不帶密碼的密鑰驗證同樣可以實現自動登入和自動遠程執行命令。但當不能使用密鑰驗證的時候,我們就沒有辦法了。所以,這時候只要知道對方機器的帳號和密碼就可以通過expect指令碼實現登入和遠程命令。


Expect中最關鍵的四個命令是send,expect,spawn,interact。

send:用於向進程發送字串
expect:從進程接收字串
spawn:啟動新的進程
interact:允許使用者互動


1. send命令


send命令接收一個字串參數,並將該參數發送到進程。


expect1.1> send "hello world\n"hello world


2. expect命令


expect命令和send命令正好相反,expect通常是用來等待一個進程的反饋。expect可以接收一個字串參數,也可以接收Regex參數。


expect "hi\n"send "hello there!\n"


這兩行代碼的意思是:從標準輸入中等到hi和換行鍵後,向標準輸出輸出hello there



3. spawn命令


上文的所有demo都是和標準輸入輸出進行互動,但是我們跟希望他可以和某一個進程進行互動。spawm命令就是用來啟動新的進程的。


set timeout -1spawn ftp ftp.test.com      //開啟新的進程,該進程使用者串連遠程ftp伺服器

expect "Name"             //進程返回Name時

send "user\r"        //向進程輸入

anonymous\rexpect "Password:"        //進程返回Password:時

send "123456\r"    //向進程輸入[email protected]\r

expect "ftp> "            //進程返回ftp>時

send "binary\r"           //向進程輸入binary\r

expect "ftp> "            //進程返回ftp>時

send "get test.tar.gz\r"  //向進程輸入get test.tar.gz\r



4.interact

到現在為止,我們已經可以結合spawn、expect、send自動化的完成很多任務了。但是,如何讓人在適當的時候幹預這個過程了。比如下載完ftp檔案時,仍然可以停留在ftp命令列狀態,以便手動的執行後續命令。interact可以達到這些目的。下面的demo在自動登入ftp後,允許使用者互動。

spawn ftp ftp.test.com

expect "Name"

send "user\r"

expect "Password:"

send "123456\r"

interact




2台主機


服務端:back :192.168.19.134

用戶端:other:192.168.19.135




服務端操作


1.使用expect之前,需要先安裝expect:


yum install -y expect 


安裝ssh


yum install openssh-server -y 


yum -y install openssh-clients

2.建立一個自動登入指令檔



格式:


       spawn ssh -l username 192.168.1.1


expect{

      "(yes/no)?" { send "yes"; exp_continue}

      "password:" {send "AAAA"}

         }

 interact



vim login.expect

#! /usr/bin/expect

#about login

#written by lizheng

set host "192.168.19.135"

set passwd "lizheng123"

spawn ssh  [email protected]$host

expect {

    "yes/no" { send "yes\r"; exp_continue}

    "password:" { send "$passwd\r" }

   }

interact


650) this.width=650;" src="http://s5.51cto.com/wyfs02/M00/8C/23/wKioL1hjdA2A6OHOAACZD-_uh4A256.png-wh_500x0-wm_3-wmp_4-s_2171822207.png" title="1.png" alt="wKioL1hjdA2A6OHOAACZD-_uh4A256.png-wh_50" />

650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />



3.更改許可權


chmod +x login.expect


650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />650) this.width=650;" src="http://s1.51cto.com/wyfs02/M02/8C/23/wKioL1hjdBXw_-f4AAAH4pcCHdg161.png-wh_500x0-wm_3-wmp_4-s_526202758.png" title="2.png" alt="wKioL1hjdBXw_-f4AAAH4pcCHdg161.png-wh_50" />


4.執行指令碼語句


./login.expect


輸入客戶機的密碼即可登入


650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/8C/23/wKioL1hjdB6wpDhNAAAaLY_Y8Tk576.png-wh_500x0-wm_3-wmp_4-s_1898479088.png" title="3.png" alt="wKioL1hjdB6wpDhNAAAaLY_Y8Tk576.png-wh_50" />


5.退出


logout


圖文教程自動登入expect指令碼執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.