#! _*_ coding:utf-8 _*_
import os,sys,re,fileinput,socket
device_list = []
ip_list = []
ip_end = []
end_ip = []
gw = []
def warning():
print("\033[31;1m注意:脚本目前不完善,请确认ifconfig网卡信息,不要有多余的网卡,如(docker0等),可能会导致错误!!!\033[0m")
user_input = input("是否以确认ifconfig信息(y or n):")
if user_input == "y":
pass
elif user_input == "n":
exit()
else:
exit()
warning()
def test_1(func):
def bashrc_file(*args,**kwargs):
func()
dic = dict(map(lambda x,y:[x,y],end_ip,device_list))
dic_2 = dict(map(lambda x,y:[x,y],ip_end,device_list))
with open("/root/.bashrc","a+") as file:
for ip_2,device_2 in dic_2.items():
os.system("echo 'ip route flush table %s' >> /root/.bashrc"%device_2)
for gw_1 in gw:
dic_3 = dict(map(lambda x,y:[x,y],gw_1,device_list))
for k,v in dic_3.items():
os.system("echo 'ip route ad default via %s dev %s table %s' >> /root/.bashrc"%(k,v,v))
for j,i in dic_2.items():
os.system("echo 'ip rule ad from %s table %s' >> /root/.bashrc"%(j,i))
return bashrc_file
 
@test_1
def readDevice():
os.system("ifconfig >> /root/ifconfig.conf")
for num in range(50):
with open("/root/ifconfig.conf","r") as file:
for line in file:
if "eth%s: "%num in line:
a = line.strip().split(":")
a_list = []
a_list.append(a[0])
for loop in a_list:
device_list.append(loop)
 
with open("/root/ifconfig.conf","r") as file:
for line in file:
result = re.findall('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}',line)
if not result == []:
IP_list = []
IP_list.append(result[0])
for loop_1 in IP_list:
ip_list.append(loop_1)
ip_end.append(loop_1)
for i in ip_end:
end_ip.append(i.split(".")[3])
 
ip_list.remove("127.0.0.1")
end_ip.remove("1")
ip_end.remove("127.0.0.1")
 
dic = dict(map(lambda x,y:[x,y],end_ip,device_list))
dic_2 = dict(map(lambda x,y:[x,y],ip_end,device_list))
 
for k,v in dic.items():
os.system("echo %s %s >> /etc/iproute2/rt_tables"%(k,v))
 
for route_flush in device_list:
os.system("ip route flush table %s"%route_flush)
 
user_input_gw = input("请按网卡顺序添加网关(以空格分割):").split(" ")
gw.append(user_input_gw)
for i in gw:
dic_gw = dict(map(lambda x,y:[x,y] ,i,device_list))
for k,v in dic_gw.items():
os.system("ip route add default via %s dev %s table %s"%(k,v,v))
 
for ip_1,device_1 in dic_2.items():
os.system("ip rule add from %s table %s"%(ip_1,device_1))
 
os.system("rm -rf ifconfig.conf")
readDevice()
 

linux添加策略路由python脚本(待完善)的更多相关文章

  1. Python脚本:Linux自动化执行Python脚本

    1.环境及其工具: ubuntu 16.04 python2.7(自带) pip2.7(安装) virtualenv(安装) crontab (自带) 2.pip2.7安装 (1)尝试使用 sudo ...

  2. linux添加开机自启动脚本示例详解

    linux下(以RedHat为范本)添加开机自启动脚本有两种方法,先来简单的; 一.在/etc/rc.local中添加如果不想将脚本粘来粘去,或创建链接什么的,则:step1. 先修改好脚本,使其所有 ...

  3. Linux下设置python脚本文件为服务

    (最简单的方式nohup python xxx.py) ------------------------------------------------------------------------ ...

  4. 【Linux学习】python脚本直接运行与nohup运行结果不同

    之前遇到问题,在云主机上运行python脚本直接运行与nohup运行结果不同,甚至nohup根本运行不出来. 后来参考下别人的博客,终于知道问题了. nohup 使用的python版本问题. 而且no ...

  5. linux crontab执行python脚本问题

    在工作中我们经常会写一个python脚本,然后想通过定时任务去执行,这时我们会想到crontab. 通过 crontab -e去设置crontab 例如:0     0       *       * ...

  6. linux 添加开机自启动脚本

    原文 Linux设置服务开机自动启动的方式有好多种,这里介绍一下通过chkconfig命令添加脚本为开机自动启动的方法. 1. 编写脚本autostart.sh(这里以开机启动redis服务为例),脚 ...

  7. [Python]linux自己定义Python脚本命令

    在window下写好的程序配置到Linux上,要实现随意文件夹下的命令调用. 因为初学Linux,这里从文件传输等最主要的方法入手,记录配置的过程中遇到的各种问题. 连接远端server 这里使用pu ...

  8. linux添加开机启动脚本

    [root@mysql ~]# ll /etc/rc.local lrwxrwxrwx. 1 root root 13 Mar 12 22:20 /etc/rc.local -> rc.d/rc ...

  9. linux定时执行python脚本

    每天清晨 4:00:01,用python执行/opt/aa.py文件. 编辑定时任务: #crontab -e 加入: 0 4 * * * python /opt/aa.py 保存,退出即可. 如果执 ...

随机推荐

  1. Ubuntu Install Chinese Input Method

    为了提高在Linux系统使用中文输入的体验,安装搜狗拼音输入法. 确保键盘输入系统选中fcitx. 搜狗拼音输入法基于fcitx(Free Chinese Input Toy for X)框架,所以要 ...

  2. 一个poll的简单例子

    该程序使用poll事件机制实现了一个简单的消息回显的功能,其服务器端和客户端的代码如下所示: 服务器端: //start from the very beginning,and to create g ...

  3. 网络流中的图像转化为OpenCV中的Mat类型

    1,从网络中读取到的图像流,不支持查找,不能直接转化为Mat类型 2,例子如下: string Url = "http://192.168.0.110/cgi-bin/camera?reso ...

  4. 04 全局局部配置 wxml数据绑定 事件 冒泡

    一. 配置介绍 一个小程序应用程序会包括最基本的两种配置文件.一种是全局的 app.json 和 页面自己的 page.json(index.json /test.json等) 注意:配置文件中不能出 ...

  5. SpringBoot上传文件大小限制

    SpringBoot默认上传文件大小不能超过1MB,超过之后会报以下异常: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSize ...

  6. 一键运行CIS安全扫描,集群安全无忧!

    CIS安全扫描是Rancher 2.4推出的其中一个重磅功能,旨在帮助用户快速.有效地加强集群的安全性.本文将详细介绍CIS安全扫描这一功能,包含详细的操作demo. 本文来自Rancher Labs ...

  7. Phoenix and Distribution(字典序贪心)

    \(给定一串字母,分成k份,使得最大字典序最小.(字母可以任意组合)\) \(------------------------------issue~------------------------\ ...

  8. P1714切蛋糕(不定区间最值)

    题面 今天是小Z的生日,同学们为他带来了一块蛋糕.这块蛋糕是一个长方体,被用不同色彩分成了N个相同的小块,每小块都有对应的幸运值. 小Z作为寿星,自然希望吃到的第一块蛋糕的幸运值总和最大,但小Z最多又 ...

  9. Kubernetes笔记(三):Gitlab+Jenkins Pipeline+Docker+k8s+Helm自动化部署实践(干货分享!)

    通过前面两篇文章,我们已经有了一个"嗷嗷待哺"的K8s集群环境,也对相关的概念与组件有了一个基本了解(前期对概念有个印象即可,因为只有实践了才能对其有深入理解,所谓"纸上 ...

  10. extract变量覆盖

    0x01 extract变量覆盖 <?php $flag='xxx'; extract($_GET); if(isset($shiyan)) { $content=trim(file_get_c ...