expect script

來源:互聯網
上載者:User

Expect script introduction(from wiki): 

 Expect is a Unix automation and testing tool, written by Don Libes as an extension to the Tcl scripting language, for interactive applications such as telnet, ftp, passwd, fsck,rlogin, tip, ssh, and others. It uses Unix pseudo terminals to wrap up subprocesses transparently, allowing the automation of arbitrary applications that are accessed over a terminal. With Tk, interactive applications can be wrapped in X11 GUIs.


It is difficult to script the ssh and scp, passwd command into the shell script because of these cant get input from redirect file or something else,  while the expect is excellent to do it.

The below expect shell to explain how to use expect script:

#!/usr/bin/expect 

set server   [lindex $argv 0]

set account  [lindex $argv 1]

set password [lindex $argv 2]

set destination [lindex $argv 3]

set build [lindex $argv 4]

if { $server == "-h" ||  $server == "" } {

   spawn echo "paramter order : server : account : password : destinatioin : build"

   exit

}

spawn echo "server      : $server"

spawn echo "account     : $account"

spawn echo "password    : $password"

spawn echo "destination : $destination"

spawn echo "build       : $build"

spawn echo "start to run the deployment======================================================>"

spawn ssh $account@$server "mkdir -p worker/$destination; echo 'create dictinary'"

expect {*password}

send  "raas\r"

spawn scp -r $build $account@$server:worker/$destination

expect {*password:}

send  "raas\r"

expect eof

spawn ssh $account@$server "echo 'run2'; cd worker/$destination; sh pyADPWorker.sh"

expect {*password}

send  "raas\r"

expect eof 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.