-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetc_services.py
127 lines (118 loc) · 3.62 KB
/
etc_services.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
#
# Copyright (C) 2007 Red Hat, Inc.
# Authors:
# Thomas Woerner <[email protected]>
#
# 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, see <http://www.gnu.org/licenses/>.
#
ETC_SERVICES = "/etc/services"
def isNumber(string):
try:
i = int(string)
except ValueError:
return 0
else:
return 1
class _Service:
def __init__(self):
self.clear()
def clear(self):
self.p_id = 0
self.p_protocol = ""
self.p_name = ""
self.p_description = ""
self.p_aliases = [ ]
def setID(self, id):
self.p_id = id
def getId(self):
return self.p_id
def setProtocol(self, protocol):
self.p_protocol = protocol
def getProtocol(self):
return self.p_protocol
def setName(self, name):
self.p_name = name
def getName(self):
return self.p_name
def setDescription(self, description):
self.p_description = description
def getDescription(self):
return self.p_description
def setAliases(self, aliases):
self.p_aliases = aliases
def getAliases(self):
return self.p_aliases
def __str__(self):
s = "%s\t%d/%s" % (self.getName(), self.getId(), self.getProtocol())
if len(self.getAliases()) > 0:
s += "\t%s" % " ".join(self.getAliases())
if self.getDescription() != "":
s += "\t# %s" % self.getDescription()
return s
__repr__ = __str__
class _Services(list):
def __init__(self):
list.__init__(self)
self.load()
def load(self):
try:
fd = open(ETC_SERVICES, "r")
except Exception, msg:
print msg
return
for line in fd.xreadlines():
if not line: break
if len(line) < 1 or line[0] == '#':
continue
line = line.strip()
# remove all after '#'
p = line.split("#")
if len(p) < 1:
continue
line = p[0]
if len(p) > 1:
description = p[1].strip()
else:
description = None
# remove empty lines
if len(line) < 1: continue
# remove entries without service name and port/protocol
p = line.split()
if len(p) < 2:
continue
# new service
service = _Service()
# set name and description
service.setName(p[0])
if description != None:
service.setDescription(description)
# port and protocol?
p2 = p[1].split("/")
if len(p2) < 2:
continue
# convert to port id
try:
id = int(p2[0])
except ValueError:
continue
else:
service.setID(id)
# set protocol
service.setProtocol(p2[1])
# append aliases
service.setAliases(p[2:])
# append service
self.append(service)
fd.close()
services = _Services()