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
|
# Copyright (C) 2007, Eduardo Silva <edsiper@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import socket
import fcntl
import struct
import string
class NetDevice(object):
def __init__(self):
self._dev = self.get_interfaces()
def get_interfaces(self):
netdevfile = "/proc/net/dev"
dev = []
try:
infile = file(netdevfile, "r")
except:
print "Error trying " + netdevfile
skip = 0
for line in infile:
# Skip first two lines
skip += 1
if skip <= 2:
continue
iface = string.split(line, ":",1)
arr = string.split(iface[1])
if len(arr) < 10:
continue
info = {'interface': iface[0].strip(), \
'bytes_recv': arr[0],\
'bytes_sent': arr[8],\
'packets_recv': arr[1],
'packets_sent': arr[9]}
dev.append(info)
return dev
def get_iface_info(self, ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
hwaddr = []
try:
ip = socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, \
struct.pack('256s', ifname[:15]))[20:24])
except:
ip = None
try:
netmask = socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x891b, \
struct.pack('256s', ifname[:15]))[20:24])
except:
netmask = None
try:
mac = []
info = fcntl.ioctl(s.fileno(), 0x8927, \
struct.pack('256s', ifname[:15]))
for char in info[18:24]:
hdigit = hex(ord(char))[2:]
if len(hdigit):
mac.append(hdigit)
except:
mac = None
mac_string = self.mac_to_string(mac)
return [ip, netmask, mac_string]
def mac_to_string(self, hexa):
string = ''
for value in hexa:
if len(string)==0:
string = value
else:
string += ':'+value
return string
|