IPv6 and Python question

27 06 2007

OK my lazywebbers with IPv6 and Python experience, this goes out to you!

I am creating an application right now, with Python of course, and I need to set an environment variable to be always on when a user logs in. I have messed with the os.environ and the os.putenv and what not, but it doesn’t work permanently. This application doesn’t do anything more than a few tests and then sets the environment variable if the tests return true. Is there an easy way to write to ~/.bashrc or ~/.bash_profile to enable the variable, and then later on if the user wants they can disable the variable which would either remove the variable all together or would set it to False?

Another question I have is with IPv6. Is there an easy way to test and see if your network is an IPv6 network or an IPv4 network? Right now I am grepping /var/log/syslog for ‘no IPv6 router present’ and printing the ‘no’. If it is ‘no’ then the network is IPv4, and if it returns 0, then it is an IPv6 network. This is rather hackish I feel, but it does work. Just wondering if there is a better method to do this simple task.

Thanks everyone!


Actions

Informations

7 responses to “IPv6 and Python question”

27 06 2007
Dennis (13:05:35) :
   (Using Mozilla Mozilla 1.8.1.4 on Ubuntu Linux Ubuntu Linux)

Modifying the environment only works for the current process and its children. Not for the parent process and other children.

Regarding ipv6: Check the IPv6 address of the card. There are ranges for link-local etc… which are used when not connected to an ipv6 net.

27 06 2007
Niall (13:20:02) :
   (Using Camino Camino 1.5 on Mac OS Mac OS X)

The command
ip -6 neigh show
should show you any ipv6 neighbours. It’s not perfect, but it’s probably good enough.

27 06 2007
nixternal (13:48:58) :
   (Using Konqueror Konqueror 3.5 on GNU/Linux GNU/Linux)

Dennis, thank you sir, but I think using strip() will help me locate and change the variable in .bash_profile.

Niall, ip -6 neigh show is better than grepping syslog, thanks for that one. If there are no ipv6 neighbors it would == 0 which is perfect.

27 06 2007
John (14:26:41) :
   (Using Mozilla Firefox Mozilla Firefox 2.0.0.4 on Ubuntu Linux Ubuntu Linux)

Personally I think I’d attack the first problem by creating a simple file (say ~/.setenvs) of the form:
export FOO=bar
export BAZ=boo
….

Which should be relatively easy to parse and modify in Python. This file could then be sourced in the .bashrc with a line like:
. ~/.setenvs

27 06 2007
rusty (14:45:08) :
   (Using Mozilla Firefox Mozilla Firefox 2.0.0.3 on Ubuntu Linux Ubuntu Linux)

an alternative to ‘ip -6 nei sho’ would be ‘route -6′ then cut the ‘Next Hop’ column out and search for [0-9a-f]. Looks something like:

route -6 | cut -c 45-86 | sort -u | grep -v Next | grep [0-9a-f]

The ‘ip -6 nei sho’ command may work just as well, but I suspect it is possible that the neighbors will eventually time out of the neighbor table. (Just as your syslog file may logrotate before you search it, or may show a message for no router from boot up, which changes later on.)

It may also be a message from an earlier boot when the network didn’t come up properly for some reason.

Route should be valid as long as there is an IPv6 router advertizing in the local network. Agreeded that if one is, then it should show up in the ‘ip -6 nei sho’ results.

28 06 2007
animefan (04:33:31) :
   (Using Mozilla Firefox Mozilla Firefox 2.0.0.4 on Ubuntu Linux Ubuntu Linux)

I would never want whatever program to modify my .bashrc or .bash_profile!
Please force the user to add
source ~/.foo.rc
to his .bashrc and modify your environment in the sourced file.

30 06 2007
Bernhard (18:18:30) :
   (Using Konqueror Konqueror 3.5 on GNU/Linux GNU/Linux)

I would recommend

ip -6 addr | grep “scope global”

to check for IPv6 connectivity. That is basically the solution Dennis suggested, as soon as your IPv6 stack is loaded you get link-local addresses assigned to your interface (scope link), but to get connectivity outside your network you need a global address.

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>