]> git.datanom.net - netconf.git/blobdiff - netconf
test highlight
[netconf.git] / netconf
diff --git a/netconf b/netconf
index 07bf06018335e965a605ca55318dd282d34084aa..3dd1d377490630a19ec45e613eb75cf2803a4a0d 100644 (file)
--- a/netconf
+++ b/netconf
@@ -6,7 +6,7 @@
 # 1.0 of the CDDL.
 #
 # A full copy of the text of the CDDL should have accompanied this
 # 1.0 of the CDDL.
 #
 # A full copy of the text of the CDDL should have accompanied this
-# source.  A copy of the CDDL is also available via the Internet at
+# source. A copy of the CDDL is also available via the Internet at
 # http://www.illumos.org/license/CDDL.
 #
 
 # http://www.illumos.org/license/CDDL.
 #
 
@@ -146,21 +146,23 @@ def usage():
     -g, --gateway      Default gateway. Optional.
     -i, --interface    Interface to configure.
     -m, --netmask      Netmask to use for interface. Default /24.
     -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 = ()
 
     try:
        opts, args = getopt.gnu_getopt(sys.argv[1:],
 
 def parse_input():
     options = ()
 
     try:
        opts, args = getopt.gnu_getopt(sys.argv[1:],
-           'ha:g:i:m:n', ['help', 'address=', 'gateway=',
-                          'interface=', 'netmask=', 'nameserver='])
+                'ha:g:i:m:n:r', ['help', 'address=', 'gateway=',
+               'interface=', 'netmask=', 'nameserver=', 'record'])
     except getopt.GetoptError as err:
        print str(err)
        usage()
        sys.exit(2)
 
     address = gateway = interface = netmask = nameserver = None
     except getopt.GetoptError as err:
        print str(err)
        usage()
        sys.exit(2)
 
     address = gateway = interface = netmask = nameserver = None
+    record = False
 
     if opts:
        for o, a in opts:
 
     if opts:
        for o, a in opts:
@@ -177,28 +179,41 @@ def parse_input():
                netmask = a
            elif o in ('-n', '--nameserver'):
                nameserver = a
                netmask = a
            elif o in ('-n', '--nameserver'):
                nameserver = a
+           elif o in ('-r', '--record'):
+               record = True
            else:
                assert False, 'Unhandled option'
            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'
     
        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
     
     return options
 
 def main():
     interactive = True
+    record = False
+    output = ''
 
     options = parse_input()
     if options:
 
     options = parse_input()
     if options:
-       interactive = False
+        record = options[5]
+        if (options[0]):
+           interactive = False
 
     p = Parser()
     try:
 
     p = Parser()
     try:
@@ -207,7 +222,15 @@ def main():
            if interactive:
                nic = make_menu(interfaces)
            else:
            if interactive:
                nic = make_menu(interfaces)
            else:
-               nic[0] = options[0]
+                nic = options
+                found = False
+                for i in interfaces:
+                    if nic[0] == i[0]:
+                        found = True
+                        break
+                if not found:
+                    err = '%s: No such interface' % nic[0]
+                    raise RuntimeError(err)
             if nic:
                if interactive:
                    (ip,mask,gw) = get_config()
             if nic:
                if interactive:
                    (ip,mask,gw) = get_config()
@@ -216,34 +239,50 @@ def main():
                    mask = options[2]
                    gw = options[3]
                 cmd = 'ipadm delete-if %s' % nic[0]
                    mask = options[2]
                    gw = options[3]
                 cmd = 'ipadm delete-if %s' % nic[0]
-                runcmd(cmd)
+               if record:
+                   output += cmd
+               else:
+                   runcmd(cmd)
                 cmd = 'ipadm create-if %s' % nic[0]
                 cmd = 'ipadm create-if %s' % nic[0]
-                (out, err) = runcmd(cmd)
-                if err:
-                    raise RuntimeError(err)
+               if record:
+                   output += "\n" + cmd
+               else:
+                   (out, err) = runcmd(cmd)
+                   if err:
+                       raise RuntimeError(err)
                 if not ip:
                     # use DHCP
                     cmd = 'ipadm create-addr -T dhcp %s/v4' % nic[0]
                 if not ip:
                     # use DHCP
                     cmd = 'ipadm create-addr -T dhcp %s/v4' % nic[0]
-                    (out, err) = runcmd(cmd)
-                    if err:
-                        raise RuntimeError(err)
+                   if record:
+                       output += "\n" + cmd
+                   else:
+                       (out, err) = runcmd(cmd)
+                       if err:
+                           raise RuntimeError(err)
                 else:
                     # use STATIC
                     cmd = 'ipadm create-addr -T static -a %s/%s %s/v4' % (ip, mask, nic[0])
                 else:
                     # use STATIC
                     cmd = 'ipadm create-addr -T static -a %s/%s %s/v4' % (ip, mask, nic[0])
-                    (out, err) = runcmd(cmd)
-                    if err:
-                        raise RuntimeError(err)
+                   if record:
+                       output += "\n" + cmd
+                   else:
+                       (out, err) = runcmd(cmd)
+                       if err:
+                           raise RuntimeError(err)
                     if gw:
                         cmd = 'route -p add default %s' % gw
                     if gw:
                         cmd = 'route -p add default %s' % gw
-                        (out, err) = runcmd(cmd)
-                        if err:
-                            raise RuntimeError(err)
-                cmd = 'netstat -rn -finet'
-                (out, err) = runcmd(cmd)
-                if err:
-                    raise RuntimeError(err)
-                print 'New route table'
-                print out
+                       if record:
+                           output += "\n" + cmd
+                       else:
+                           (out, err) = runcmd(cmd)
+                           if err:
+                               raise RuntimeError(err)
+               if not record:
+                   cmd = 'netstat -rn -finet'
+                   (out, err) = runcmd(cmd)
+                   if err:
+                       raise RuntimeError(err)
+                   print 'New route table'
+                   print out
                if interactive:
                    dns = raw_input('Configure DNS [n]? ').lower()
                else:
                if interactive:
                    dns = raw_input('Configure DNS [n]? ').lower()
                else:
@@ -260,9 +299,14 @@ def main():
                     cmd = "echo 'nameserver %s' >> /etc/resolv.conf " % dns
                     cmd += '&& cp /etc/nsswitch.conf{,.bak} '
                     cmd += '&& cp /etc/nsswitch.{dns,conf}'
                     cmd = "echo 'nameserver %s' >> /etc/resolv.conf " % dns
                     cmd += '&& cp /etc/nsswitch.conf{,.bak} '
                     cmd += '&& cp /etc/nsswitch.{dns,conf}'
-                    (out, err) = runcmd(cmd)
-                    if err:
-                        raise RuntimeError(err)
+                   if record:
+                       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:
         else:
             print "Found no unassigned interfaces"
     except ParserError as e:
This page took 0.081476 seconds and 5 git commands to generate.