-
Notifications
You must be signed in to change notification settings - Fork 0
/
Host.py
162 lines (148 loc) · 6.13 KB
/
Host.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# -*- coding: utf-8 -*-
#
# Representation of a host
#
import os, threading
from Node import *
from IP_address import *
from locator import Base, simulate
if Base != object.__class__:
from sqlalchemy import Column, Integer, String
class Host(Node, Base):
if Base != object.__class__:
__tablename__ = 'host'
ident = Column(String, primary_key=True)
netmask = Column(String)
interface = Column(String)
x = Column(Integer)
y = Column(Integer)
# ipaddr is an instance of the class IP_address,
# while ip represents the "real" ip of the host
# we'll expose in the *features lists only ip
read_features = ["ip", "netmask", "network", "interface"]
features = ["ip", "netmask", "interface"]
tiny_pixbuf = gtk.gdk.pixbuf_new_from_xpm_data([
"26 20 5 1",
" c black",
". c grey",
"o c yellow",
"X c blue",
"- c None",
"--------------------------",
"-........................-",
"-........................-",
"-..XXXXXXXXXXXXXXXXXXXX..-",
"-..XXXXXXXXXXXXXXXXXXXX..-",
"-..XXXXXXXXXXXXXXXXXXXX..-",
"-..XXXXXXXXXXXXXXXXXXXX..-",
"-..XXXXXXXXXXXXXXXXXXXX..-",
"-..XXXXXXXXXXXXXXXXXXXX..-",
"-..XXXXXXXXXXXXXXXXXXXX..-",
"-..XXXXXXXXXXXXXXXXXXXX..-",
"-........................-",
"-........................-",
"---------........---------",
"--------..........--------",
"---....................---",
"---. . . . . . . . . ..---",
"--- ---",
"---. . . . . . . . . . ---",
"--- ---",
"---. . . . . . . . . ..---"
])
def __init__(self, name=None, ipaddr=None, netmask="24", interface="wlan0", x=50, y=50, ident=None, gui=None):
Node.__init__(self, name, "Host", x, y, ident, gui)
if ipaddr == None:
ipaddr = gui.get_new_node("IP_address", None, None, x - 30, y)
gui.connect(self, ipaddr)
assert(ipaddr != None)
self.ipaddr = ipaddr
self.ip = self.ipaddr.getIp()
self.network = self.get_network(self.ip)
self.netmask = netmask
self.interface = interface
self.find_neighbors_script = "./script_ip.sh"
self.refresh_data_script = "./refresh_data.sh"
self.find_up_script = "./get_up.sh"
self.find_up_script_real = "./get_up_real.sh"
def get_network(self, ip):
return ".".join(ip.split(".")[:-1]) + ".0"
def get_changes(self, widget, event, window, textboxes):
for elem in textboxes:
label = elem[0]
text = elem[1]
# If we are modifying the ip, be sure to refresh also the
# data in the IP_address structure
if label == 'ip':
self.ipaddr.setIp(text)
setattr(self, str(label.get_text()), str(text.get_text()))
# Refresh representation
self.Label.set_text(self.__repr__())
# Drop everything now; window hide will drop all textboxes
window.hide_all()
def node_clicked(self, widget, event):
# If right-click
if event.button == 3:
newmenu = gtk.Menu()
newitem = gtk.MenuItem('Find neighbors (ip)')
newmenu.append(newitem)
newitem.connect("button-press-event", self.find_neighbors)
newitem1 = gtk.MenuItem('Refresh data')
newmenu.append(newitem1)
newitem1.connect("button-press-event", self.refresh_data)
newitem2 = gtk.MenuItem('Find up hosts')
newmenu.append(newitem2)
newitem2.connect("button-press-event", self.find_up)
if not self.gui.node_connected_with_class(self, "WebServer"):
item_remove = gtk.MenuItem('Remove')
newmenu.append(item_remove)
item_remove.connect("button-press-event", self.disappear)
newmenu.show_all()
newmenu.popup(None, None, None, event.button, event.time)
def disappear(self, widget=None, event=None):
self.ipaddr.disappear()
super(Host, self).disappear()
# Refresh host information with the current machine's
def refresh_data(self, widget, event):
out = self.runProcess([self.refresh_data_script, self.interface])
if out == "":
return
self.ip = str(out).strip().split()[0]
self.ipaddr.setIp(str(out).strip().split()[0])
self.network = self.get_network(self.ip)
self.netmask = str(out).strip().split()[1]
def find_connect_node(self, newid):
# Search for IP_address instance and eventually create a new one
ipaddr = self.gui.search_for_node_with_class("IP_address", "ip", newid)
if ipaddr == None:
ipaddr = self.gui.get_new_node("IP_address", "ip", newid, self.x - 30, self.y)
# Create new host
classname = self.__class__.__name__
neigh = self.gui.search_for_node_with_class(classname, "ipaddr", ipaddr)
if neigh == None:
neigh = self.gui.get_new_node(classname, "ipaddr", ipaddr, self.x, self.y)
self.gui.connect(neigh, ipaddr)
self.gui.connect(self, neigh)
# Find up hosts
def find_up(self, widget, event):
if os.geteuid() != 0:
print "This functionality needs root privileges"
return
def my_thread(obj):
# FIXME: locking!
print "Exploring " + self.network + "/" + self.netmask + "..."
if simulate:
out = obj.runProcess([self.find_up_script, self.network, self.netmask])
else:
out = obj.runProcess([self.find_up_script_real, self.network, self.netmask])
print "... done."
for newip in str(out).strip().split():
print "Found " + newip
obj.find_connect_node(newip)
threading.Thread(target=my_thread, args=(self,)).start()
# Search by ip
def find_neighbors(self, widget, event):
out = self.runProcess([self.find_neighbors_script])
for newid in str(out).strip().split():
self.find_connect_node(newid)
# vim: set et sts=4 sw=4: