24212 发表于 2016-2-23 09:18:00

python:使用paramiko批量管理服务器(含密钥和密码)

最近在学习python,学一个模块,写一个脚本,方便以后应用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/local/bin/python2.7
# -*- coding: utf-8 -*-
'''
使用paramiko模块通过SSH登陆将执行多条命令,需要多次执行登陆操作。
'''
from paramiko importSSHClient
from paramiko import AutoAddPolicy
from paramiko import RSAKey

class SshServerCMD():
    def __init__(self):
      #定义服务器IP和用户名和密钥之类
      serverfile = "D:\\testtmp\\server_list2.txt"
      serverlist = open(serverfile,"r")
      for line in serverlist:
            #取IP、用户名和密码或者密钥文件等
            if len(line.split()) == 2:
                self.HostIP = '%s'% line.split()
                self.UserName = '%s'% line.split()
                self.Private_key = RSAKey.from_private_key_file(filename='D:\\key_pub\\id_rsa2048.ppk')#操作机的先私钥路径
                self.client = SSHClient()
                self.Ssh_Pkey(self.HostIP,self.UserName,self.Private_key)
            elif len(line.split()) > 2:
                self.HostIP = '%s'% line.split()
                self.UserName = '%s'% line.split()
                self.PassWord = '%s'% line.split()
                self.client = SSHClient()
                self.SSH_Passwd(self.HostIP,self.UserName,self.PassWord)
            else:
                print "%s文件列表长度出错,第一列为服务器IP,第二列为服务器用户名,第三列为服务器对应用户密码!" % serverfile

    def SSH_Passwd(self,HostIP,UserName,PassWord):
      #使用密码登陆
      self.client.set_missing_host_key_policy(AutoAddPolicy())
      self.client.load_system_host_keys()
      self.client.connect(hostname=HostIP,port=22,username=UserName,password=PassWord)   #使用密钥登陆
      print 'HOSTIP:"%s" doing command:' % HostIP
      self.DoCommand(HostIP)
      self.client.close()
      print 'HOSTIP:"%s" is close' % HostIP

    def Ssh_Pkey(self,HostIP,UserName,Private_key):
      #使用密码登陆
      self.client.set_missing_host_key_policy(AutoAddPolicy())
      self.client.load_system_host_keys()
      private_key2 = RSAKey.from_private_key_file(filename='D:/key_pub/id_rsa2048.ppk')#操作机的先私钥路径
      print HostIP,UserName
      self.client.connect(hostname=HostIP,port=22,username=UserName,pkey=Private_key)#使用密钥登陆
      print 'HOSTIP:"%s" doing command:' % HostIP
      self.DoCommand(HostIP)
      self.client.close()
      print 'HOSTIP:"%s" is close' % HostIP

    def DoCommand(self,HostIP):
      CMDList = open("D:\\testtmp\\cmd_list.txt","r")#执行文件中的命令
      RED_COLOR='\033[1;31;48m'#红 ,配置终端输出的颜色
      BLUE_COLOR='\033[1;34;48m'#红 ,配置终端输出的颜色
      RES='\033[0m'
      for cmddo in CMDList:
            stdin, stdout, stderr = self.client.exec_command(cmddo)
            DoOut=stdout.read()
            if DoOut =='' :
                print '%s"%s" execution result is NULL,please check command!%s' % (BLUE_COLOR,cmddo.split(),RES)
            else:
                print '%s"%s" execution result:%s' % (RED_COLOR,cmddo.split(),RES)
                print DoOut

if __name__ == "__main__":
    SshServerCMD()





server_list2.txt文件内容:
IP   用户名

IP   用户名密码

cmd_list.txt文件内容:(就是命令)

whoami
/sbin/ifconfig
ifconfig
/bin/hostname

执行结果:


页: [1]
查看完整版本: python:使用paramiko批量管理服务器(含密钥和密码)