Detailed Python Simple host 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 core if __name__ = = "__main__":  Core.run ()

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

"" "Core Code" "" Import settingsimport paramikoimport Threadingimport OS class Remote_host (object): #远程操作主机 def __init__ (self, Host, port, username, password, cmd): self.host = host Self.port = Port Self.username = Username Self.passwor  d = password Self.cmd = cmd def run (self): "" "After the thread connects to the remote host, call" "" "" "Cmd_str = Self.cmd.split () [0] if hasattr (self,      CMD_STR): #反射 eg: Call the Put method GetAttr (self, CMD_STR) () Else: #setattr (x, ' Y ', V) are equivalent to ' x.y=v ' SetAttr (self, cmd_str, Self.command) getattr (self, Cmd_str) () #调用command方法, performing 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) p  Rint (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 = Paramiko. Sftpclient.from_transport (transport) sftp.put (filename, filename) print ("Put sucesss") transport.close () def SH Ow_host_list (): "" "Displays host name and IP" "for index, key in Enumerate (Settings.msg_dic): print (index + 1, key, Len (setting) by selecting Group S.msg_dic[key]) While true:choose_host_list = input (">>> (Eg:group1)"). Strip () Host_dic = Settings.msg_di C.get (choose_host_list) if host_dic: #print (host_dic) for key in Host_dic:print (key, host_dic[key]["I  P "]) 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 () 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) # up Thread T.start () thread_list.append (t) for T in Thread_list:t.join () # Master Thread waits for child thread to execute Else:continue def run (): Choose_host_list = Show_host_list () Interactive (choose_host_list)

settings.py (configuration file)

"" "" "" "" "" Msg_dic = {"Group1": {#分组1 "H1": {"IP": "192.168.1.1", "username": "One", "Password": "AA", "Port": "$", "H2 ": {" IP ":" 192.168.1.2 "," username ":" $ "," password ":" BB "," Port ":" $ "," H3 ": {" IP ":" 192.168.1.3 "," username ":" 33 "," Password ":" "CC", "Port": "H4": {"IP": "192.168.1.4", "username": "" "," "Password": "DD", "Port": "192", "H5": {"IP": "". "." 168.1.5 "," username ":" "," "Password": "ee", "Port": "$", "h6": {"IP": "192.168.1.6", "username": "$", "password": "FF", " Port ": $},}," Group2 ": {#分组2" H1 ": {" IP ":" 192.168.2.1 "," username ":" 111 "," Password ":" AAA "," Port ": $}," H2 ": {" I P ":" 192.168.2.2 "," username ":" 222 "," Password ":" BBB "," Port ":" $ "," H3 ": {" IP ":" 192.168.2.3 "," username ":" 333 "," Password ":" "CCC", "Port": "H4": {"IP": "192.168.2.4", "username": "444", "Password": "DDD", "Port": $}, "h5": {"IP": "1 92.168.2.5 "," username ":" 555 "," password ":" Eee "," Port ":" $ "," h6 ": {" IP ":" 192.168.2.6 "," username ":" 666 "," Password ": "FFF", "Port": $}, "H7": {"IP": "192.168.2.7 "," username ":" 777 "," Password ":" GGG "," Port ": $}," H8 ": {" 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!

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.

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.