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