Posts

Showing posts from July, 2015

Avoid using global/class-level mutable datatypes like list/dicts

Class-level variables that are assigned with mutable datatypes are like global static variables, they refer to the same list or dict and can be modified/accessed via multiple instance or through the class itself, >>> class Foo: bar = [] >>> a, b = A(), A() >>> a.bar.append(10) >>> a.bar [10] >>> b.bar [10] >>> Foo.bar [10] >>> class A: def __init__(self): self.bar = [] >>> a, b = A(), A() >>> a.bar.append(10) >>> a.bar [10] >>> b.bar [] >>> Foo.bar Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: type object 'Foo' has no attribute 'bar' To avoid using the same global list/dict across multiple instances we should avoid initializing class/global variables with a mutable datatypes like list/dict

How to restart networking without DHCP RELEASE on Ubuntu

On Ubuntu systems, there is no more networking service restart option, so users are forced to do ifdown and ifup. But  ifdow n  by default do a DHCPRELEASE causing systems to potentially change IPs on ifup. This would not be a very appreciated sideaffect with automated systems where IPs could change during the lifetime of the system. Following script allows you to restart networking without actually do a DHCPRELEASE, (to avoid the DHCPRELEASE create temporary interfaces with all interfaces set to static and  bring down interfaces using that file, but use the original file to do the bring up the interfaces) # cat ifdownup.sh pkill dhclient && \ sed 's/dhcp/manual/' /etc/network/interfaces > /tmp/interfaces && \ for intf in `cat /etc/network/interfaces | grep inet | cut -d ' ' -f 2`; \ do ifdown $intf -i /tmp/interfaces && ifup $intf; done​