Expect is a software tool for automated control and testing in UNIX systems, made by Don Libes, as an extension of the TCL scripting language, applied in interactive software such as TELNET,FTP,PASSWD,FSCK,RLOGIN,TIP,SSH, etc.
Yum Install-y expect
Script one:
Automatic Telnet
#! /usr/bin/expectset host "192.168.226..130"set passwd "123456"spawn ssh [email protected]$hostexpect {"yes/no" { send "yes\r"; exp_continue}"assword:" { send "$passwd\r" }}interact
Run the script
chmod a+x 1.expect
./1.expect
Note, when the initial SSH login, will be prompted to enter Yes or no, so you need to send a yes, and then enter. Enter the password for the account immediately thereafter. If you do a key authentication, you do not need to enter a password. Interact means to keep a login status. If not added, it will be returned immediately.
Script two:
After automatic telnet, execute the command and exit
#!/usr/bin/expectset user "root"set passwd "123456"spawn ssh [email protected]expect {"yes/no" { send "yes\r"; exp_continue}"password:" { send "$passwd\r" }}expect "]*"send "touch /tmp/12.txt\r"expect "]*"send "echo 1212 > /tmp/12.txt\r"expect "]*"send "exit\r"
Note:] indicates that the system is logged in, enter the status prompt in front of the cursor, the root user for]# ordinary user is]$, is a wildcard character. The above script indicates a login to the system and executes two commands under the current user directory.
Script Three:
Parameter passing. Equivalent to the $1,$2,$3 in the shell
#!/usr/bin/expectset user [lindex $argv 0]set host [lindex $argv 1]set passwd "123456"set cm [lindex $argv 2]spawn ssh [email protected]$hostexpect {"yes/no" { send "yes\r"}"password:" { send "$passwd\r" }}expect "]*"send "$cm\r"expect "]*"send "exit\r"
Note: This script defines three parameters that need to be passed, the first is the user name to log on to the machine, the second is the host IP, and the third is the command to be sent after the login system. So when you run a script, you need to add these three parameters.
chmod a+x 3.expect
./expect root 192.168.226.130 ls
If you need to send more than one command at a time, you can write "Ls;w;ifconfig" or define several parameters in the script.
Script four:
Synchronizing files for rsync applications
#!/usr/bin/expectset passwd "123456"spawn rsync -av [email protected]:/tmp/12.txt /tmp/expect {"yes/no" { send "yes\r"}"password:" { send "$passwd\r" }}expect eof
Note: This script is equivalent to directly perform a remote synchronization of the rsync command, the difference is embedded in the login password, so you do not need to interact with the user directly to complete the file synchronization. The role of expect EOF is to keep the remote connection in a state, without adding a direct disconnect when the synchronization is not complete.
Script Five:
Specify host and files to synchronize
#!/usr/bin/expectset passwd "123456"set host [lindex $argv 0]set file [lindex $argv 1]spawn rsync -av $file [email protected]$host:$fileexpect {"yes/no" { send "yes\r"}"password:" { send "$passwd\r" }}expect eof
Note: The function of this script is to synchronize the local files to the remote host, define two parameters, the first is the transport host IP, the second is the file name to synchronize. The file must exist locally, and the file name parameter will use an absolute path when executing the script.
chmod a+x 5.expect
./5.expect 192.168.226.130 "/tmp/12.txt"
Linux Learning Summary (63) Expect script