we can expect automatic login to the remote machine, and automatic remote execution of commands can be achieved. Of course, if the use of key authentication without a password can also automatically log and automatic remote execution of commands. But you can not use key authentication, we have no other way. So, this time to know each other as long as the machine account and password to log in and can be achieved through remote commands expect script.
Ready for distribution: template script, server ip, user name, password, expect script
yum install -y expect
expect the script to log the machine:
vim 1.expect #!/usr/bin/expect set host "192.168.133.132" # connection to the host set passwd "123456" # password spawn ssh root@$host #spawn call the shell command ssh (login), "set host" and "set passwd" to expect define two variables expect { "yes/no" { send "yes\r"; exp_continue} #ssh remote login for the first time a host will be prompted yes / no, yes it is sent in the past; "\ r" represents a carriage return "password:" { send "$passwd\r" } # If you need to send the password prompt passwd past, user interaction, "\ r" represents a carriage return } interact #Interact role is to stay on a remote machine, do not quit # End script symbol: expect eof-- execution pause a few seconds after the end of the exit # If you do not add any end symbol, quit immediately after the command is executed
Note that the 755 needs permission that chmod a + x 1.expect
Execute the script using: ./ 1.expect
expect script remote execution of commands:
vim 2.expect #!/usr/bin/expect set user "root" set passwd "123456" spawn ssh [email protected] expect { "yes/no" { send "yes\r"; exp_continue} "password:" { send "$passwd\r" } } expect "]*" # Match to "]" refers to the user information right bracket on the left after a successful login, which means executing the following command after a successful login send "touch /tmp/12.txt\r" "\ r" to indicate carriage to the left and then match user information parentheses expect "]*" send "echo 1212> /tmp/12.txt\r" "\ r" to indicate carriage to the left and then match user information parentheses expect "]*" send "exit\r"
expect script arguments:
vim 3.expect #!/usr/bin/expect # Call the internal parameters, the user name ip password, called directly in the implementation of set user [lindex $argv 0] set host [lindex $argv 1] set passwd "123456" set cm [lindex $argv 2] # This is the third parameter is the command to be executed, such as ls spawn ssh $user@$host expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect "]*" send "$cm\r" expect "]*" send "exit\r" # You need to perform multiple command; separation and double quotation marks, such as "ls; w; cat 1.txt" #expect exist timeout, this command is not suitable for running vmstart # If you want to modify the timeout in send "$ cm \ r" plus the next line Timeout is not permanently set timeout -1 set timeout 5 five seconds to launch
expect scripts to synchronize files:
vim 4.expect #!/usr/bin/expect set passwd "123456" spawn rsync -av [email protected]:/tmp/12.txt /tmp/ # 12.txt on the remote machine to the machine synchronous expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof # End script symbol: expect eof-- execution pause a few seconds after the end of the exit
expect script file and specify the host to be synchronized:
vim 5.expect #!/usr/bin/expect set passwd "123456" set host [lindex $argv 0] set file [lindex $argv 1] spawn rsync -av $ file root @ $ host: $ file #file write absolute path expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof chmod a+x 5.expect touch 1.txt ./5.expect 192.168.133.132 "/tmp/1.txt"
Build file distribution system
Here the idea is first of all expect to write the script, and then provide parameters, and finally the use of shell scripts to achieve distribution function
Step # expect to write the script, the last shell is invoked to here to realize the function vim rsync.expect #!/usr/bin/expect set passwd "123456" set host [lindex $argv 0] set file [lindex $argv 1] # This refers to a list of IP and the corresponding file in the final shell script to achieve spawn rsync -av --files-from=$file / root@$host:/ # If you can not guarantee that the other machine has the same path together -avR expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof
# The second step provides the file to be transferred collection, which is the second parameter exoect script file vim list.txt /root/shell/1.sh ... ... # This file can add multiple files, this is to transfer files
Step # ip definition to the content, which is host of parameters expect vim iplist.txt 192.168.133.132 192.168.133.133 ...... # Here IP (host) password to be consistent and rsync.expect script. In order to avoid the risk of password leak, you can use key authentication method
# Finally realize the function shell script vim rsync.sh #!/bin/bash for ip in `cat iplist.txt` do echo $ip ./rsync.expect $ip list.txt done # This script action is through the file list and ip
Batch remote execution of commands
Step # expect script calls and host specific commands $ cm vim exe.expect #!/usr/bin/expect set host [lindex $ argv 0] # the first variable set passwd "123456" set cm [lindex $ argv 1] # second variable spawn ssh root@$host expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect "]*" send "$cm\r" expect "]*" send "exit\r" # This script is the role of remote command execution chmod a+x exe.expect
# Then define the command shell script, and use the specified ip # The purpose of execution of this script is a disposable multiple machines perform the same command, all shell commands to be executed are defined vim exe.sh #!/bin/bash for ip in `cat iplist.txt` do echo $ip ./exe.expect $ip "w;free -m;ls /tmp" done # Action of the script is to call iplist.txt file IP and exe.expect script