栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

ubuntu18.04初始化script(python3+shell)

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ubuntu18.04初始化script(python3+shell)

#!/bin/bash
# ubuntu18.04初始化配置


#判断用户执行
if [ $UID != "0" ];then
	echo "请使用root用户运行"
	exit 1
fi

hostnamectl set-hostname 041840636zwt


# 配置apt源
mv /etc/apt/source.list /etc/apt/source.list.bak
touch /etc/apt/source.list
cat>>/etc/apt/source.list<>/root/.bashrc<>config_net.py< list:
    interface_list = []
    with open('/proc/net/dev') as f:
        net_file = f.readlines()
    for line in net_file[2:]:
        interface = line[0:line.index(':')].strip()
        if interface != 'lo':
            interface_list.append(interface)
    return interface_list


def netmask_2_int(netmask: str) -> int:
    str_list = netmask.split('.')
    one_count = 0
    for str in str_list:
        bin_str = bin(int(str))[2:]
        one_count += bin_str.count('1')
    return one_count


if __name__ == '__main__':
    net_file = open('/etc/netplan/01-network-manager-all.yaml', mode='a')
    net_file.write('n  ethernets:n')
    for interface in get_interfaces():
        print('=' * 50)
        print(f'ttconfigure interface {interface}')
        print('=' * 50)
        ip_reg = r'[0-9]+.[0-9]+.[0-9]+.[0-9]+'
        ip_group_reg = r'([0-9]+).([0-9]+).([0-9]+).([0-9]+)'
        reg = re.compile(ip_group_reg)
        while True:
            address = input('please enter your ip address:n')
            if re.match(ip_reg, address):
                matcher = reg.match(address)
                for i in range(1, 5):
                    if int(matcher.group(i)) >= 255:
                        print('ERROR:please enter the right address')
                        continue
                break

        while True:
            netmask = input(f'please enter your netmask(255.255.255.0):n')
            if netmask == '':
                netmask = '255.255.255.0'
                break
            bin_mask = ''
            if re.match(ip_reg, netmask):
                matcher = reg.match(netmask)
                for i in range(1, 5):
                    if int(matcher.group(i)) > 255:
                        print('ERROR:please enter the right netmask')
                        continue
                    bin_num = bin(int(matcher.group(i)))[2:]
                    bin_mask += bin_num
            if not re.match('1+0+', bin_mask):
                print('ERROR:the netmask is not recommended')
                break

        while True:
            gateway = input(f'please enter your gateway:n')
            if re.match(ip_reg, gateway):
                matcher = reg.match(gateway)
                for i in range(1, 5):
                    if int(matcher.group(i)) >= 255:
                        print('ERROR:please enter the right gateway')
                        continue
                break

        while True:
            dns = input(f'please enter your DNS({gateway}):n')
            if dns == '':
                dns = gateway
                break
            if re.match(ip_reg, dns):
                matcher = reg.match(dns)
                for i in range(1, 5):
                    if int(matcher.group(i)) >= 255:
                        print('ERROR:please enter the right DNS')
                        continue
                break

        content = f'''
            {interface}:
              dhcp4: no
              dhcp6: no
              addresses: [{address}/{netmask_2_int(netmask)}]
              gateway4: {gateway}
              nameservers:
                addresses: [{dns}]n
        '''
        net_file.write(content)
        net_file.close()
    if os.system('netplan apply') == 0:
        print("33[1;32m interface configure succeed33[0m")
    else:
        print("33[1;31m interface configure failed33[0m")

EOF

python3 config_net.py
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/333715.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号