美文网首页python百例
111-配置IP地址

111-配置IP地址

作者: 凯茜的老爸 | 来源:发表于2018-08-12 13:36 被阅读50次

RHEL7主机有四块网卡,名为eth0/eth1/eth2/eth3。为四块网卡配置IP地址。

#!/usr/bin/env python

import sys
import re

def configip(fname, ip_addr, if_ind):
    content = """TYPE=Ethernet
BOOTPROTO=none
NAME=eth%s
DEVICE=eth%s
ONBOOT=yes
IPADDR=%s
PREFIX=24
""" % (if_ind, if_ind, ip_addr)
    with open(fname, 'w') as fobj:
        fobj.write(content)

def check_ip(ip_addr):   # 判断IP地址是不是X.X.X.X格式
    m = re.match(r'(\d{1,3}\.){3}\d{1,3}$', ip_addr)
    if not m:
        return False
    return True

def show_menu():
    prompt = """Configure IP Address:
(0) eth0
(1) eth1
(2) eth2
(3) eth3
Your choice(0/1/2/3): """
    try:
        if_ind = raw_input(prompt).strip()[0]
    except:
        print 'Invalid input.'
        sys.exit(1)

    if if_ind not in '0123':
        print 'Wrong Selection. Use 0/1/2/3'
        sys.exit(2)

    fname = '/etc/sysconfig/network-scripts/ifcfg-eth%s' % if_ind
    ip_addr = raw_input('ip address: ').strip()
    result = check_ip(ip_addr)
    if not result:
        print 'Invalid ip address'
        sys.exit(3)
    configip(fname, ip_addr, if_ind)
    print '\033[32;1mConfigure ip address done. Please execute "systemctl restart NetworkManager"\033[0m'

if __name__ == '__main__':
    show_menu()

相关文章

  • 111-配置IP地址

    RHEL7主机有四块网卡,名为eth0/eth1/eth2/eth3。为四块网卡配置IP地址。

  • linux网络管理-2

    Linux网络配置 Linux配置IP地址 DHCP服务器:自动分配IP地址 Linux配置IP地址的方法ifco...

  • Linux网络管理

    Linux配置IP 4种方式1.ifconfig命令临时配置IP地址2.setup工具永久配置IP地址3.修改网络...

  • 第九课 linux网络相关配置文件

    centos系统的网络配置 一、修改IP地址1、ip配置文件 修改对应网卡的IP地址配置文件/etc/syscon...

  • linux网络相关配置文件

    centos系统的网络配置 一、修改IP地址 1、ip配置文件 修改对应网卡的IP地址配置文件/etc/sysco...

  • linux网络相关配置文件

    centos系统的网络配置 一、修改IP地址 1、ip配置文件 修改对应网卡的IP地址配置文件/etc/sysco...

  • Linux网络基础04

    Linux配置IP地址的方法 1.ifconfig命令临时配置IP地址 ifconfig命令:查看与配置网络状态命...

  • Linux网络管理-Linux网络配置

    Linux配置IP地址 ifconfig命令临时配置IP地址DHCP服务器:自动分配IP地址的服务器重启计算机和网...

  • 01:TCP/IP地址配置

    1 案例1:TCP/IP地址配置 1.1 问题 为主机配置以下网络参数: 1)IP地址 192.168.1.10 ...

  • 02nginx的虚拟主机配置

    Nginx的虚拟主机配置步骤 第一步:ip地址的配置 使用ifconfig命名进行IP地址的配置ifconfig ...

网友评论

    本文标题:111-配置IP地址

    本文链接:https://www.haomeiwen.com/subject/yussbftx.html