Python script implements cluster detection and management functions _python

Source: Internet
Author: User

The scene is like this: a production room, there will be a lot of testing machines and production machines (that is, about 30 bar), because of the confusion of management, which machine was used and which machine was not used by anyone, it created an idea-to use a machine to manage all the machines, to record the responsible person for the equipment, Device usage status, etc. information .... So why choose Python,python is simple enough and has rich Third-party library support.

The original idea

Since just joined the work soon, to these things have not been contacted, round Hillock to a department need to do something to (the project is what is not the case, must make things, no way to bite the bullet to think of ideas) ...

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

So do something, often find colleagues ask 208 who use? 201 Who's using it? That IP is my!!!. Did you unplug my network cable? 242 which is the machine?

Suddenly, spring is coming, is it possible to do a system used to detect the use of IP and recording devices, and even to run a script or command on a device as needed? Put 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 on all servers, you need to deploy an agent on any server as a node for information, sending server information data to the Management Server node at timed intervals.
2. Server, as an integrated management node, receives and stores information submitted by the agent.
3. For convenience, use the form of Web pages to do the display.

Development tool Selection

1. Development language: Python
The choice of Python, simple, third-party library rich, do not build wheels
2. Database: MySQL
Simple, Easy-to-use
3. Webpy:web Framework
Easy to get started and easy to deploy
4. Bootstrap: Front End frame
Don't care too much about front-end problems
5. Paramiko:python Library, following the SSH2 protocol, supports remote server connections in an encrypted and authenticated manner
Connecting the agent server via SSH: Remotely running commands, transferring 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: Connecting MySQL
8. Shell and Python scripting interfaces: interfaces that provide shell scripting for others

Experience sharing

1. Front end for me is a new thing, has never been done, the page animation effect, script run-time transition are to be considered, start to consider the use of the countdown, but this time is not controllable, and then use Ajax to deal with this problem
2. Agent to automatically deploy to each machine, and can control the refresh time through the server
3. It is important to establish an extensible table, and some important information needs to be written to disk, and data can be obtained from disk in the event of a database failure
4. Database connection, if there is no operation for a long time will timeout, to take into account
... ...

Project Structure--webpy

1. website.py is the main program for webpy, URL mapping is set
2. model.py is a webpy URL mapping class that handles requests and returns
3. Static Resource Storage
4. Scripts used to store the processing of the script, there are some problems with the name

Connecting to a database

Use MYQSLDB to connect to MySQL, where I don't use the database interface provided by WEBPY, but I encapsulate a set of

SSH Remote connection server

Paramiko implement SSH connectivity, data transfer, execute commands, and scripts

Copy Code code as follows:

def executecmd (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)
Except Exception as E:
S.close ()
Print E
print ' connet error ... '
Return

Try
Stdin,stdout,stderr=s.exec_command (CMD)
#print ' Host:%s ... '%host
res = Stdout.readlines ()
Except Exception as E:
print ' Exec_commmand error ... '
S.close ()
return res

def executefile (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)
Except Exception as E:
S.close ()
Print E
print ' connet error ... '
Return ""

Try
filename = os.path.basename (file)
If Filename.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 ')
Except Exception as E:
S.exec_command (' rm-rf/tmp/tmp_test* 2>/dev/null ')
print ' Timeout error ... '
Print E
Return ""
return res

IP Scan

Using Scapy for IP scanning

Copy Code code as follows:

def pro (IP, CC, handle):
Global Dict
DST = IP + str (cc)
Packet = IP (DST=DST, ttl=20)/icmp ()
Reply = SR1 (packet, timeout=timeout)
If reply:
Print Reply.src, ' is online '
TMP = [1, reply.src]
Handle.write (reply.src + ' \ n ')
#handle. Write (reply.src+ "is online" + "\ n")

def main ():
Threads=[]
ip = ' 192.168.1.1 '
s = 2
E = 254
F=open (' Ip.log ', ' W ')
For I in range (S, e):
T=threading. Thread (target=pro,args= (ip,i,f))
Threads.append (t)
Print "Main Thread begins at", CTime ()
For T in Threads:
T.start ()
For T in Threads:
T.join ()
Print "Main Thread ends at", CTime ()

Bulk Add Ssh-key

Copy Code code as follows:

Home_dir = '/home/xx '
id_rsa_pub = '%s/.ssh/id_rsa.pub '%home_dir

If not id_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 ()


def up_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:%s@%s auth success!\n '% (user, host)
S.close ()
T.close ()
Except Exception, 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

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.