Python simple host batch management tool and python host Tool
Forward indicated Source: http://www.cnblogs.com/0zcl/p/6352278.html
Today, I made a very simple small project and felt the power of the paramiko module. I also felt that my Linux skills were not good ~~
I. Requirements
Ii. Simple requirement analysis and Flowchart
There are few requirements. Let me simply say:
1. The host group can be implemented using configuration files (I use dictionaries to store data ).
2. the login function is not enabled. After you select a group, you can view the Host Name and IP address of the corresponding host in the group.
3. >>> cmd: df (multiple threads (depending on the number of hosts in the group) are run at the same time when you enter the command)
Output:
------------- H1 ------------
...... (Data returned by the command)
------------- H2 ------------
......
>>> Put test. yy (local file) filename (upload the local test. yy file to the/root directory of the remote host)
4. It can be written in the configuration file. Including remote host: Host Name, IP name, password, Port
Flowchart
I made the flowchart https://www.processon.com/diagraming/588a1219e4b087b11659068b
Iii. directory structure and source code
Directory structure:
From_windows.py (file to be uploaded)
Main. py (batch host Management Interface)
1 "batch host Management Interface" "2 3 import core4 5 if _ name _ =" _ main _ ": 6 core. run ()
View Code
Core. py (core code, called by Interface)
1 "core code" 2 import settings 3 import paramiko 4 import threading 5 import OS 6 7 8 class REMOTE_HOST (object ): 9 # remote host operation 10 def _ init _ (self, host, port, username, password, cmd): 11 self. host = host12 self. port = port13 self. username = username14 self. password = password15 self. cmd = Limit 16 17 def run (self): 18 "start a thread to connect to a remote host and call" "19 minutes _str = self. cmd. split () [0] 20 if hasattr (self, distinct _str): # reflection eg: Call Use the put Method 21 getattr (self, pai_str) () 22 else: 23 # setattr (x, 'y', v) is equivalent to ''x. y = v'' 24 setattr (self, pai_str, self. command) 25 getattr (self, cmd_str) () # Call the command method and execute a batch command to process 26 27 def command (self ): 28 "batch command processing" 29 ssh = paramiko. SSHClient () # create an ssh object 30 # Allow connection to host 31 ssh that is not in the know_hosts file. set_missing_host_key_policy (paramiko. autoAddPolicy () 32 ssh. connect (hostname = self. host, port = self. port, username = self. Username, password = self. password) 33 stdin, stdout, stderr = ssh.exe c_command (self. cmd) 34 result = stdout. read () 35 print ("% s ". center (50, "-") % self. host) 36 print (result. decode () 37 ssh. close () 38 39 def put (self): 40 "Upload File" "41 filename = self. cmd. split () [1] # the file to be uploaded 42 transport = paramiko. transport (self. host, self. port) 43 transport. connect (username = self. username, password = self. password) 44 sftp = p Aramiko. SFTPClient. from_transport (transport) 45 sftp. put (filename, filename) 46 print ("put sucesss") 47 48 transport. close () 49 50 51 def show_host_list (): 52 "display the Host Name and IP Address by selecting a group" "53 for index, key in enumerate (settings. msg_dic): 54 print (index + 1, key, len (settings. msg_dic [key]) 55 while True: 56 choose_host_list = input (">>> (eg: group1 )"). strip () 57 host_dic = settings. msg_dic.get (choose_host_list) 58 if Host_dic: 59 # print (host_dic) 60 for key in host_dic: 61 print (key, host_dic [key] ["IP"]) 62 return host_dic63 else: 64 print ("NO exit this group! ") 65 66 67 def interactive (choose_host_list): 68" batch interaction is performed by multiple threads on the selected group host "69 thread_list = [] 70 while True: 71 cmd = input (">>> "). strip () 72 if cmd: 73 for key in choose_host_list: 74 host, port, username, password = choose_host_list [key] ["IP"], choose_host_list [key] ["port"], \ 75 choose_host_list [key] ["username"], choose_host_list [key] ["password"] 76 func = REMOTE_HOST (host, port, username, password, cmd) # instantiate class 77 t = threading. thread (target = func. run) # Start thread 78 t. start () 79 thread_list.append (t) 80 for t in thread_list: 81 t. join () # The main thread waits for the sub-thread to finish executing 82 else: 83 continue84 85 86 def run (): 87 choose_host_list = show_host_list () 88 interactive (choose_host_list)
Settings. py (configuration file)
1 "configuration file" 2 3 msg_dic = {4 "group1": {# group 1 5 "h1": {"IP": "192.168.1.1 ", "username": "11", "password": "aa", "port": 22}, 6 "h2": {"IP": "192.168.1.2 ", "username": "22", "password": "bb", "port": 22}, 7 "h3": {"IP": "192.168.1.3 ", "username": "33", "password": "cc", "port": 22}, 8 "h4": {"IP": "192.168.1.4 ", "username": "44", "password": "dd", "port": 22}, 9 "h5": {"IP": "192.168.1.5 ", "username": "55", "password": "ee", "port": 22}, 10 "h6": {"IP": "192.168.1.6 ", "username": "66", "password": "ff", "port": 22}, 11}, 12 13 "group2": {# group 214 "h1 ": {"IP": "192.168.2.1", "username": "111", "password": "aaa", "port": 22}, 15 "h2 ": {"IP": "192.168.2.2", "username": "222", "password": "bbb", "port": 22}, 16 "h3 ": {"IP": "192.168.2.3", "username": "333", "password": "ccc", "port": 22}, 17 "h4 ": {"IP": "192.168.2.4", "username": "444", "password": "ddd", "port": 22}, 18 "h5 ": {"IP": "192.168.2.5", "username": "555", "password": "eee", "port": 22}, 19 "h6 ": {"IP": "192.168.2.6", "username": "666", "password": "fff", "port": 22}, 20 "h7 ": {"IP": "192.168.2.7", "username": "777", "password": "ggg", "port": 22}, 21 "h8 ": {"IP": "192.168.2.8", "username": "888", "password": "hhh", "port": 22}, 22 }, 23 24 "group3": {25 "h1": {"IP": "192.168.179.20."," username ":" root "," password ":" zcl ", "port": 22}, 26} 27}
View Code
Test:
Hardware restrictions: I only need to connect to a virtual machine for testing ~
C:\Python34\python3.exe C:/Users/Administrator/PycharmProjects/laonanhai/host_manage/main.py1 group1 62 group3 13 group2 8>>>(eg:group1)group3h1 192.168.179.133>>>put from_windows.pyput sucesss>>>>>>ls------------------------192.168.179.133------------------------anaconda-ks.cfgdatabase_testfrom_windows.pyinstall.loginstall.log.syslogmoot\roottmp\from_windows.py>>>
There is no from_windows.py file before the upload. It will be available after the upload!
PS: About paramiko module can look at this blog http://www.cnblogs.com/starof/p/4670433.html