From 6640d1bf7e3d88999946ebe3413fed535e8a12e1 Mon Sep 17 00:00:00 2001 From: Michael Rasmussen Date: Mon, 3 Apr 2017 22:18:12 +0200 Subject: [PATCH] Add license. Support command line input --- netconf | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 105 insertions(+), 8 deletions(-) diff --git a/netconf b/netconf index 0374493..07bf060 100644 --- a/netconf +++ b/netconf @@ -1,6 +1,28 @@ #!/usr/bin/env python +# +# This file and its contents are supplied under the terms of the +# Common Development and Distribution License ("CDDL"), version 1.0. +# You may only use this file in accordance with the terms of version +# 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 +# http://www.illumos.org/license/CDDL. +# -import sys, subprocess +# +# Copyright 2017 +# + +# +# The porpuse of this script is to help with basic and/or initial +# network configuration for Illumos based distribution. However, +# the script is developed specifically for OmniOS so there is no +# garaunty that it will work on other Illumos based distributions +# than Omnios. +# + +import sys, subprocess, getopt def runcmd(cmd): process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) @@ -115,14 +137,84 @@ def get_config(): return (ip,mask,gw) +def usage(): + print 'Usage: %s [options]' % sys.argv[0] + print + print '%s' % """Options: + -h, --help This usage. + -a, --address IP for interface. 0 means use DHCP + -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.""" + +def parse_input(): + options = () + + try: + opts, args = getopt.gnu_getopt(sys.argv[1:], + 'ha:g:i:m:n', ['help', 'address=', 'gateway=', + 'interface=', 'netmask=', 'nameserver=']) + except getopt.GetoptError as err: + print str(err) + usage() + sys.exit(2) + + address = gateway = interface = netmask = nameserver = None + + if opts: + for o, a in opts: + if o in ('-h', '--help'): + usage() + sys.exit(0) + if o in ('-a', '--address'): + address = a + elif o in ('-g', '--gateway'): + gateway = a + elif o in ('-i', '--interface'): + interface = a + elif o in ('-m', '--netmask'): + netmask = a + elif o in ('-n', '--nameserver'): + nameserver = a + else: + assert False, 'Unhandled option' + + if not address or not interface: + print 'Error: missing options' + usage() + sys.exit(2) + if address == '0': + address = None + if not netmask: + netmask = '24' + + options = (interface, address, netmask, gateway, nameserver) + + return options + def main(): + interactive = True + + options = parse_input() + if options: + interactive = False + p = Parser() try: interfaces = p.parse() if interfaces: - nic = make_menu(interfaces) + if interactive: + nic = make_menu(interfaces) + else: + nic[0] = options[0] if nic: - (ip,mask,gw) = get_config() + if interactive: + (ip,mask,gw) = get_config() + else: + ip = options[1] + mask = options[2] + gw = options[3] cmd = 'ipadm delete-if %s' % nic[0] runcmd(cmd) cmd = 'ipadm create-if %s' % nic[0] @@ -152,14 +244,19 @@ def main(): raise RuntimeError(err) print 'New route table' print out - dns = raw_input('Configure DNS [n]? ').lower() + if interactive: + dns = raw_input('Configure DNS [n]? ').lower() + else: + dns = options[4] if not dns or dns == 'n': pass else: - dns = None - print - while not dns: - dns = raw_input('Enter nameserver: ') + if interactive: + dns = None + print + while not dns: + dns = raw_input('Enter nameserver: ') + cmd = "echo 'nameserver %s' >> /etc/resolv.conf " % dns cmd += '&& cp /etc/nsswitch.conf{,.bak} ' cmd += '&& cp /etc/nsswitch.{dns,conf}' -- 2.39.5