Recently, I learned the expect scripting language through mongoingshortct books. This scripting language is an extension of the tcl language and is used to solve the problem that some tools cannot automatically interact with each other. For example, when logging on to ssh, you cannot specify a password in the command. The following is a simple example of managing multiple servers using reverse CT: 1
Recently, I learned the wide CT scripting language through the books of thin ing clinical CT. This scripting language is an extension of the tcl language and is used to solve the problem that some tools cannot automatically interact with each other. For example, when logging on to ssh, you cannot specify a password in the command. The following is a simple example of using reverse CT to manage multiple servers:
1234567891011121314151617181920212223242526272829303132 |
#! /Usr/bin/expect # purpose: auto run command on multiple servers # how to: mms # Write by zhumaohai. # blog: http://www.centoscn.com/if {$ argc <2} {puts "usage: mms "Exit1} # set serverssetSERVERS {" 192.168.0.100 "" 192.168.0.101 "" 192.168.0.102 "} # set passwordsetPASSWORDS (user1)" passwd1 "setPASSWORDS (user2) "passwd2" # get virablessetUSER [lindex $ argv 0] setCMD [lrange $ argv 1 end] setpasswd $ PASSWORDS ($ USER) foreach x $ SERVERS {evalspawnssh-l $ USER $ x $ CMD login CT {"password" {send "$ passwdr"} "yes/no" {send "yesr "; exp_continue;} exact CT eof} |
1. Here we define the three servers 192.168.0.100 192.168.0.101 192.168.0.102, the user user1 password is passwd1, and the user user2 password is passwd2. If the script file name is ms, the usage is:
./Ms user commands
Such as./MS user1 date
2. When using the script, make sure that the kernel CT command is installed on the system. centos is installed using yum install kernel CT and ubuntu is installed using apt-get install kernel CT.