-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_tool.py
140 lines (108 loc) · 3.3 KB
/
net_tool.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import requests
import os
import os.path
from flask import Flask, request, redirect
from flask.ext.autodoc import Autodoc
import urllib
import socket
import time
# The following are imported so that these are
# in debug context when needed
import netifaces
import sh
app = Flask(__name__)
auto = Autodoc(app)
port = os.getenv('VCAP_APP_PORT', os.getenv('PORT', '5000'))
tempdir = os.getenv('TMPDIR', '/tmp')
INFO_URL = "https://github.com/status.json"
def is_running(pid):
try:
os.kill(pid, 0)
return True
except OSError:
return False
def remove_params_and_redirect(params):
ldict = {k: v for k, v in request.args.iteritems() if k not in params}
return redirect("{}?{}".format(request.path, urllib.urlencode(ldict)))
NOCACHE = "_nocache"
def run_async(shCmd, host, **kwargs):
"""
Run an sh command in async mode
This takes care of _nocache flag and meta refresh
so any command can be run this way
"""
hostfile = tempdir + '__{}__'.format(shCmd._path.replace('/', '_')) + host
pidfile = hostfile + ".pid"
refresh = """<meta http-equiv="refresh" content="3">"""
redo = """<a href="{}?{}=1"> Rerun </a>""".format(request.path, NOCACHE)
if not os.path.isfile(hostfile) or request.args.get(NOCACHE):
cmd = shCmd(host, _bg=True, _out=hostfile, **kwargs)
with open(pidfile, "wt") as fl:
print >> fl, cmd.pid
if request.args.get(NOCACHE):
return remove_params_and_redirect([NOCACHE])
with open(hostfile, "rt") as fl:
resp = "<pre>" + fl.read() + "</pre>"
proc_start = os.stat(pidfile).st_ctime
proc_now = os.stat(hostfile).st_mtime
with open(pidfile, "rt") as fl:
pid = int(fl.read())
if is_running(pid):
cmdtime = int(time.time()) - proc_start
resp = (refresh +
"\nRunning with pid {}, {} sec \n".format(pid, cmdtime) +
resp)
else:
cmdtime = proc_now - proc_start
resp = redo + " ({} sec)\n".format(cmdtime) + resp
return resp
@app.route('/get/<url>')
@app.route('/get')
@auto.doc()
def get(url=None):
"""
attempt an HTTP GET request to the specified url
"""
if not url:
url = request.args.get("url")
if not url.startswith("http"):
url = "http://" + url
return ("url: " + url + "\n<p>response:" +
requests.get(url, timeout=5.0, verify=False).text)
@app.route('/resolve/<host>')
@auto.doc()
def resolve(host):
"""
resolve host using socket
"""
return socket.gethostbyname(host)
@app.route('/traceroute/<host>')
@auto.doc()
def traceroute(host):
"""
run platform traceroute in asynchronous mode
"""
from sh import traceroute as shCmd
return run_async(shCmd, host)
@app.route('/ping/<host>')
@auto.doc()
def ping(host):
"""
run platform ping in asynchronous mode
"""
from sh import ping as shCmd
return run_async(shCmd, host, c="10")
@app.route('/dig/<host>')
@auto.doc()
def dig(host):
"""
run platform dig in asynchronous mode
"""
from sh import dig as shCmd
return run_async(shCmd, host)
@app.route('/')
def health():
return auto.html()
if __name__ == "__main__":
print "Starting on port =", port
app.run(host='0.0.0.0', port=int(port), debug=True)