Brief introduction:
Expect can perform interactive operation with system for system administrator
Yum Install Yum which expect # path /usr/bin/expect
Example one:
Shell >Cat Login. exp#!/usr/bin/ExpectspawnSSH[Email protected]192.168.1.81; expect {"(yes/no)?"{Send"yes\r"; Exp_continue}"Password:"{Send"123456\r"; Exp_continue}"#"{Send"uptime\r"; interact}}
# # Use/usr/bin/expect to explain the script, spawn open an SSH process, expect to receive information, send for sending information
"(yes/no)?" When the first login to the remote host will prompt yes/no, matching to the message send "Yes\r", \ r for line break, exp_continue to continue to match the next bar
"#" Because it is the root user login, then match to the correct password will get the Super user's flag #, then you can send the system command uptime
Interact directive means: to maintain the interactive state, the control to the console, that is, just log on to the remote host, and now will not exit, and the administrator can operate the host
Example two:
Shell >Cat Login. exp#!/usr/bin/expectset Timeout-1; SpawnSSH[Email protected]192.168.1.81; expect {"(yes/no)?"{Send"yes\r"; Exp_continue}"Password:"{Send"123456\r"; Exp_continue}"#"{Send"uptime\rexit\r"; expect EOF}}
# # Set Timeout-1: Set timeout time, 1 is infinity, can be specified for example 20 (in seconds)
Expect EOF: waits for the process to end (does not maintain an interactive state), expect EOF waits for the time-out to expire when the execution command does not explicitly specify an exit login (exit\r)
Exit the remote system (timeout is set to-1, does not exit), if explicitly specified exit\r (that is, the above example), then execute the command immediately exit the remote login system
Example three:
Shell >Cat Login.SH#!/bin/bashlist=("192.168.1.81 Root 123456" "192.168.1.82 Root 123456" "192.168.1.83 Root 123456") Length=${#list [@]} for((i=0;i< $length; i++) ) {host=`Echo${list[$i]} |awk '{print $}'' User=`Echo${list[$i]} |awk '{print $}'`passwd=`Echo${list[$i]} |awk '{print $}'' expect-C"SpawnSSH[email protected] $host; expect {\"(yes/no) \ "{Send \" yes\r\ "; exp_continue}\"password:\ " {Send \ "$passwd \r\"; exp_continue}\"#\ "{send \" uptime\rexit\r\ "; expect EOF}}"}
# # Expect-c execution of expect scripts from the command line can now be implemented by looping the server to execute commands and exit the remote system
Exit\r removed, and expect EOF to interact can be implemented to stay on the remote server (only one server, such as too many stations, the first stop will not cycle)
Example four:
Shell >Cat Login. exp1#!/usr/bin/expectif{$ARGC! =3} {puts"usage $argv 0 User host passwd"Exit1}set User [lindex $argv0]set host [lindex $argv1]setpasswd[Lindex $argv2]spawnSSH[email protected] $host; expect {"(yes/no)?"{Send"yes\r"; Exp_continue}"Password:"{Send"$passwd \ r"; Exp_continue}"#"{Send"uptime\r"; interact}}
# # $ARGC represents the number of parameters, $ARGV 0 for the script name, $ARGV 0 for the first parameter ...
If the pass parameter is less than 3, the screen displays the usage script name User host passwd, so the script should be executed as follows:./LOGIN.EXP1 Root 192.168.1.81 123456
Exit script if less than 3 parameters
Set assigns the first parameter to the variable user ... (Variable storage is calculated from 0 1 2 3 ... n)
Expect Small Script