Chinaunix首页 | 论坛 | 博客
  • 博客访问: 240006
  • 博文数量: 49
  • 博客积分: 246
  • 博客等级: 二等列兵
  • 技术积分: 1034
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-02 13:18
文章分类

全部博文(49)

分类: 系统运维

2015-12-10 17:00:30


点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. #coding=utf-8
  3. #QQ: 917611
  4. import os
  5. import time
  6. import sys
  7. import re

  8. class Login:
  9.     def __init__(self,user,port,ip):
  10.         self.user = user
  11.         self.port = port
  12.         self.ip_dict = ip

  13.     def server(self):
  14.         for k,v in ip.items():
  15.             print k,v

  16.     def ssh1(self):
  17.         #传入了变量直接登陆
  18.         for key in ip.values():
  19.             if re.findall(sys.argv[1],key):
  20.                 print "正在登陆服务器%s...." %key
  21.                 cmd = 'ssh -p%s ops@114.114.114.114  -t ssh -p22 %s@%s' %(port,user,key)
  22.                 os.system(cmd)
  23.                 sys.exit()

  24.     def ssh2(self):
  25.         stats = True
  26.         while stats:
  27.             search_id = raw_input('please input ip or id Login server: ')
  28.             if len(search_id) == 0:
  29.                 continue
  30.             if search_id == 'q':
  31.                 sys.exit()
  32.             else:
  33.                 stats = False

  34.             #判断输入的是id还是ip
  35.             if len(search_id) == 1:
  36.                 cmd = 'ssh -p%s ops@114.114.114.114 -t ssh -p22 %s@%s' %(port,user,ip[int(search_id)])
  37.                 os.system(cmd)
  38.             else:
  39.                 for key in ip.values():
  40.                     if re.findall(search_id,key):
  41.                         print "正在登陆服务器%s...." %key
  42.                         cmd = 'ssh -p%s ops@114.114.114.114 -t ssh -p22  %s@%s' %(port,user,key)
  43.                         os.system(cmd)
  44. user = 'ops'
  45. port = 51826

  46. ip = {
  47.               1:"172.20.12.1" , #php
  48.               2:"172.20.12.2" , #pre_php
  49.               3:"172.20.12.3" , #API
  50.               4:"172.20.12.4" , #FastDFS
  51.               5:"172.20.12.5", #JAVA
  52.               6:"172.20.12.6", #JAVA
  53.               7:"172.20.12.7", #JAVA
  54.               8:"172.20.12.8" , #LB-Haproxy
  55.               0:"172.20.12.9" , #LB-Haproxy
  56.     }

  57. if __name__ == '__main__':
  58.     Job = Login(user,port,ip)
  59.     Job.server()
  60.     print '========================='
  61.     if len(sys.argv) == 2:
  62.         Job.ssh1()
  63.     else:
  64.         Job.ssh2()

阅读(2870) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~