]> git.datanom.net - netconf.git/blobdiff - netconf
add option to print create commands to stdout
[netconf.git] / netconf
diff --git a/netconf b/netconf
index a4811ca78a814c8b01b9a43e2eebd47ae8e2b1b7..2f0694fd2da43a857e9bb3a37ce6cf8279431335 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
-# 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.
 #
 
@@ -153,14 +153,15 @@ def parse_input():
 
     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
+    record = False
 
     if opts:
        for o, a in opts:
@@ -177,6 +178,8 @@ def parse_input():
                netmask = a
            elif o in ('-n', '--nameserver'):
                nameserver = a
+           elif o in ('-r', '--record'):
+               record = True
            else:
                assert False, 'Unhandled option'
     
@@ -224,34 +227,50 @@ def main():
                    mask = options[2]
                    gw = options[3]
                 cmd = 'ipadm delete-if %s' % nic[0]
-                runcmd(cmd)
+               if record:
+                   print cmd
+               else:
+                   runcmd(cmd)
                 cmd = 'ipadm create-if %s' % nic[0]
-                (out, err) = runcmd(cmd)
-                if err:
-                    raise RuntimeError(err)
+               if record:
+                   print 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]
-                    (out, err) = runcmd(cmd)
-                    if err:
-                        raise RuntimeError(err)
+                   if record:
+                       print 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])
-                    (out, err) = runcmd(cmd)
-                    if err:
-                        raise RuntimeError(err)
+                   if record:
+                       print cmd
+                   else:
+                       (out, err) = runcmd(cmd)
+                       if err:
+                           raise RuntimeError(err)
                     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:
+                           print 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:
@@ -268,9 +287,12 @@ def main():
                     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:
+                       print cmd
+                   else:
+                       (out, err) = runcmd(cmd)
+                       if err:
+                           raise RuntimeError(err)
         else:
             print "Found no unassigned interfaces"
     except ParserError as e:
This page took 0.033104 seconds and 5 git commands to generate.