Blog Post

IPv6 and Python question

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!

This entry was posted in Development and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • Dennis

    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.

  • Niall

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

  • 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.

  • 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

  • rusty

    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.

  • animefan

    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.

  • Bernhard

    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.

  • Subscribe to

     Subscribe in a reader

    Or, subscribe via email:
    Enter your email address:

  • Archives

%d bloggers like this: