IPv6 Routing table Building access in Python

Questions about programming languages and debugging
Post Reply
User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

IPv6 Routing table Building access in Python

Post by maboroshi »

Hello Everyone...

I am trying to convert some code to IPv6 on windows

here it is

Code: Select all


def read_routes():
        if WINDOWS:
            ok = 0
            routes = []
            ip = '(\d+\.\d+\.\d+\.\d+)\s+'
            netstat_line = ip + ip + ip + ip + "(\d+)"
            pattern = re.compile(netstat_line)
            f=os.popen("netstat -rn")
            for l in f.readlines():
                match = re.search(pattern,l)
                if match:
                    dest   = match.group(1)
                    mask   = match.group(2)
                    gw     = match.group(3)
                    netif  = match.group(4)
                    metric = match.group(5)
                    intf = dnet.intf().get_dst(dnet.addr(type=2, addrtxt=dest))
                    if not intf.has_key("addr"):
                        break
                    addr = str(intf["addr"])
                    addr = addr.split("/")[0]

                    dest, = struct.unpack("I", dnet.ip_aton(dest))
                    mask, = struct.unpack("I", dnet.ip_aton(mask))
                    routes.append((dest,mask,gw, str(intf["name"]), addr))
            f.close()
            return routes
here is the code for the Unix Version of "IPv6"

Code: Select all

def read_routes6():
        f = os.popen("netstat -rn -f inet6")
        ok = -1
        routes = []
        lifaddr = in6_getifaddr()
        for l in f.readlines():
            if not l:
                break
            l = l.strip()
            if ok < 0:
                ok = l.find('Destination')
                continue
            # gv 12/12/06: under debugging	
	    #if NETBSD:
	    #    dest,nh,fl,_,_,_,dev = l.split()[:7]
	    #else:
	    if 1:
                d,nh,fl,dev = l.split()[:4]
	    if filter(lambda x: x[2] == dev, lifaddr) == []:
	        continue
            if 'L' in fl: # drop MAC addresses
                continue

            if 'link' in nh:
	     	nh = '::'

	    cset = [] # candidate set (possible source addresses)
	    dp = 128
	    if d == 'default':
		d = '::'
		dp = 0
	    if '/' in d:
		d,dp = d.split("/")
		dp = int(dp)
	    if '%' in d:
		d,dev = d.split('%')
	    if '%' in nh:
		nh,dev = nh.split('%')
	    if 'lo' in dev:
		cset = ['::1']
		nh = '::'
            else:
		devaddrs = filter(lambda x: x[2] == dev, lifaddr)
		cset = construct_source_candidate_set(d, dp, devaddrs)

            if len(cset) != 0:
		routes.append((d, dp, nh, dev, cset))

        f.close()
        return routes
I could probably figure it out but I am tempted to see what kind of response I may get...

Anyway ty for any help

Cheers

Maboroshi

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Post by bad_brain »

man, I understand almost nothing of the code... 8O :?

shamir
Computer Manager
Computer Manager
Posts: 853
Joined: 01 Mar 2007, 17:00
17
Location: NY
Contact:

Post by shamir »

me enther I don't get it at all 8O

Post Reply