]> git.datanom.net - netconf.git/commitdiff
initial release
authorMichael Rasmussen <mir@datanom.net>
Sat, 1 Apr 2017 01:53:49 +0000 (01:53 +0000)
committerMichael Rasmussen <mir@datanom.net>
Sat, 1 Apr 2017 01:53:49 +0000 (01:53 +0000)
netconf [new file with mode: 0644]

diff --git a/netconf b/netconf
new file mode 100644 (file)
index 0000000..9dd9a81
--- /dev/null
+++ b/netconf
@@ -0,0 +1,181 @@
+#!/usr/bin/env python
+
+import sys, subprocess
+
+def runcmd(cmd):
+    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
+    output, error = process.communicate();
+    
+    return (output.rstrip(), error.rstrip())
+
+class ParserError(Exception):
+    pass
+
+class Parser:
+    """ Parse nic information """
+    
+    def __init__(self):
+        self.__dladm1 = "dladm show-phys -p -o link,media | sed 's/:/ /g'"
+        self.__dladm2 = "dladm show-phys -p -m -o link,address,inuse | sed 's/:/ /g' | sed 's/\\\[[:space:]]/:/g' | sed 's/\\\//g'"
+
+    def __parse_nics(self, text):
+        nics = {}
+        for line in text.splitlines():
+            parts = line.split()
+            #if parts[1] in ('Ethernet', 'Infiniband'):
+            if parts[1] in ('Ethernet'):
+                nics[parts[0]] = [parts[0], parts[1]]
+    
+        return nics
+
+    def __parse_macs(self, obj, text):
+        list = []
+        if obj:
+            for line in text.splitlines():
+                parts = line.split()
+                try:
+                    if parts[2] == 'yes':
+                        del obj[parts[0]]
+                    else:
+                        obj[parts[0]].append(parts[1])
+                except KeyError:
+                    pass
+            for k, v in obj.items():
+                list.append(v)
+
+        return list
+
+    def parse(self):
+        (nics, error) = runcmd(self.__dladm1)
+        if error:
+            raise ParserError(error)
+
+        (macs, error) = runcmd(self.__dladm2)
+        if error:
+            raise ParserError(error)
+
+        return self.__parse_macs(self.__parse_nics(nics), macs)
+
+def get_terminal_width():
+        try:
+            if sys.version_info >= (2,7):
+                width = int(subprocess.check_output(['tput', 'cols']))
+            else:
+                (out, err) = runcmd('tput cols')
+                if err:
+                    raise OSError(err);
+                width = int(out)
+        except OSError as e:
+            print("Invalid Command 'tput cols': exit status ({1})".format(e.errno))
+        except subprocess.CalledProcessError as e:
+            print("Command 'tput cols' returned non-zero exit status: ({1})".format(e.returncode))
+        else:
+            return width
+
+def make_menu(interfaces):
+    res = None
+    while not res:
+        sys.stderr.write("\x1b[2J\x1b[H")
+        cols = get_terminal_width()
+        intro = 'The following unconfigured interfaces was discovered'
+        fill = (cols / 2) - (len(intro) / 2)
+        print '{0:^{cols}}'.format('Simple network interface configuration tool', cols=cols)
+        print
+        print '{0:<{fill}}{1}'.format('', intro, fill=fill)
+        print '{0:<{fill}}{1:>2} {2:^15}  {3:^10}  {4:^17}'.format('','#','Interface','Media','MAC',fill=fill)
+        print '{0:<{fill}}{1:-^2} {2:-^15}  {3:-^10}  {4:-^17}'.format('','','','','',fill=fill)
+
+        n = 0
+        for i in interfaces:
+            print '{0:<{fill}}{1:>2} {2:<15}  {3:<10}  {4:>17}'.format('',n,i[0],i[1],i[2],fill=fill)
+            n += 1
+        print
+        print '{0:<{fill}}{1:<}'.format('','select interface number to configure:',fill=fill),
+        nic = int(raw_input())
+        if nic >= n or nic < 0:
+            print '{0:<{fill}}{1:<} {2:<}'.format('','Error: Interface: 0 <= # <',n,fill=fill),
+            raw_input(' << Hit any key >>')
+        else:
+            res = interfaces[nic]
+
+    return res
+
+def get_config():
+    ip = mask = gw = None
+    nettype = raw_input('dhcp or static [dhcp]: ').lower()
+    if nettype == 'static':
+        while not ip:
+            ip = raw_input('Enter IP: ')
+        mask = raw_input('Enter netmask [24]: ')
+        if not mask:
+            mask = 24
+        gw = raw_input('Enter default route [0=no]: ')
+        if not gw:
+            gw = 0
+    
+    return (ip,mask,gw)    
+
+def main():
+    p = Parser()
+    try:
+        interfaces = p.parse()
+        if interfaces:
+            nic = make_menu(interfaces)
+            if nic:
+                (ip,mask,gw) = get_config()
+                cmd = 'ipadm delete-if %s' % nic[0]
+                runcmd(cmd)
+                cmd = 'ipadm create-if %s' % nic[0]
+                (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)
+                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 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
+                dns = raw_input('Configure DNS [n]? ').lower()
+                if not dns or dns == 'n':
+                    pass
+                else:
+                    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}'
+                    (out, err) = runcmd(cmd)
+                    if err:
+                        raise RuntimeError(err)
+        else:
+            print "Found no unassigned interfaces"
+    except ParserError as e:
+        print 'Parse Errror: %s' % e
+    except RuntimeError as e:
+        print 'Runtime Error: %s' % e
+
+if __name__ == '__main__':
+    try:
+        main()
+    except KeyboardInterrupt:
+        sys.stderr.write("\x1b[2J\x1b[H")
+
This page took 0.035839 seconds and 5 git commands to generate.