In the automatic operation and maintenance platform is not finished, often need to log on to the server to do a lot of maintenance operations, every time to find a good long password, so many servers, you will be crazy, so blind to make the following script. First of all, haha.
Cat Login_root.exp
#!/usr/bin/expect -cset IP [lindex $argv 0]set PWD [lindex $argv 1]set timeout 2spawn ssh [email protected]$IPexpect "*yes/no*" {send "yes\r"}expect "*assword*" {send "$PWD\r";interact}
Use the following script to invoke the/opt/.script/login_root.exp script and log in to the server you need to log in
Cat login_remote.py#!/usr/bin/env python#author:[email protected] #name: login_remote.py import Re,timeimport Sysimport commandsimport subprocessdef Login_main (ip,pwd): if IP! = "" and pwd! = "": print "\033[32;1m start login at:% s\033[0m "%time.strftime ("%y-%m-%d%h:%m:%s ", Time.localtime ()) p = Subprocess.call (" Expect/opt/.script/troot.exp%s " %s ' "% (ip,pwd), shell=true) else:print" \033[32;1m IP or the password is wrong, please handle \033[0m "Def re_ip (Ipinfo): With open ('/root/.script/password.txt ', ' R ') as F:for ipline in F.readlines (): Hostinfo = Re.match (R ' ^192.168.%s. * ') %ipinfo,ipline,re. M|re. I) If Hostinfo:print "\033[32;1m you is going to the login server IP is:%s\033[0m"%hostinfo.group (). Split () [0 ] Return Hostinfo.group (). Split () [0],hostinfo.group (). Split () [1]if __name__ = = "__main__": Try:ipinfo = sys.argv [1].strip () if ipinfo! = "": Hostinfo = Re_ip (ipinfo) Try:login_main (hostinfo[0],hostinfo[1]) except T Ypeerror: print "\033[31;1mthere is no matching to the correct IP address \033[0m" except Indexerror:print "\033[31;1m Y ou do not need to log in to the IP information input\033[0m "#print Ipinfo,
Test:
Python login_remote.py 10.100
Password-free logon server Python script