X-Git-Url: http://git.datanom.net/netconf.git/blobdiff_plain/0b4b929c889fb8f9ae1f0dab4c1fea029512a6b5..c050b028d64ee733e3a7e95c92505a81ace8ed70:/netconf diff --git a/netconf b/netconf index 2f0694f..3dd1d37 100644 --- a/netconf +++ b/netconf @@ -146,7 +146,8 @@ def usage(): -g, --gateway Default gateway. Optional. -i, --interface Interface to configure. -m, --netmask Netmask to use for interface. Default /24. - -n, --nameserver Nameserver to use. Optional.""" + -n, --nameserver Nameserver to use. Optional. + -r, --record Output create commands to stdout.""" def parse_input(): options = () @@ -182,26 +183,37 @@ def parse_input(): record = True else: assert False, 'Unhandled option' - - if not address or not interface: - print 'Error: missing options' - usage() - sys.exit(2) + + if (record): + if (bool(address) ^ bool(interface)): + print 'Error: missing options' + usage() + sys.exit(2) + else: + if not address or not interface or error: + print 'Error: missing options' + usage() + sys.exit(2) + if address == '0': address = None if not netmask: netmask = '24' - options = (interface, address, netmask, gateway, nameserver) + options = (interface, address, netmask, gateway, nameserver, record) return options def main(): interactive = True + record = False + output = '' options = parse_input() if options: - interactive = False + record = options[5] + if (options[0]): + interactive = False p = Parser() try: @@ -228,12 +240,12 @@ def main(): gw = options[3] cmd = 'ipadm delete-if %s' % nic[0] if record: - print cmd + output += cmd else: runcmd(cmd) cmd = 'ipadm create-if %s' % nic[0] if record: - print cmd + output += "\n" + cmd else: (out, err) = runcmd(cmd) if err: @@ -242,7 +254,7 @@ def main(): # use DHCP cmd = 'ipadm create-addr -T dhcp %s/v4' % nic[0] if record: - print cmd + output += "\n" + cmd else: (out, err) = runcmd(cmd) if err: @@ -251,7 +263,7 @@ def main(): # use STATIC cmd = 'ipadm create-addr -T static -a %s/%s %s/v4' % (ip, mask, nic[0]) if record: - print cmd + output += "\n" + cmd else: (out, err) = runcmd(cmd) if err: @@ -259,7 +271,7 @@ def main(): if gw: cmd = 'route -p add default %s' % gw if record: - print cmd + output += "\n" + cmd else: (out, err) = runcmd(cmd) if err: @@ -288,11 +300,13 @@ def main(): cmd += '&& cp /etc/nsswitch.conf{,.bak} ' cmd += '&& cp /etc/nsswitch.{dns,conf}' if record: - print cmd + output += "\n" + cmd else: (out, err) = runcmd(cmd) if err: raise RuntimeError(err) + if record: + print output else: print "Found no unassigned interfaces" except ParserError as e: