cvmachine.com - 申博开户网

查找: 您的方位主页 > 网络频道 > 阅览资讯:python之主机批量办理工具都有哪些

python之主机批量办理工具都有哪些

2017-04-16 10:07:18 来历:www.cvmachine.com 【

python之主机批量办理工具都有哪些

今日做了一个很简略的小项目,感触到了paramiko模块的强壮,也深感自己Linux的功力不可~~

一、需求

python之主机批量办理工具都有哪些

二、简略需求剖析及流程图

需求很少,我就简略地说下:

1. 主机分组可以配置文件完成(我用字典存数据的).

2. 登陆功用不做。挑选分组后可检查组内对应主机的主机名和IP地址.

3. >>>cmd: df(输入指令则起多个线程(视组内有多少个主机而定)一起履行)

输出:

-------------h1------------

……(指令回来的数据)

-------------h2------------

……

>>>put test.yy(本地文件) filename (把本地的test.yy文件传到长途主机的/root目录下)

4.可写在配置文件中。包含长途主机的: 主机名 IP 用户名 暗码 端口

流程图

python之主机批量办理工具都有哪些

三、目录结构及源代码

目录结构:

python之主机批量办理工具都有哪些

from_windows.py(待上传的文件)

main.py(批量主机办理接口)

 """批量主机办理接口"""
 
 import core
 
 if __name__ == "__main__":
  core.run() 

core.py(中心代码,被接口调用)

"""中心代码"""
import settings
import paramiko
import threading
import os


class REMOTE_HOST(object):
 #长途操作主机
 def __init__(self, host, port ,username, password, cmd):
  self.host = host
  self.port = port
  self.username = username
  self.password = password
  self.cmd = cmd

 def run(self):
  """起线程衔接长途主机后调用"""
  cmd_str = self.cmd.split()[0]
  if hasattr(self, cmd_str):  #反射 eg:调用put办法
   getattr(self, cmd_str)()
  else:
   #setattr(x,'y',v)is equivalent to ``x.y=v''
   setattr(self, cmd_str, self.command)
   getattr(self, cmd_str)() #调用command办法,履行批量指令处理

 def command(self):
  """批量指令处理"""
  ssh = paramiko.SSHClient() #创立ssh目标
  #答应衔接不在know_hosts文件中的主机
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  ssh.connect(hostname=self.host,port=self.port,username=self.username,password=self.password)
  stdin,stdout,stderr = ssh.exec_command(self.cmd)
  result = stdout.read()
  print("%s".center(50, "-") % self.host)
  print(result.decode())
  ssh.close()

 def put(self):
  """上传文件"""
  filename = self.cmd.split()[1] #要上传的文件
  transport = paramiko.Transport((self.host, self.port))
  transport.connect(username=self.username, password=self.password)
  sftp = paramiko.SFTPClient.from_transport(transport)
  sftp.put(filename, filename)
  print("put sucesss")

  transport.close()


def show_host_list():
 """经过挑选分组显现主机名与IP"""
 for index, key in enumerate(settings.msg_dic):
  print(index + 1, key, len(settings.msg_dic[key]))
 while True:
  choose_host_list = input(">>>(eg:group1)").strip()
  host_dic = settings.msg_dic.get(choose_host_list)
  if host_dic:
   #print(host_dic)
   for key in host_dic:
    print(key, host_dic[key]["IP"])
   return host_dic
  else:
   print("NO exit this group!")


def interactive(choose_host_list):
 """依据挑选的分组主机起多个线程进行批量交互"""
 thread_list = []
 while True:
  cmd = input(">>>").strip()
  if cmd:
   for key in choose_host_list:
    host, port, username, password = choose_host_list[key]["IP"], choose_host_list[key]["port"], \
             choose_host_list[key]["username"], choose_host_list[key]["password"]
    func = REMOTE_HOST(host, port, username, password, cmd) # 实例化类
    t = threading.Thread(target=func.run) # 起线程
    t.start()
    thread_list.append(t)
   for t in thread_list:
    t.join() # 主线程等候子线程履行结束
  else:
   continue


def run():
 choose_host_list = show_host_list()
 interactive(choose_host_list)

settings.py(配置文件)

"""配置文件"""

msg_dic = {
 "group1":{ #分组1
  "h1":{"IP":"138.com", "username":"11", "password":"aa", "port":22},
  "h2":{"IP":"192.168.1.2", "username":"22", "password":"bb", "port":22},
  "h3":{"IP":"192.168.1.3", "username":"33", "password":"cc", "port":22},
  "h4":{"IP":"192.168.1.4", "username":"44", "password":"dd", "port":22},
  "h5":{"IP":"192.168.1.5", "username":"55", "password":"ee", "port":22},
  "h6":{"IP":"192.168.1.6", "username":"66", "password":"ff", "port":22},
 },

 "group2":{ #分组2
  "h1":{"IP":"192.168.2.1", "username":"111", "password":"aaa", "port":22},
  "h2":{"IP":"192.168.2.2", "username":"222", "password":"bbb", "port":22},
  "h3":{"IP":"192.168.2.3", "username":"333", "password":"ccc", "port":22},
  "h4":{"IP":"192.168.2.4", "username":"444", "password":"ddd", "port":22},
  "h5":{"IP":"192.168.2.5", "username":"555", "password":"eee", "port":22},
  "h6":{"IP":"192.168.2.6", "username":"666", "password":"fff", "port":22},
  "h7":{"IP":"192.168.2.7", "username":"777", "password":"ggg", "port":22},
  "h8":{"IP":"192.168.2.8", "username":"888", "password":"hhh", "port":22},
 },

 "group3":{
  "h1":{"IP":"192.168.179.133", "username":"root", "password":"zcl", "port":22},
 }
}

测验:

硬件约束,我只用衔接一台虚拟机测验~

C:\Python34\python3.exe C:/Users/Administrator/PycharmProjects/laonanhai/host_manage/main.py
1 group1 6
2 group3 1
3 group2 8
>>>(eg:group1)group3
h1 192.168.179.133
>>>put from_windows.py
put sucesss
>>>
>>>ls
------------------------192.168.179.133------------------------
anaconda-ks.cfg
database_test
from_windows.py
install.log
install.log.syslog
m
oot
\root
tmp\from_windows.py

>>> 

上传前没有from_windows.py文件,上传后就有了!

python之主机批量办理工具都有哪些

以上便是本文的全部内容,期望对咱们的学习有所协助,也期望咱们多多支撑申博开户。


本文地址:http://www.cvmachine.com/a/question/87944.html
Tags: 简略 python 详解
修改:申博开户网
关于咱们 | 联络咱们 | 友情链接 | 网站地图 | Sitemap | App | 回来顶部