-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·62 lines (52 loc) · 1.74 KB
/
server.py
File metadata and controls
executable file
·62 lines (52 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-
# Threaded xml-rpc server
import SocketServer
import xmlrpclib
from SimpleXMLRPCServer import SimpleXMLRPCServer,SimpleXMLRPCRequestHandler
# Ping, Traceroute
import subprocess
import re
# Threaded mix-in
class AsyncXMLRPCServer(SocketServer.ThreadingMixIn,SimpleXMLRPCServer): pass
error = "Адрес недоступен"
def ping(destination):
ping = subprocess.Popen(
["ping", "-c", "4", destination],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
)
average = re.search("round-trip min/avg/max/stddev = (\d+.\d+)/(\d+.\d+)/(\d+.\d+)/(\d+.\d+)", ping.stdout.read())
if average: result = average.group(2)
else: result = error
return result
def traceroute(destination):
traceroute = subprocess.Popen(
["traceroute", "-n", destination],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
)
#found = re.search("(\d+) (\d+.\d+.\d+.\d+) (\d+.\d+) ms (\d+.\d+) ms (\d+.\d+) ms", traceroute.stdout.read())
result = traceroute.stdout.read()
#if found: result = route
#else: result = error
print result
return result
def nmap(destination):
nmap = subprocess.Popen(
["nmap", destination],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
)
#route = re.search("Nmap done: (\d+) IP address ((\d+) host up) scanned in (\d+.\d+) seconds", nmap.stdout.read())
result = nmap.stdout.read()
print result
return result
# Instantiate and bind to localhost:8080
server = AsyncXMLRPCServer(('', 8080), SimpleXMLRPCRequestHandler)
print "Прослушивается порт 8080..."
server.register_function(ping, "ping")
server.register_function(traceroute, "traceroute")
server.register_function(nmap, "nmap")
# run!
print "Нажмите Control-C для выхода"
server.serve_forever()