Skip to main content
Homepage
Explore
Search or go to…
/
Register
Sign in
Explore
Primary navigation
Project
H
hostinfo
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Redmine
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Collapse sidebar
Snippets
Groups
Projects
Show more breadcrumbs
Anders Blomdell
hostinfo
Commits
fc4f1175
Commit
fc4f1175
authored
8 months ago
by
OskarStenberg
Browse files
Options
Downloads
Patches
Plain Diff
Missing file...
parent
ee8bebe9
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/hostinfo/nettools.py
+91
-0
91 additions, 0 deletions
src/hostinfo/nettools.py
with
91 additions
and
0 deletions
src/hostinfo/nettools.py
0 → 100644
+
91
−
0
View file @
fc4f1175
import
sys
import
os
import
hostinfo.util
as
util
# File syntax is according to nettools manual:
# IP-adress;hostnamn;kommasepareradeMAC-adresser;DHCP-grupp;LucatID;kommentar
# Where IP-adress and hostnamn are not optional
# There are a few reserved IPs for each network, for now these seem to be standardized as:
RESERVED_IPS
=
{
'
1
'
:
'
gateway
'
,
'
2
'
:
'
ldc-admin
'
,
'
3
'
:
'
ldc-admin
'
,
'
254
'
:
'
ldc-monitoring
'
,
'
255
'
:
'
broadcast
'
,
}
def
generate_row
(
ip
,
hostname
,
maclist
=
[],
dhcp_group
=
""
,
owner
=
""
,
comment
=
""
):
if
not
ip
:
#or not hostname:
raise
util
.
HostinfoException
(
"
Ip and hostname are non-optional
"
,
where
=
ip
if
not
ip
else
hostname
)
if
type
(
maclist
)
is
list
:
maclist
=
'
,
'
.
join
(
maclist
)
pass
template
=
(
"""
%s;%s;%s;%s;%s;%s
"""
%
(
ip
,
hostname
,
maclist
,
dhcp_group
,
owner
,
comment
))
return
template
def
generate_row_tup
(
host
):
h
=
list
(
host
)
# Ensure proper length of tuple to unpack
h
=
h
+
[
""
]
*
(
6
-
len
(
h
))
ip
,
hostname
,
maclist
,
dhcp_group
,
owner
,
comment
=
h
return
generate_row
(
ip
,
hostname
,
maclist
,
dhcp_group
,
owner
,
comment
)
def
filter_nets
(
tree
,
options
,
ip
):
net
=
util
.
get_subnet
(
tree
,
util
.
address
(
ip
))
if
net
:
return
(
net
.
nettools
[
0
]
is
not
None
and
ip
.
split
(
'
.
'
)[
-
1
]
not
in
RESERVED_IPS
)
return
False
def
emit_hosts
(
tree
,
options
):
result
=
[]
for
iface
in
tree
.
_host_
.
_interface_
:
for
ip
in
iface
.
_ip_
:
if
ip
and
ip
.
address
[
0
]:
result
.
append
((
ip
.
address
[
0
],
iface
.
name
[
1
],
[
iface
.
ethernet
[
0
]
or
""
]))
pass
pass
pass
result
=
list
(
filter
(
lambda
e
:
filter_nets
(
tree
,
options
,
e
[
0
]),
result
))
return
result
def
subnet_hosts
(
tree
,
options
):
nets
=
[
util
.
network
(
s
)
for
s
in
tree
.
_subnet_
if
s
.
nettools
[
0
]
is
not
None
]
hosts
=
[
str
(
h
)
for
n
in
nets
for
h
in
n
.
hosts
()]
return
list
(
filter
(
lambda
e
:
filter_nets
(
tree
,
options
,
e
),
hosts
))
def
generate
(
tree
,
options
):
hosts
=
emit_hosts
(
tree
,
options
)
expected_hosts
=
subnet_hosts
(
tree
,
options
)
for
eh
in
expected_hosts
:
exists
=
False
for
h
in
hosts
:
if
h
[
0
]
==
eh
:
exists
=
True
break
pass
if
not
exists
:
hosts
.
append
((
eh
,
""
))
#print("Appended", eh)
pass
pass
hosts
.
sort
(
key
=
lambda
e
:
[
int
(
v
)
for
v
in
e
[
0
].
split
(
'
.
'
)])
output
=
[]
for
h
in
hosts
:
output
.
append
(
generate_row_tup
(
h
))
return
'
\n
'
.
join
(
output
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment