免密碼登入伺服器python指令碼

來源:互聯網
上載者:User

標籤:

在自動化營運平台沒有做完之前,常需要登入伺服器做很多維護操作,每次找好長好長的密碼,那麼多伺服器,你會瘋掉的,所以瞎搞了以下指令碼.先解一下燃眉之急,哈哈

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}

用下面指令碼調用 /opt/.script/login_root.exp 指令碼,登入你需要登入的伺服器

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 are 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 TypeError:        print "\033[31;1mThere is no matching to the correct IP address \033[0m"except IndexError:    print "\033[31;1m You do not need to log in to the IP information input\033[0m"#print ipinfo,

測試:

  python login_remote.py 10.100

免密碼登入伺服器python指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.