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
163
164
165
166
167
|
#!/usr/bin/python
"""net_monitor: wifi monitoring"""
import os
import socket
import fcntl
import struct
import traceback
import array
# native librari implements a few bits
import _native
class Monitor:
# based on http://svn.pardus.org.tr/pardus/tags/pardus-1.0/system/base/wireless-tools/comar/link.py
# wireless IOCTL constants
SIOCGIWMODE = 0x8B07 # get operation mode
SIOCGIWRATE = 0x8B21 # get default bit rate
SIOCGIWESSID = 0x8B1B # get essid
# wireless modes
modes = ['Auto', 'Ad-Hoc', 'Managed', 'Master', 'Repeat', 'Second', 'Monitor']
def __init__(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.net = {}
def ioctl(self, func, params):
return fcntl.ioctl(self.sock.fileno(), func, params)
def wifi_ioctl(self, iface, func, arg=None):
"""Prepares some variables for wifi and runs ioctl"""
if not arg:
data = (iface + '\0' * 32)[:32]
else:
data = (iface + '\0' * 16)[:16] + arg
try:
res = self.ioctl(func, data)
return res
except:
return None
def wifi_get_max_quality(self, iface):
"""Gets maximum quality value"""
return _native.wifi_get_max_quality(iface)
def wifi_get_ap(self, iface):
"""Gets access point address"""
return _native.wifi_get_ap(iface)
def wifi_get_essid(self, iface):
"""Get current essid for an interface"""
buffer = array.array('c', '\0' * 16)
addr, length = buffer.buffer_info()
arg = struct.pack('Pi', addr, length)
self.wifi_ioctl(iface, self.SIOCGIWESSID, arg)
return buffer.tostring().strip('\0')
def wifi_get_mode(self, iface):
"""Get current mode from an interface"""
result = self.wifi_ioctl(iface, self.SIOCGIWMODE)
if not result:
return _("Unknown")
mode = struct.unpack("i", result[16:20])[0]
return self.modes[mode]
def wifi_get_bitrate(self, iface):
"""Gets current operating rate from an interface"""
# Note: KILO is not 2^10 in wireless tools world
result = self.wifi_ioctl(iface, self.SIOCGIWRATE)
if result:
size = struct.calcsize('ihbb')
m, e, i, pad = struct.unpack('ihbb', result[16:16+size])
if e == 0:
bitrate = m
else:
bitrate = float(m) * 10**e
return bitrate
else:
return 0
def get_status(self, ifname):
try:
fd = open("/sys/class/net/%s/operstate" % ifname)
status = fd.readline().strip()
fd.close()
except:
status="unknown"
if status == "unknown":
# pretty-format interface status
status = _("Unknown")
return status
def wireless_stats(self):
"""Check if device is wireless and get its details if necessary"""
try:
stats = {}
with open("/proc/net/wireless") as fd:
ifaces = fd.readlines()[2:]
for line in ifaces:
line = line.strip()
if not line:
continue
iface, params = line.split(":", 1)
iface = iface.strip()
params = params.replace(".", "").split()
link = int(params[1])
stats[iface] = link
return stats
except:
# something bad happened
traceback.print_exc()
return {}
def has_wireless(self, iface):
"""Checks if device has wireless capabilities"""
return os.access("/sys/class/net/%s/wireless" % iface, os.R_OK)
def get_address(self, ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
mac=_("No physical address")
# ip address
try:
addr=socket.inet_ntoa(fcntl.ioctl( s.fileno(), 0x8915, struct.pack('256s', ifname[:15]))[20:24])
except:
addr=_("No address assigned")
# mac address
try:
mac_struct=fcntl.ioctl( s.fileno(), 0x8927, struct.pack('256s', ifname[:15]))[18:24]
mac=":".join(["%02x" % ord(char) for char in mac_struct])
except:
addr=_("No address assigned")
# addr, mac
return addr, mac
def readnet(self):
"""Reads values from /proc/net/dev"""
net = {}
data = open("/proc/net/dev").readlines()[2:]
for l in data:
dev, vals = l.split(":")
dev = dev.strip()
vals = vals.split()
net[dev] = vals
return net
def get_traffic(self, iface, net=None):
if not net:
if not self.net:
self.readnet()
net = self.net
if iface in net:
bytes_in = int(net[iface][0])
bytes_out = int(net[iface][8])
else:
bytes_in = 0
bytes_out = 0
return bytes_in, bytes_out
def format_size(self, size):
"""Pretty-Formats size"""
return size
|