This article provides a graphic explanation of python's simple host batch management tool. Today, we have made a very simple small project. we feel the power of the paramiko module and the power of our Linux system ~~
I. requirements
From_windows.py (file to be uploaded)
Main. py (batch host management interface)
"Batch host management interface" import coreif name = "main": core. run ()
Core. py (core code, called by Interface)
"Core code" import settingsimport paramikoimport threadingimport osclass REMOTE_HOST (object): # remote operation host 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): "" call "when a thread connects to a remote host" "cmd_str = self. cmd. split () [0] if hasattr (self, pai_str): # reflection eg: Call the put method getattr (self, pai_str) () else: # setattr (x ,' Y', v) is equivalent to ''' x. y = v '''setattr (self, pai_str, self. command) getattr (self, cmd_str) () # Call the command method and execute the batch command to process def command (self): "batch command processing" ssh = paramiko. SSHClient () # create an ssh object # allow connection to the host ssh that is not in the know_hosts file. 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.exe c_command (se Lf. cmd) result = stdout. read () print ("% s ". center (50, "-") % self. host) print (result. decode () ssh. close () def put (self): "" Upload file "filename = self. cmd. split () [1] # file to be uploaded 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") trans Port. close () def show_host_list (): "display the host name and IP address by selecting a group" 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):" batch interaction is performed by multiple threads based on the selected grouping host "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) # instantiate Class t = threading. thread (target = func. run) # start thread t. start () thread_list.append (t) for t in thread_list: t. join () # The main thread waits for the sub-thread to finish executing else: continuedef run (): choose_host_list = show_host_list () interactive (choose_host_list)
Settings. py (configuration file)
"Configuration file" msg_dic = {"group1": {# Group 1 "h1": {"IP": "192.168.1.1", "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": {# Group 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.20."," username ":" root "," password ":" zcl "," port ": 22 },}}
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!
The above is a detailed description of python's simple host batch management tool. For more information, see other articles in the first PHP community!