-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpn
executable file
·84 lines (70 loc) · 1.88 KB
/
vpn
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
#!/usr/bin/env python
nodes_n2n = {
"192.168.2.32": "opsbm01",
"192.168.2.33": "sshtmc-laptop",
"192.168.2.34": "dell7",
}
nodes_hamachi = {
"5.42.83.186": "dell7",
"5.113.217.242": "opsbm01",
"5.124.120.170": "dell02",
}
import subprocess
from sys import stdout
if stdout.isatty():
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
else:
HEADER = ''
OKBLUE = ''
OKGREEN = ''
WARNING = ''
FAIL = ''
ENDC = ''
def execCmd(command, sudo=False, cwd=None, infile=None, outfile=None,
data=None, raw=False, env=None):
"""
Executes an external command, optionally via sudo.
"""
if sudo:
if isinstance(command, types.StringTypes):
command = " ".join([constants.EXT_SUDO, SUDO_NON_INTERACTIVE_FLAG, command])
else:
command = [constants.EXT_SUDO, SUDO_NON_INTERACTIVE_FLAG] + command
if infile == None:
infile = subprocess.PIPE
if outfile == None:
outfile = subprocess.PIPE
p = subprocess.Popen(command, close_fds=True, cwd=cwd,
stdin=infile, stdout=outfile, stderr=subprocess.PIPE,
env=env)
(out, err) = p.communicate(data)
if out == None:
# Prevent splitlines() from barfing later on
out = ""
if not raw:
out = out.splitlines(False)
err = err.splitlines(False)
return (p.returncode, out, err)
def nodestatus(nds):
up = []
down = []
for k,v in nds.iteritems():
ret, out, err = execCmd(["ping", "-c1", "-w1", k])
if ret == 0:
up.append((k,v))
else:
down.append((k,v,))
for k,v in up:
print OKGREEN, "%s\t%s\t# Up" % (k, v), ENDC
for k,v in down:
print FAIL, "%s\t%s\t# Down" % (k, v), ENDC
if __name__ == "__main__":
print "# N2N nodes"
nodestatus(nodes_n2n)
print "# Hamachi nodes"
nodestatus(nodes_hamachi)