1 , expect- regular expression
( 1) Expect to realize non-interactive login
expect is a free programming tool language used to realize automatic and interactive task communication without human intervention. Such as: ssh login, ftp login, etc. are written on a script to make it complete automatically.
( 2) Install expect
[root@test ~]# yum -y install expect
( 3) How to use
A. Define the shell for script execution
[root@test ~]# find / -name expect /usr/bin/expect
#!/usr/bin/expect
Defined is the link path of the expect executable file .
B、set timeout 30
Set the timeout time, the unit is seconds, if set to timeout -1 means never time out, the default timeout is 10s
C、spawn
spawn is an internal command that can only be executed after entering the expect environment. If expect is not installed or executed directly under the default SHELL, the spawn command will not be found. The main function cannot be performed directly in the default shell environment. Its main function is to add a shell to the ssh running process to transmit interactive instructions.
D、expect
The expect internal command is mainly used to determine whether the output result contains a certain string. If it does not, it will return immediately. Otherwise, it will return after a period of time. The waiting time is set by timeout.
E、send
Execute the interactive action, and input the action to be performed in the interaction to the interactive instruction.
"\R" should be added to the end of the command string . If there is an abnormal waiting state, it can be checked.
F、exp_continue
Continue to perform the next interactive operation
G、interact
After execution, keep the interactive state and transfer the control to the console; if you don't add this item, it will automatically exit after the interaction is completed.
H、$argv
The expect script can accept the parameters passed from bash, which can be obtained by using [lindex $argv n]. n starts from 0 and represents the first, second, third...parameters respectively.
( 4) Log in to the server via SSH without a password (non-secret key method)
[root@test shell]# vi ssh.sh "password" {send "$passwd\r"} } #!/usr/bin/expect set ip "192.168.0.20" set name "root" set passwd "123456" set timeout 25 spawn ssh $name@$ip expect { "yes/no" {send "yes\r";exp_continue} "password" {send "$passwd\r"} } expect "#" #Judging the last output result Whether it contains the string of "password:", if there is, return immediately and execute downward; otherwise, it will wait until the timeout period reaches send "touch /root/test.txt\r" send "ls /root> /root/ test.txt\r" send "exit\r" expect eof #After executing the above command, exit Expect, transfer control to the console, and change back to manual operation [root@test shell]# expect ssh. sh spawn ssh [email protected] [email protected]'s password: Last login: Fri Nov 27 00:18:47 2020 from 192.168.0.10 [root@test ~]# touch /root/test.txt [root@test ~]# ls /root > /root/test.txt [root@test ~]# exit 登出 Connection to 192.168.0.20 closed. [root@test shell]#
Note: expect -f ssh.sh will display the error message of unsuccessful script execution.
( 5) Batch SSH login server for management
[root@test shell]# cat ssh.exp #!/usr/bin/expect set ipaddr [lindex $argv 0] set passwd [lindex $argv 1] set timeout 30 spawn ssh root@$ipaddr expect { "yes/no" { send "yes\r";exp_continue } "password" { send "$passwd\r" } } expect "#" send "cd /root\r" send "rm -rf test.txt\r" send "exit\r" expect eof [root@test shell]# Shell脚本传递ip及密码等参数信息: [root@test shell]# cat login.sh #!/bin/bash echo for ip in `awk '{print $1}' /root/shell/ip_list.txt` do pass=`grep $ip /root/shell/ip_list.txt|awk '{print $2}'` expect /root//shell/ssh.exp $ip $pass done [root@test shell]# ip_list.txt文件 [root@test shell]# cat ip_list.txt 192.168.0.20 123456 [root@test shell]# 执行脚本 [root@test shell]# sh login.sh spawn ssh [email protected] [email protected]'s password: Last login: Fri Nov 27 00:32:36 2020 from 192.168.0.106 [root@test ~]# cd /root [root@test ~]# rm -rf test.txt [root@test ~]# exit 登出 Connection to 192.168.0.20 closed. [root@test shell]#
Personal public number: