Python Development Program: simple host batch management tool, python Development

Source: Internet
Author: User

Python Development Program: simple host batch management tool, python Development

Question: simple host batch management tools

Requirements:

 

Flowchart:

Note:

### Author Introduction: * author: lzl ### blog address: * http://www.cnblogs.com/lianzhilei/p/5881434.html### function to achieve the title: simple host Batch Management Tools requirements: Host group after logon display Host group, after you select a group, you can view the host list to execute commands and send files in batches. The results are returned in real time. The host user name and password can be different ### directory structure: host-Manage │ ── ftpclient # client program ── README.txt ── management. py # ├ ── database # database ── test. py # modify database ### note the format of commands for batch execution and file upload sending: put database/tmp/db ### running environment windows python3.0 +README

 

Main Program:

#! /Usr/bin/env python #-*-coding: UTF-8-*-#-Author-Lianimport jsonimport paramikoimport threadingclass Remotehost (object ): # remote 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 command (self): # obtain the command ssh = paramiko. SSHClient () ssh. set_missing_host_key_policy (paramiko. autoAddPolicy () # Allow connection to host ssh that is not in the know_hosts file. connect (hostname = self. host, port = self. port, username = self. username, password = self. password) # connect to the server stdin, stdout, stderr = ssh.exe c_command (self. cmd) # obtain the command result res, err = stdout. read (), stderr. read () # result = res if res else err print ("[% s]". center (50, "-") % self. host) print (result. decode () # print out ssh. close () def put (self): # upload try: transport = paramiko. transport (self. host, self. port) transport. connect (username = self. username, password = self. password) sftp = paramiko. SFTPClient. from_transport (transport) sftp. put (self. cmd. split () [1], self. cmd. split () [2]) # Upload File transport. close () print ("\ 033 [32; 0m [% s] Upload File [% s] successful .... \ 033 [0 m "% (self. host, self. cmd. split () [2]) failed t Exception as error: # Catch the Exception print ("\ 033 [31; 0m error: [% s] [% s] \ 033 [0 m "% (self. host, error) def run (self): # reflection pai_str = self. cmd. split () [0] if hasattr (self, inclu_str): getattr (self, inclu_str) () else: setattr (self, inclu_str, self. command) getattr (self, cmd_str) () if _ name _ = "_ main _": # main program with open ("database", "r ") as file: data_dict = json. loads (file. read () # obtain database information for k in data_dict: # print the address group print (k) group_choice = input ("enter the name of the group to be operated :"). strip () if data_dict.get (group_choice): host_dict = data_dict [group_choice] # define the host dictionary for k in host_dict: # print all the host names of the selected address group print (k) while True: cmd = input ("select the operation command :"). strip () thread_list = [] if cmd: # The command is not empty for k in host_dict: host, port, username, password = k, host_dict [k] ["port"], host_dict [k] ["username"], host_dict [k] ["password"] func = Remotehost (host, port, username, password, cmd) # instantiate class t = threading. thread (target = func. run) # create thread t. start () thread_list.append (t) for t in thread_list: t. join () # Wait for the thread to execute the result else: print ("\ 033 [31; 0m operation group does not exist \ 033 [0 m ")

 

Database:

{"Group1": {"192.168.20.217": {"password": "123456", "username": "root", "port": 22}, "192.168.20.219 ": {"password": "zyw @ 123", "username": "root", "port": 22 }}, "group2": {"192.168.20.217 ": {"password": "123456", "username": "root", "port": 22 }}}Database

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.