1 root@zenghui:~/python# cat ipnew.txt 2 vm_esxi133,192.168.11.133,22,1qaz2wsx,root,0 3 int_www45,192.168.11.45,22,wdzj201445,root,0 4 int_23,192.168.11.23,22,wdzj2014,root,0 5 int_24,192.168.11.24,2022,wdzjkey,salt,1 6 root@zenghui:~/python# cat ip_ssh.py 7 #!/usr/bin/python 8 #-*- coding:utf-8 -*- 9 import pexpect,commands,os,re,sys 10 import struct, fcntl, signal,termios 11 ip_new=open('ipnew.txt','a+') 12 global ipaddress,ipaddresskey 13 ipaddress={} 14 ipaddresskey={} 15 16 #从ipnew.txt获取内容加入ipaddress字典中 17 def ip_name(): 18 file=open('ipnew.txt','r').readlines() 19 ii=0 20 for i in file: 21 ii+=1 22 aa=i.split(',') 23 ipaddress.setdefault(ii,[aa[0],[aa[1],aa[2],aa[3],aa[4],aa[5]]]) 24 ipaddresskey.setdefault(aa[0],[aa[1],aa[2],aa[3],aa[4],aa[5]]) 25 #print ipaddress 26 #添加用户 27 def ipadd(): 28 ipre=[] 29 namere=[] 30 portre=[] 31 pwre=[] 32 keyre=[] 33 usernamere=[] 34 while namere==[]: 35 name=str(raw_input('请输入你的项目名称: ')) 36 namere=re.findall('w+',name) 37 if len(namere) > 1 or ipaddresskey.has_key(name)==True: 38 print '你输入的格式不对或名称已经存在,请重新输入!' 39 namere=[] 40 41 while ipre==[]: 42 ip=str(raw_input('请输入ip地址:192.168.11.23: ')) 43 ipre=re.findall('d{1,3}.d{1,3}.d{1,3}.d{1,3}',ip) 44 if len(ipre) > 1: 45 ipre=[] 46 while portre==[]: 47 port=str(raw_input('请输入端口号:22: ')) 48 portre=re.findall('d{1,5}',port) 49 if len(portre) > 1: 50 portre=[] 51 52 while pwre==[]: 53 pw=str(raw_input('请输入密码“: ')) 54 pwre=re.findall('S+',pw) 55 if len(pwre) > 1: 56 pwre=[] 57 58 while usernamere==[]: 59 username=str(raw_input('输入用户名: ')) 60 usernamere=re.findall('w+',username) 61 if len(usernamere) > 1: 62 usernamere=[] 63 64 while keyre==[]: 65 key=str(raw_input('请输入key“: ')) 66 keyre=re.findall('S+',key) 67 if len(keyre) > 1: 68 keyre=[] 69 70 ipadd=str(name)+','+str(ip)+','+str(port)+','+str(pw)+','+str(username)+','+str(key)+' ' 71 ip_new.write(ipadd) 72 print ipadd+'添加成功!' 73 exit() 74 # ip_new.close() 75 # file.close() 76 77 78 #主菜单 79 def menu(): 80 os.system('clear') 81 print '**************************MENU*****************************' 82 for key,value in ipaddress.items(): 83 print str(key)+' '+str(value[0])+' '+str(value[1][0]) 84 print "回车退出!" 85 print '***********************************************************' 86 name=int(raw_input('请输入机器编号: ')) 87 if name == '': 88 exit() 89 ssh_command(ipaddress[name][1][0],ipaddress[name][1][1],ipaddress[name][1][2],ipaddress[name][1][3],ipaddress[name][1][4].strip()) 90 91 92 #用户登陆 93 def ssh_command(ipaddr,port,passwd,name,key): 94 try: 95 global ssh_key 96 if key == '0': 97 keys='' 98 ssh_key=pexpect.spawn('ssh %s -p %s %s@%s' % (keys,port,name,ipaddr)) 99 status=ssh_key.expect(['password']) 100 else: 101 keys='-i id_dsa_key' 102 ssh_key=pexpect.spawn('ssh %s -p %s %s@%s' % (keys,port,name,ipaddr)) 103 status=ssh_key.expect([pexpect.TIMEOUT,'id_dsa_key']) 104 if status==1: 105 ssh_key.sendline(passwd) 106 elif status==0: 107 ssh_key.sendline('yes ') 108 ssh_key.expect('password:') 109 ssh_key.sendline(passwd) 110 signal.signal(signal.SIGWINCH, sigwinch_passthrough) 111 size = getwinsize() 112 ssh_key.setwinsize(size[0], size[1]) 113 ssh_key.interact() 114 except Exception as e: 115 print e 116 117 #设置终端大小适合窗口 118 def sigwinch_passthrough (sig, data): 119 winsize = getwinsize() 120 global ssh_key 121 ssh_key.setwinsize(winsize[0],winsize[1]) 122 #获取终端屏幕大小 123 def getwinsize(): 124 if 'TIOCGWINSZ' in dir(termios): 125 TIOCGWINSZ = termios.TIOCGWINSZ 126 else: 127 TIOCGWINSZ = 1074295912L # Assume 128 s = struct.pack('HHHH', 0, 0, 0, 0) 129 x = fcntl.ioctl(sys.stdout.fileno(), TIOCGWINSZ, s) 130 return struct.unpack('HHHH', x)[0:2] 131 132 133 if __name__ == "__main__": 134 try: 135 if len(sys.argv) == 2: 136 if sys.argv[1]=='ipadd': 137 ip_name() 138 ipadd() 139 else: 140 print '请输入参数"ipadd"' 141 elif len(sys.argv) == 1: 142 ip_name() 143 menu() 144 else: 145 print '请重新输入!' 146 except: 147 pass