xuyangus 发表于 2015-12-15 08:53:32

python版本的网卡启动

#! /usr/bin/env python
#--*-- coding:utf8 ---*---

之前在板卡工作记录里面,提到的写的shell脚本。有些可以修改的空间,自己修改了一下,顺便联系python.

import commands
import re
import sys

def find_encap(line, if_dict):
      line = line.lower()
      index = line.find('encap')
      up = line.find(' ')
      if index != -1 and up != -1:
                key, value = line.split(':')
                if_dict = value
                return 1
      return 0

def find_hwaddr(line, if_dict):
      line = line.lower()
      index = line.find('hwaddr')
      up = line.find(' ')       #skip first space
      up += 7
      if index != -1:
                key, value = line.split(' ')
                if_dict = value
                return 1
      return 0


def write_to_file(name, if_dict):
      filepath = '/etc/sysconfig/network-scripts/ifcfg-' + name
      file = open(filepath, 'w')
      file.write('DEVICE="' + name + '"\n')
      file.write('BOOTPROTO="static"' + '\n')
      file.write('TYPE="' + if_dict['encap'] + '"\n')
      file.write('HWADDR="' + if_dict['hwaddr'] + '"\n')
      file.write('ONBOOT="yes"' + '\n')
      file.write('NM_CONTROLLED="yes"' + '\n')
      file.close()


if __name__ == '__main__':
      ifconfig = commands.getoutput("ifconfig -a")
      ifconfig = ifconfig.split('\n')

      interfaces = {}
      name=None
      for line in ifconfig:
                match = re.match('^[^ \t]\S*', line)
                if match:
                        name = match.group()
                        interfaces = {}         #save as key:value
                if name == None :
                        exit(-1);               #unlikely
                if find_encap(line,interfaces):
                        print interfaces['encap']
                if find_hwaddr(line, interfaces):
                        print interfaces['hwaddr']

      for name in interfaces.keys():
                if name == 'lo':
                        continue
                write_to_file(name, interfaces )

      print 'done'
页: [1]
查看完整版本: python版本的网卡启动