#! _*_ 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. 2019 ICPC 银川网络赛 D. Take Your Seat (疯子坐飞机问题)

    Duha decided to have a trip to Singapore by plane. The airplane had nn seats numbered from 11 to nn, ...

  2. 设置 Linux 支持中文

    1.首先在 command 输入 locale,可以看到 Linux 下默认的系统语言的是英文 2.vim ~/.bashrc 打开这个文件,该文件夹相当于系统配置文件 3.打开后,将后三行命令输入到 ...

  3. RF(读写 excel)

    1.安装 ExcelLibrary 库:pip install robotframework-ExcelLibrary 但是 Python3.0 通过上面的命令安装 ExcelLibrary 时,会发 ...

  4. pytho xlrd简介

    xlrd:是什么? xlrd是python的一个模块,主要用来对Excel进行读的操作,相对应的,xlwd就是对Excel进行写的模块了. xlrd中有哪些方法可以用呢? 1.打开Excel文件读取数 ...

  5. pycharm安装与破解

    安装即教程地址: https://www.jianshu.com/p/355a6920116f 转载文章,资源失效可用这个链接下载: 链接:https://pan.baidu.com/s/1kBb3s ...

  6. php扩展开发之hello world

    最近在公司做的事情就是php扩展开发,虽然我只负责c++代码的编写,但是了解扩展开发的流程还是很有必要的. (本文介绍的是动态扩展,对静态扩展有兴趣的读者可自行google) php扩展开发环境搭建可 ...

  7. E. Yet Another Task with Queens(分类思想)

    \(\color{Red}{描述}\) \(在n*n的棋盘上有m个K皇后,每个皇后可能被来自8个方向的其他皇后攻击\) \(每个皇后只可能被(0-8)只皇后攻击,分别求出被(0-8)只皇后攻击的皇后数 ...

  8. C. Coffee Break 贪心 思维 有点难 有意思

    C. Coffee Break 这个贪心之前好像写过,还是感觉挺难的,有点不会写. 这个题目大意是:给你一个数列n个元素,然后给你一天的时间,给你一个间隔时间d, 问你最少要用多少天可以把这个数列的所 ...

  9. 你应该知道的Vue高级特性

    本文使用的Vue版本:2.6.10 Vue为我们提供了很多高级特性,学习和掌握它们有助于提高你的代码水平. 一.watch进阶 从我们刚开始学习Vue的时候,对于侦听属性,都是简单地如下面一般使用: ...

  10. 「从零单排HBase 10」HBase集群多租户实践

    在HBase1.1.0发布之前,HBase同一集群上的用户.表都是平等的,大家平等共用集群资源.容易碰到两个问题: 一是某些业务较其他业务重要,需要在资源有限的情况下优先保证核心重要业务的正常运行 二 ...