|
Python /
ipconfig.pyPython.Ipconfigpy HistoryShow minor edits - Show changes to output January 07, 2008, at 11:14 AM
by -
Changed lines 7-258 from:
to:
#!/usr/bin/python """ ipconfig by Bob Brandt (projects@brandt.ie) inspired by Meir Michanie's (meirm@riunx.com) implemention. Licensed under GPL version 3 December 1st 2007 """ import sys, os, getopt, socket version = "0.2" def convertsubnetmask(mask): subnetmask = "" for i in [1, 2, 3, 4]: if mask > 7: subnetmask = subnetmask + ".255" elif mask == 7: subnetmask = subnetmask + ".254" elif mask == 6: subnetmask = subnetmask + ".252" elif mask == 5: subnetmask = subnetmask + ".248" elif mask == 4: subnetmask = subnetmask + ".240" elif mask == 3: subnetmask = subnetmask + ".224" elif mask == 2: subnetmask = subnetmask + ".192" elif mask == 1: subnetmask = subnetmask + ".128" else: subnetmask = subnetmask + ".0" mask = mask - 8 return subnetmask.strip(".") def getipinfo(): temp = dict() for line in os.popen("/bin/ip -f inet a"): check = line.strip('\n').strip().split(' ') if check[0] == "inet": if check[2] == "brd": check.pop(2) check.pop(2) if not temp.has_key(check[4]): temp[check[4]] = [] ipaddress = check[1].split("/")[0] subnet = convertsubnetmask(long(check[1].split("/")[1])) scope = check[3] temp[check[4]].append([ipaddress, subnet, scope]) return temp def getrouteinfo(): temp = dict() for line in os.popen("/bin/ip r"): check = line.strip('\n').strip().split(' ') if check[1] == "via": temp[check[0]] = [check[2], check[3], check[4]] return temp def getinterfaces(): temp = [] for line in os.popen("/bin/ip l"): check = line.strip('\n').strip().split(':') if check[0][0:4] != "link": temp.append(check[1].strip()) return temp def getMACs(): temp = dict() oldline = "" for line in os.popen("/bin/ip l"): check = line.strip('\n').strip().split(' ') if check[0][0:4] == "link": mac = check[1] check = oldline.strip('\n').strip().split(':') interface = check[1].strip() temp[interface] = mac oldline = line return temp def getdnsinfo(filename = "/etc/resolv.conf"): templist = [] for line in open(filename): temp=line.strip('\n').strip().split(' ') if temp[0] == "nameserver": temp.pop(0) templist.append(temp[0]) return templist def getdomaininfo(filename = "/etc/resolv.conf"): templist = [] for line in open(filename): temp=line.strip('\n').strip().split(' ') if temp[0] == "search": temp.pop(0) templist = temp return templist def flushDNS(controlfile = "/etc/init.d/nscd"): os.spawnv(os.P_WAIT, "/usr/bin/sudo", ["sudo", controlfile, "restart"]) def releaseDHCP(controlfile = "/sbin/ifdown-dhcp"): for item in getinterfaces(): os.spawnv(os.P_WAIT, "/usr/bin/sudo", ["sudo", controlfile, item]) def renewDHCP(controlfile = "/sbin/ifup-dhcp"): releaseDHCP() for item in getinterfaces(): os.spawnv(os.P_WAIT, "/usr/bin/sudo", ["sudo", controlfile, item]) def restartNetwork(controlfile = "/etc/init.d/network"): os.spawnv(os.P_WAIT, "/usr/bin/sudo", ["sudo", controlfile, "restart"]) def printinfo(printtype = "basic"): print "\nLinux IP Configuration\n" ipdict = getipinfo() routedict = getrouteinfo() macdict = getMACs() iflist = getinterfaces() dnslist = getdnsinfo() domainlist = getdomaininfo() if printtype == "all": domain = "" for item in domainlist: domain = domain + ", " + item dns = [] for item in dnslist: dns.append([" ", item]) print " Host Name . . . . . . . . . . . . :", socket.gethostname() print " Primary Dns Suffix . . . . . . . :", socket.getfqdn()[len(socket.gethostname()):].strip(".") print " Node Type . . . . . . . . . . . . : Unknown" print " IP Routing Enabled. . . . . . . . : Unknown" print " WINS Proxy Enabled. . . . . . . . : No" print " DNS Suffix Search List. . . . . . :", domain.strip(",").strip(), "\n" for interface in iflist: if interface[0:2] == "lo": pass elif interface[0:3] == "sit": pass else: ipaddress = ipdict[interface][0][0] subnetmask = ipdict[interface][0][1] if routedict["default"][2] == interface: defaultgateway = routedict["default"][0] else: defaultgateway = "" print "Ethernet adapter", interface, ":\n" print " Connection-specific DNS Suffix . :", domain.strip(",").strip() print " Description . . . . . . . . . . . :" print " Physical Address. . . . . . . . . :", macdict[interface] print " Dhcp Enabled. . . . . . . . . . . : Unknown" print " Autoconfiguration Enabled . . . . : Unknown" print " IP Address. . . . . . . . . . . . :", ipaddress print " Subnet Mask . . . . . . . . . . . :", subnetmask print " Default Gateway . . . . . . . . . :", defaultgateway print " DHCP Server . . . . . . . . . . . : Unknown" if len(dns) > 1: dns[0][0] = " DNS Servers . . . . . . . . . . . :" for item in dns: print item[0], item[1] else: domain = "" for item in domainlist: domain = domain + ", " + item for interface in iflist: if interface[0:2] == "lo": pass elif interface[0:3] == "sit": pass else: ipaddress = ipdict[interface][0][0] subnetmask = ipdict[interface][0][1] if routedict["default"][2] == interface: defaultgateway = routedict["default"][0] else: defaultgateway = "" print "\nEthernet adapter", interface, ":\n" print " Connection-specific DNS Suffix . :", domain.strip(",").strip() print " IP Address. . . . . . . . . . . . :", ipaddress print " Subnet Mask . . . . . . . . . . . :", subnetmask print " Default Gateway . . . . . . . . . :", defaultgateway def usage(): print "\nipconfig [Options]\n" print " Options:" print " -a | --all = Display full configuration information." print " -b | --basic = Display basic configuration information. (default)" print " --release = Release the IP address for all interfaces." print " -r | --renew = Renew the IP address for all interfaces." print " -n | --network = Restarts the Network Daemon." print " -f | --flushdns = Purges the DNS Resolver cache by restarting the NSCD Daemon." print " -h, --help = Show this help." print " -v, --version = Output version.\n" print " The default is to display only the IP address, subnet mask and default gateway " print " for each adapter bound to TCP/IP.\n" print " Examples:" print " > ipconfig ... Show basic information." print " > ipconfig /all ... Show detailed information" print " > ipconfig /renew ... renew all adapters" print " > ipconfig /release ... release all adapters\n" def showversion(): print "\nGNU ipconfig", version, "\n" print "This program is distributed in the hope that it will be useful," print "but WITHOUT ANY WARRANTY; without even the implied warranty of" print "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the" print "GNU General Public License for more details.\n" print "Originally written by Bob Brandt <projects@brandt.ie>.\n" try: opts, args = getopt.getopt(sys.argv[1:], "abrnf?hv", ["all", "basic", "renew", "network", "flushdns", "help", "version", "release"]) except getopt.GetoptError: usage() sys.exit(2) for opt, arg in opts: if opt in ["-a", "--all"]: printinfo("all") sys.exit() elif opt in ["-b", "--basic"]: printinfo("basic") sys.exit() elif opt in ["--release"]: releaseDHCP() sys.exit() elif opt in ["-r", "--renew"]: renewDHCP() sys.exit() elif opt in ["-n", "--network"]: restartNetwork() sys.exit() elif opt in ["-f", "--flushdns"]: flushDNS() sys.exit() elif opt in ["-?", "-h", "--help"]: usage() sys.exit() elif opt in ["-v", "--version"]: showversion() sys.exit() printinfo() sys.exit() January 07, 2008, at 10:34 AM
by -
Added lines 1-9:
(:title ipconfig.py :) This was a small little project I started to replicate the ipconfig command from windows in linux. In retrospect, this command is not that useful as the standard linux commands are more useful, but it was a good programming exercise. (:div style="border-style:ridge; border-width:2px; background-color:#ffffcc; margin-left:50px; overflow:auto; font-size:small; width:650px; height:500px;":) [@ @] (:divend:) |