#!/usr/bin/env pythonimport os,sys,time,re,shutilimport socketimport fcntlimport structimport tracebackimport commands#get interface nameinterface_path = '/etc/ sysconfig/network-scripts/' Def log (info): "" " LOG FILES  ....." "" logfile = '/root/pxe_install.log ' files = Open (LogFile, ' a ') try: files.write ('%s : %s \n '% (Time.ctime (), info)) except IOError: files.close () files.close () def get_interface () : os.chdir (Interface_path) eth = em = List () for inter in os.listdir (Interface_path): if inter[0:-1] == ' Ifcfg-em ': if inter == ' ifcfg-em1 ' or inter == ' ifcfg-em2 ': em.append (inter) elif inter[0:-1] == ' Ifcfg-eth ': if inter == ' Ifcfg-eth0 ' or inter == ' Ifcfg-eth1 ': eth.append (inter) if eth: log ("Getting interface file name %s is ok" %eth) return eth else: log ("Getting iNterface file name %s is ok " %em) return em def main (): net_name = get_interface () ipaddr = str () for inter in net_name: try: shutil.move (Inter, '/opt/' + inter+ '. Bak ') _interface_config (inter) new_interface = inter.split ('-') [-1] if _configure_bond (New_interface): _configure_bond (New_interface) &nbsP; log ("Bond script init is ok") except exception,e: log ( Traceback.format_exc ()) if _interface_modprobe (): _interface_modprobe () if _rester_network (): _rester_network () # set interface eth* or em*def _interface_config (interface): "" " DEVICE=eth0 BOOTPROTO=static noboot=yes nm_ controlled=no master=bond0 slave=yes  , "" " fp = open (interface, ' W ') new_interface = Interface.split ('-') [-1] if interface == ' ifcfg-em1 ': fp.write (' device=%s \n '%new_interface) fp.write (' bootproto=static \n ') fp.write (' Onboot=yes \n ') fp.write (' nm_controlled=no \n ') fp.write (' master=bond0 \n ') fp.write (' slave=yes \n ') elif interface == ' ifcfg-em2 ': fp.write (' device=%s \n '%new_interface) fp.write (' bootproto=static \n ') fp.writE (' onboot=yes \n ') fp.write (' nm_controlled=no \n ') fp.write (' master=bond0 \n ') fp.write (' slave=yes \n ') elif interface == ' Ifcfg-eth0 ': fp.write (' device=%s \n '%new_interface) fp.write (' bootproto=static \n ') fp.write (' onboot=yes \n ') fp.write (' NM_ Controlled=no \n ') fp.write (' MASTER=bond0 \n ') fp.write (' slave=yes \n ') elif interface == ' ifcfg-eth1 ': fp.write (' DEVICE=%s \ N '%new_interface) fp.write (' bootproto=static \n ') fp.write (' onboot=yes \n ') fp.write (' NM_ Controlled=no \n ') fp.write (' MASTER=bond0 \n ') fp.write (' slave=yes \n ') fp.close () def _configure_bond (inter): "" " DEVICE=bond0 BOOTPROTO=static ONBOOT=yes IPADDR=192.168.0.100 NETMASK=255.255.255.0 NETWORK=192.168.0.0 broadcast=192.168.0.255 "" " #bond name message if inter == ' eth0 ': bond_name = ' ifcfg-bond0 ' elif inter == ' em1 ': bond_name = ' ifcfg-bond0 ' elif inter == ' eth1 ': bond_name = ' ifcfg-bond0 ' elif inter == ' em2 ': bond_name = ' ifcfg-bond0 '   # IP ADDRESS MESSAGE    IF _INTERFACE_GET_IP (inter):        IPADDR  = _INTERFACE_GET_IP (inter) else: ipaddr = ' 0.0.0.0 ' # ip net mask info try: net_mk = os.popen (' ip a |grep %s|grep inet ' %inter). ReadLines () [0] &nbsP; res = net_mk.split () [1] net_masklen = res.split ('/') [-1] except: net_masklen = 18 net_mask = _interface_sum_ Master (Net_masklen) # default gateway is .... try: net_gate = os.popen (' ip route | Grep default '). ReadLines () [0] net_gateway = net_ Gate = net_gate.split (' ') [2] except: net_gateway = ' 0.0.0.0 ' try: if ipaddr == ' 0.0.0.0 ': return ' fp = open (bond_name, ' W ') bond = bond_name.split ('-') [-1] fp.write ("device=%s \n" %bond) fp.write ("bootproto=static \n") fp.write (" Onboot=yes \n ") fp.write (" ipaddr=%s \n " %ipaddr) fp.write ("netmask=%s \n" % net_mask) if bond == ' bond0 ': fp.write ("gateway=%s \n" % net_gateway) fp.write ("dns1=202.106.0.20 \n") fp.write ("dns2=8.8.8.8 \n") log (" Ifcfg-bond* configure is ok ") return true except Exception,e: return FALSE DEF _INTERFACE_GET_IP (inter): try: s = socket.socket (Socket.af_inet, socket. SOCK_DGRAM) ip = socket.inet_ntoa (Fcntl.ioctl ( s.fileno (), 0x8915, # SIOCGIFADDR struct.pack (' 24s ', Inter)) [20:24]) return ip except Exception,e: pass return # add modprobe bonding modeldef _interface_modprobe (): try: fp = open ('/etc/modprobe.d/ Bonding.conf ', ' W ') fp.write ("#Module options and blacklists written by bonding \n ") Fp.write ("alias bond0 bonding \n") fp.write (" options bond0 miimon=100 mode=1 \n ") Fp.close () x,y = commands.getstatusoutput (' Modprobe bonding ') if x != 0: &nbsP; log ("modprobe bonding is failed") return true except: log ( Traceback.format_exc ()) return # Restart Networkdef _rester_network (): x,y = commands.getstatusoutput (' Service network restart ') if x == 0: log ("restart netowrk is ok ") return true else: log (" restart netowrk is faild ") return # according to the cidr calculation.net masterdef _interface_sum_master (net_ Master): mask = (2** 8) - 2 ** (24 - int (net_master)) return ' 255.255.%s.0 ' % mask if __name__ == ' __main__ ': sc = main ()
This article from "Welcome comments, Welcome to like" blog, please be sure to keep this source http://swq499809608.blog.51cto.com/797714/1557187
Implementation of Linux network card bonding using Python program