A-A+

Python IP地址切换的脚本、编码

2016年01月02日 站长资讯 暂无评论

最近在笔记本重新安装了虚拟机,由于在家在公司经常需要切换IP,修改IP配置文件,觉得挺繁琐的,所以就索性的写了一个自动切换IP的脚本,主要是读配置文件修改,详情如下:

1、创建配置文件:

  1. # cat ip.conf  
  2. [home]  
  3. ip = 192.168.7.150  
  4. gateway = 192.168.7.1  
  5.    
  6. [work]  
  7. ip = 10.7.93.150  
  8. gateway = 10.7.93.1  

2、脚本实例

  1. #!/usr/bin/env  python  
  2. conding = utf-8  
  3. # finame swtch_ip.py  
  4. # This is swtch ip python file  
  5.    
  6. import socket,os,sys  
  7. import ConfigParser  
  8. import time  
  9. from optparse import OptionParser  
  10.    
  11. def init():  
  12.     global IPS  
  13.     global GATEWAYS  
  14.     config = ConfigParser.ConfigParser()  
  15.     dir = os.path.dirname(os.path.abspath(__file__))  
  16.     conf = dir + "/conf"  
  17.     filepath = "%s/ip.conf" %(conf)  
  18.     if not os.path.exists(filepath):  
  19.         raise "ERROR: ip.conf is not it!"  
  20.     config.read(filepath)  
  21.     IPS = config.get(options.type,"ip")  
  22.     GATEWAYS = config.get(options.type,"gateway")  
  23.     print "======[%s] Start get new options,please wait... ======" %(options.type)  
  24.   
  25.   
  26.     print "%s config file ip is %s,gateway is %s" %(options.type,IPS,GATEWAYS)  
  27.    
  28. def getopts():  
  29.     MSG_USAGE='''python %s -t home''' % sys.argv[0]  
  30.     optParser=OptionParser(MSG_USAGE)  
  31.     optParser.add_option('-t',action='store',type='string',dest='type',default='work',help=u'type:work,home')  
  32.     (options,args)=optParser.parse_args()  
  33.     return options  
  34.    
  35. def ipconfig():  
  36.     ipconfigfile = open('/etc/sysconfig/network-scripts/ifcfg-eth0','r')  
  37.     while True:  
  38.         ipconfiglines = ipconfigfile.readlines()  
  39.         if not ipconfiglines:  
  40.         break  
  41.         #ipconfigfile.close()  
  42.     if "IPADDR" in ipconfiglines[4] and "GATEWAY" in ipconfiglines[3]:   
  43.         print "====== Change IP and GATEWAY, please wait... ======"  
  44.             temp = ipconfiglines[4].split('\"')  
  45.             a = temp[1]  
  46.             temp1 = ipconfiglines[3].split('\"')  
  47.             b = temp1[1]  
  48.             print "ifcfg-et0 config old IP is %s and old GATEWAY is %s" %(a,b)  
  49.             edit_file = os.system('sed -i "s/'+a+'/'+IPS+'/g;s/'+b+'/'+GATEWAYS+'/g" /etc/sysconfig/network-scripts/ifcfg-eth0')  
  50.             if edit_file == 0:  
  51.                 print 'Edit ifcfg-eth0 ip and gateway is OK,restart network restart'   
  52.                 restart_network = os.system('/etc/init.d/network restart')  
  53.         if restart_network == 0:  
  54.             print "Network restart Succ!"  
  55.                 else:  
  56.                 print "Network restart Fail!"  
  57.         else:  
  58.                 print 'Edit ifcfg-eth0 ip is ERROR'  
  59.     else:  
  60.         print "no IP address and GATEWAY!!!"  
  61.    
  62. if __name__ == '__main__':  
  63.     if len(sys.argv)<1 and sys.argv[1] != '-h' and sys.argv[1] != '--help':   
  64.         print '''Usage:  python %s -t work  
  65.         python %s -h|--help''' % (sys.argv[0],sys.argv[0])  
  66.         sys.exit(1)  
  67.     options = getopts()  
  68.     init()  
  69.     ipconfig()  

3、查看帮助

  1. # python swich_ip.py --help  
  2. Usage: python swich_ip.py -t home  
  3.    
  4. Options:  
  5.   -h, --help  show this help message and exit  
  6.   -t TYPE     type:work,home  

4、运行测试

  1. # python swich_ip.py -t home  
  2. ======[home] Start get new options,please wait... ======  
  3. home config file ip is 192.168.7.250,gateway is 192.168.7.1  
  4. ====== Change IP and GATEWAY, please wait... ======  
  5. ifcfg-et0 config old IP is 192.168.7.250 and old GATEWAY is 192.168.7.1  
  6. Edit ifcfg-eth0 ip and gateway is OK,restart network restart  

正在关闭接口 eth0:[确定]

关闭环回接口:[确定]

弹出环回接口:[确定]

弹出界面 eth0:[确定]

Network restart Succ!

5、结果

  1. # cat  /etc/sysconfig/network-scripts/ifcfg-eth0  
  2. DEVICE="eth0"  
  3. ONBOOT="yes"  
  4. BOOTPROTO="static"  
  5. GATEWAY="192.168.7.1"  
  6. IPADDR="192.168.7.250"  
标签:

给我留言