]>
git.datanom.net - netconf.git/blob - netconf
6 process
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
, shell
=True)
7 output
, error
= process
.communicate();
9 return (output
.rstrip(), error
.rstrip())
11 class ParserError(Exception):
15 """ Parse nic information """
18 self
.__dladm
1 = "dladm show-phys -p -o link,media | sed 's/:/ /g'"
19 self
.__dladm
2 = "dladm show-phys -p -m -o link,address,inuse | sed 's/:/ /g' | sed 's/\\\[[:space:]]/:/g' | sed 's/\\\//g'"
21 def __parse_nics(self
, text
):
23 for line
in text
.splitlines():
25 #if parts[1] in ('Ethernet', 'Infiniband'):
26 if parts
[1] in ('Ethernet'):
27 nics
[parts
[0]] = [parts
[0], parts
[1]]
31 def __parse_macs(self
, obj
, text
):
34 for line
in text
.splitlines():
40 obj
[parts
[0]].append(parts
[1])
43 for k
, v
in obj
.items():
49 (nics
, error
) = runcmd(self
.__dladm
1)
51 raise ParserError(error
)
53 (macs
, error
) = runcmd(self
.__dladm
2)
55 raise ParserError(error
)
57 return self
.__parse
_macs
(self
.__parse
_nics
(nics
), macs
)
59 def get_terminal_width():
61 if sys
.version_info
>= (2,7):
62 width
= int(subprocess
.check_output(['tput', 'cols']))
64 (out
, err
) = runcmd('tput cols')
69 print("Invalid Command 'tput cols': exit status ({1})".format(e
.errno
))
70 except subprocess
.CalledProcessError
as e
:
71 print("Command 'tput cols' returned non-zero exit status: ({1})".format(e
.returncode
))
75 def make_menu(interfaces
):
78 sys
.stderr
.write("\x1b[2J\x1b[H")
79 cols
= get_terminal_width()
80 intro
= 'The following unconfigured interfaces was discovered'
81 fill
= (cols
/ 2) - (len(intro
) / 2)
82 print '{0:^{cols}}'.format('Simple network interface configuration tool', cols
=cols
)
84 print '{0:<{fill}}{1}'.format('', intro
, fill
=fill
)
85 print '{0:<{fill}}{1:>2} {2:^15} {3:^10} {4:^17}'.format('','#','Interface','Media','MAC',fill
=fill
)
86 print '{0:<{fill}}{1:-^2} {2:-^15} {3:-^10} {4:-^17}'.format('','','','','',fill
=fill
)
90 print '{0:<{fill}}{1:>2} {2:<15} {3:<10} {4:>17}'.format('',n
,i
[0],i
[1],i
[2],fill
=fill
)
93 print '{0:<{fill}}{1:<}'.format('','select interface number to configure:',fill
=fill
),
94 nic
= int(raw_input())
95 if nic
>= n
or nic
< 0:
96 print '{0:<{fill}}{1:<} {2:<}'.format('','Error: Interface: 0 <= # <',n
,fill
=fill
),
97 raw_input(' << Hit any key >>')
104 ip
= mask
= gw
= None
105 nettype
= raw_input('dhcp or static [dhcp]: ').lower()
106 if nettype
== 'static':
108 ip
= raw_input('Enter IP: ')
109 mask
= raw_input('Enter netmask [24]: ')
112 gw
= raw_input('Enter default route [0=no]: ')
121 interfaces
= p
.parse()
123 nic
= make_menu(interfaces
)
125 (ip
,mask
,gw
) = get_config()
126 cmd
= 'ipadm delete-if %s' % nic
[0]
128 cmd
= 'ipadm create-if %s' % nic
[0]
129 (out
, err
) = runcmd(cmd
)
131 raise RuntimeError(err
)
134 cmd
= 'ipadm create-addr -T dhcp %s/v4' % nic
[0]
135 (out
, err
) = runcmd(cmd
)
137 raise RuntimeError(err
)
140 cmd
= 'ipadm create-addr -T static -a %s/%s %s/v4' % (ip
, mask
, nic
[0])
141 (out
, err
) = runcmd(cmd
)
143 raise RuntimeError(err
)
145 cmd
= 'route -p add default %s' % gw
146 (out
, err
) = runcmd(cmd
)
148 raise RuntimeError(err
)
149 cmd
= 'netstat -rn -finet'
150 (out
, err
) = runcmd(cmd
)
152 raise RuntimeError(err
)
153 print 'New route table'
155 dns
= raw_input('Configure DNS [n]? ').lower()
156 if not dns
or dns
== 'n':
162 dns
= raw_input('Enter nameserver: ')
163 cmd
= "echo 'nameserver %s' >> /etc/resolv.conf" % dns
164 cmd
+= '&& cp /etc/nsswitch.conf{,.bak}'
165 cmd
+= '&& cp /etc/nsswitch.{dns,conf}'
166 (out
, err
) = runcmd(cmd
)
168 raise RuntimeError(err
)
170 print "Found no unassigned interfaces"
171 except ParserError
as e
:
172 print 'Parse Errror: %s' % e
173 except RuntimeError as e
:
174 print 'Runtime Error: %s' % e
176 if __name__
== '__main__':
179 except KeyboardInterrupt:
180 sys
.stderr
.write("\x1b[2J\x1b[H")
This page took 0.17248 seconds and 6 git commands to generate.