Simple Python Master Batch management tool

Source: Internet
Author: User
today did a very simple small project, feel the Paramiko module of the powerful, but also feel that their own Linux power not ~ ~

First, the demand

Second, simple requirements analysis and flow chart


With few requirements, I'll simply say:
1. Host groupings can be configured with file implementations (I use a dictionary to store data).
2. Login function is not done. Select a group to view the host name and IP address of the corresponding host in the group.
3. >>>CMD:DF (Input command from multiple threads (depending on the number of hosts in the group)
Output:
-------------h1------------
...... (The data returned by the command)
-------------h2------------
......
>>>put test.yy (local file) filename (the local test.yy file is uploaded to the/root directory of the remote host)
4. Can be written in the configuration file. Includes remote host: hostname IP user name password port

Flow chart

III. directory structure and source code

Directory structure:

from_windows.py (file to be uploaded)

main.py (Bulk host management interface)

"" "Bulk Host Management Interface" "" Import coreif name = = "Main":    Core.run ()

core.py (core code, called by the interface)

"" "Core Code" "" Import settingsimport paramikoimport threadingimport osclass remote_host (object): #远程操作主机 def init (self, h        OST, port, username, password, cmd): self.host = host Self.port = Port Self.username = Username Self.password = password Self.cmd = cmd def run (self): "" "After the thread connects to the remote host, call" "" "" "Cmd_str = SELF.CMD.SPL            It () [0] if hasattr (self, cmd_str): #反射 eg: Call the Put method GetAttr (self, CMD_STR) () Else: #setattr (x, ' Y ', V) is equivalent to ' X.y=v "setattr (self, cmd_str, Self.command) getattr (self, C MD_STR) () #调用command方法, perform a batch command processing def command (self): "" "Bulk Command Processing" "" "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 (, "-")% self.host) print (Result.decode ()) Ssh.close () def put (self ): "" "Upload File" "" filename = Self.cmd.split () [1] #要上传的文件 transport = Paramiko. Transport ((Self.host, Self.port)) Transport.connect (Username=self.username, password=self.password) SFTP = P Aramiko. Sftpclient.from_transport (transport) sftp.put (filename, filename) print ("Put sucesss") Transport.clos  E () def show_host_list (): "" Displays host name and 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 H Ost_dic:print (Key, host_dic[key]["IP"]) return host_dic else:print ("NO exit This group! ") def Interactive (choose_host_lIST): "" "the bulk interaction of multiple threads based on the selected grouping host" "" Thread_list = [] While true:cmd = input (">>>"). Strip () I F 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"], Choo                se_host_list[key]["Password"] func = Remote_host (host, port, username, password, cmd) # instantiate class t = Threading. Thread (target=func.run) # up Thread T.start () thread_list.append (t) for T in Thread_li St:t.join () # The main thread waits for the child thread to execute Else:continuedef run (): Choose_host_list = Show_host_lis T () Interactive (choose_host_list)

settings.py (config file)

"" Profile "" "Msg_dic = {" Group1 ": {#分组1" H1 ": {" IP ":" 192.168.1.1 "," username ":" One "," Password ":" AA "," Port ": 22}, "H2": {"IP": "192.168.1.2", "username": "$", "password": "BB", "Port": $}, "H3": {"IP": "192.168.1.3", "username        ":", "Password": "CC", "Port": "H4": {"IP": "192.168.1.4", "username": "22", "Password": "DD", "Port":},  "h5": {"IP": "192.168.1.5", "username": "", "" Password ":" ee "," Port ":" $ "," h6 ": {" IP ":" 192.168.1.6 "," username ":" 66 ", "Password": "FF", "Port": "Group2": {#分组2 "H1": {"IP": "192.168.2.1", "username": "111", "Password": "A AA "," Port ": $}," H2 ": {" IP ":" 192.168.2.2 "," username ":" 222 "," Password ":" BBB "," Port ": $}," H3 ": {" IP ":" 192.1 68.2.3 "," username ":" 333 "," Password ":" CCC "," Port ": $}," H4 ": {" IP ":" 192.168.2.4 "," username ":" 444 "," password ":" DD D "," Port ":" H5 ": {" IP ":" 192.168.2.5 "," username ":" 555 "," password ":" Eee "," Port ": $}," H6 ": {" IP ":" 192.16 8.2.6 "," username ":" 666 ", "password": "FFF", "Port": "$", "H7": {"IP": "192.168.2.7", "username": "777", "Password": "GGG", "Port": $}, "H 8 ": {" IP ":" 192.168.2.8 "," username ":" 888 "," password ":" HHH "," Port ": $},}," Group3 ": {" H1 ": {" IP ":" 192.168.179 .133 "," username ":" root "," password ":" ZCL "," Port ": 22},}}

Test:

Hardware limitations, I only use to connect a single virtual machine test ~

C:\Python34\python3.exe c:/users/administrator/pycharmprojects/laonanhai/host_manage/main.py1 group1 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 uploading, you have it after uploading!

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.