Python script for cluster detection and management

Source: Internet
Author: User
Tags creative commons attribution python script

Python script for cluster detection and management

The scene is this: a production room, there will be a lot of test machines and production machines (that is, about 30), because the management of the confusion caused by which machine is used, which machine no one is not clear, resulting in an idea-to use a machine to manage all the machines, record equipment responsible person, Device usage status and more .... So why choose Python,python is simple enough and has a rich third-party library support.

The original idea

As a result of the new job soon, there is no contact with these things, rotation to a department needs to make something (the project is nothing, it is necessary to make things, no way to bite the bullet think of ideas) ...

I would like to do a simple point of automated testing tools, but this project test methods and test cases temporarily can not use this generic test method (input and output are not OK), so forget ...

  

  

Then do something, often find colleagues ask 208 who use? 201 Who's using it? That IP is mine!!! Did you unplug my cable? 242 which one is that machine?

Suddenly, spring is coming, is it possible to do a system to detect the IP and recording device users, or even as needed to run a script or command on a device? This short dwarfish poor idea and leader after communication, confirm can do, then start it!!!

Design ideas

The approximate idea of the system:

1. To obtain a variety of information for all servers, you need to deploy an agent on any server as a node for information acquisition, and periodically send server information data to the Management Server node.

2. Server receives and stores information submitted by the agent as an integrated management node.

3. In order to facilitate the use of Web pages in the form of display.

  

Development tool Selection

1. Development language: Python

Choose Python, simple, third-party library rich, no wheels to build

2. Database: MySQL

Simple, easy to use

3. Webpy:web Framework

Easy to get started, easy to deploy

4. Bootstrap: Front-end frame

Don't care too much about front end problems

5. Paramiko:python Library, follow the SSH2 protocol, support the remote server connection in the way of encryption and authentication

Connect to the agent server via SSH: Run commands remotely, transfer files

6. Scapy:python Library, which can be used to send, sniff, parse and forge network packets, which are used to scan IP

7. MySQLdb: Connect MySQL

8. Shell and Python scripting interface: an interface that provides shell scripts for others

Experience sharing

1. Front end for me is a new thing, has never been done, the page animation effect, the script runtime transition is to consider, began to consider the use of the countdown, but this time is not controllable, and then use Ajax to deal with the problem

2. Agent to automatically deploy to each machine, and can control the refresh time through the server

3. It is important to create an extensible table, and some important information needs to be written to the disk, and the data can be obtained from the disk in case of database failure

4. Database connection, if there is no operation for a long time will time out, to take into account

... ...

Project Structure--webpy

1. website.py is the main program of the webpy, set the URL mapping

2. model.py is a URL mapping class for webpy that handles requests and returns

3 Static resources in static

4. Scripts used to store the processing of the script, where the name of some problems

    

Connecting to a database

Using MYQSLDB to connect to MySQL, where I did not use the database interface provided by WEBPY, but I encapsulated a set of

SSH Remote connection server

Paramiko for SSH connection, data transfer, command and script execution

defExecutecmd (cmd, host, port=22, user='Root', passwd='Root'):    Try: S=Paramiko. Sshclient () S.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) S.connect (host, port, user, passwd, timeout= 10)    exceptException as E:s.close ()PrintePrint 'connet Error ...'        return     Try: Stdin,stdout,stderr=S.exec_command (cmd)#print ' Host:%s ... '%hostres =Stdout.readlines ()exceptException as E:Print 'Exec_commmand Error ...'s.close ()returnResdefExecutefile (file, host, port=22, user='Root', passwd='Root'):    Try: S=Paramiko. Sshclient () S.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) S.connect (host, port, user, Passwd,timeout=5) T=Paramiko. Transport ((host, Port)) T.connect (username=user, password=passwd) sftp=Paramiko. Sftpclient.from_transport (t)exceptException as E:s.close ()PrintePrint 'connet Error ...'        return "'    Try: FileName=os.path.basename (file)ifFilename.find ('. SH') >=0:sftp.put (Path+'/'+file,'/tmp/tmp_test.sh') Stdin,stdout,stderr=s.exec_command ('sh/tmp/tmp_test.sh 2>/dev/null', timeout=5)        Else: sftp.put (Path+'/'+file,'/tmp/tmp_test.py') Stdin,stdout,stderr=s.exec_command ('python/tmp/tmp_test.py', timeout=5)        #Stdin,stdout,stderr=s.exec_command (' rm-rf/tmp/tmp_test* 2>/dev/null ')res =stdout.readlines () S.exec_command ('rm-rf/tmp/tmp_test* 2>/dev/null')     exceptException as E:s.exec_command ('rm-rf/tmp/tmp_test* 2>/dev/null')         Print 'Timeout Error ...'        Printereturn "'    returnRes
View CodeIP Scan

IP scanning with scapy

defPro (IP, CC, handle):Globaldict DST= IP +str (cc) packet= IP (DST=DST, ttl=20)/ICMP () Reply= SR1 (Packet, timeout=TIMEOUT)ifReply:PrintREPLY.SRC,'is online'tmp= [1, Reply.src] Handle.write (reply.src+'\ n')        #Handle.write (reply.src+ "is online" + "\ n") defMain (): Threads=[] IP='192.168.1.1's= 2e= 254F=open ('Ip.log','W')     forIinchRange (S, e): t=threading. Thread (target=pro,args=(ip,i,f)) Threads.append (t)Print "Main Thread begins at", CTime () forTinchThreads:t.start () forTinchThreads:t.join ()Print "main Thread ends at", CTime ()
View CodeBulk Add Ssh-key
Home_dir ='/home/xx'id_rsa_pub='%s/.ssh/id_rsa.pub'%Home_dirif  notid_rsa_pub:Print 'id_rsa.pub Does not exist!'sys.exit (0) File_object= Open ('%s/.ssh/config'%home_dir,'W') File_object.write ('stricthostkeychecking no\n') File_object.write ('Userknownhostsfile/dev/null') File_object.close ()defUp_key (host,port,user,passwd):Try: S=Paramiko. Sshclient () S.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) S.connect (host, port, user, passwd) T=Paramiko. Transport ((host, Port)) T.connect (username=user, PASSWORD=PASSWD, timeout=3) sftp=Paramiko. Sftpclient.from_transport (t)Print 'create host:%s. SSH dir ...'%host Stdin,stdout,stderr=s.exec_command ('mkdir ~/.ssh/')        Print 'upload id_rsa.pub to host:%s ...'%host Sftp.put (Id_rsa_pub,"/tmp/temp_key") Stdin,stdout,stderr=s.exec_command ('cat/tmp/temp_key >> ~/.ssh/authorized_keys && rm-rf/tmp/temp_key')        Print 'host:%[email protected]%s Auth success!\n'%(user, host) s.close () t.close ()exceptException, E:#Import Traceback        #Traceback.print_exc ()        Print 'Connect Error ...'        Print 'Delete'+ Host +'From database ...'Delip (host)#Delete from mysql****        Try: S.close () t.close ()except:            Pass
View Code


This work is licensed under the Creative Commons Attribution-NonCommercial use-Share 3.0 non-localized version license agreement in the same way. Welcome reprint, please specify the source:
Reprinted from: Cococo Point http://www.cnblogs.com/coder2012

Python script for cluster detection and management

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.