Password-free logon server Python script

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.