The expect command for the Linux script uses

Source: Internet
Author: User

1 overview

Expect, developed by Don Libes based on the TCL (Tool Command Language) language, is mainly used for automating interactive operation scenarios, using expect to process interactive commands, such as SSH logins, FTP logins, and so on in a script, Automate the completion of it. This is especially useful in environments where you need to perform the same operations on multiple servers, which can greatly improve the productivity of your system administrators.

2 syntax

Expect command

Expect syntax:

Expect [options] [-C CMDS] [[-[f|b]] cmdfile] [args]

Options

-C: Executes the expect script from the command line, and the default expect is executed interactively

Example: expect-c ' expect "\ n" {send "pressed enter\n"}

-D: Output debug information can be exported

Example: expect-d ssh.exp

Related commands in expect

Spawn: Start a new process

Send: For sending a string to a process

Expect: Receiving a string from a process

Interact: Allow user interaction

Exp_continue match multiple strings after executing the action add this command

Expect most commonly used syntax (TCL language: mode-action)

Single branch mode syntax:

Expect "HI" {send "you said Hi\n"}

After matching to Hi, it will output "you said hi" and wrap

Multi-branch mode syntax:

Expect "HI" {send "you said hi\n"} \

"hehe" {send "hehe yourself\n"} \

"Bye" {send "good bye\n"}

Executes the corresponding output when matching hi,hello,bye arbitrary strings. Equivalent to the following:

Expect {

"HI" {send "you said Hi\n"}

"hehe" {send "hehe yourself\n"}

"Bye" {send "good bye\n"}

}

3 scripts

Here are a few common uses of expect in shell scripts, automatic copy scripts, scripts, SSH and telnet to connect to remote hosts.

#!/bin/bash##****************************************************************************** #Author:                Sunny#Date:                  2017-09-03#filename:              scp.sh#version:               1.0#Your change info:       #Description:           for#copyright (C):          2017  all rihts reserved#******* echo  "1 copy wang  Home dir,usage:$0 "echo " 2 copy file under wang home,usage: $0  File_to_copy "echo " 3&nbsP;send file to wang home,usage: $0 file_to_send "echo " 4 login  Other host by ssh,usage: $0 login_ip "echo " 5 login other host  by telnet,usage: $0 login_ip login_user "read -p " input the  remote ip:  " ipread -p " Input full path file_name or dir:   " fileread -p " input host password:  " passwdread -p "  input your choice:  " choicecase  $choice  in1)     expect  -c  "    spawn scp -r [email protected" $ip: $file   $file \_$ (date +%f-%h-%m) "    expect {           # \ "*assword\"  {set timeout 300; send \ "$passwd \r\"; }      &nbsP;     \ "*assword\"  {set timeout 300; send \ "$passwd \r\";  }           \ "yes/no\"  { send \ " Yes\r\ ";  exp_continue; }    }    expect eof"      ;;   2)     expect -c  "   spawn scp  [email  protected] $ip: $file   /root/    expect {           # \ "*assword\"  {set timeout 500; send \ "$passwd \r\ ";  }           \" *assword\ " {set timeout  500; send \ "$passwd \r\"; }            \ "yes/no\"  { send \ "Yes\r\"; exp_continue; }    }   &nBsp; expect eof "    ;;   3)     expect -c  "   spawn  scp  $ File  [email protected] $ip:/root   expect {           # \ "*assword\"  {set timeout 500; send \ "$passwd \r\ ";  }           \" *assword\ " {set timeout  500; send \ "$passwd \r\"; }            \ "yes/no\"  { send \ "Yes\r\"; exp_continue; }    }         expect eof "    ;; 4)    expect -c      spawn  ssh  $ip      expect {           \ "Yes/no\"  { send \ "yes\r\";  exp_continue; }    \ "*assword\"  {set  Timeout 500; send \ "$passwd \r\"; }    }        interact     expect eof "     ;; 5)       expect -c  "    spawn  telnet   $IP     expect {    \ "*assword\"  {set timeout  500; send \ "$passwd \r\";  }    \ "login\"  { send \ "Sunny \r\ ";exp_continue; }    }      interact      expect eof "     ;; *)     echo  "Your input is wrong,please check"      exit    ;; Esac

4 Summary

Expect can be actually silent operation, which is often used in the script, the reader according to the corresponding function, copy the relevant code, you can directly change the variable to a fixed value, do not need to run the script every time you need to manually input variable values, to achieve silent operation.

This article is from the "Self-learning Linux" blog, so be sure to keep this source http://ghbsunny.blog.51cto.com/7759574/1963007

The expect command for the Linux script uses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.