#! _*_ 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. WFP之位图效果

    首先看一个图片:   在"第一步"这个按钮周围,有一转红色,这个效果就是用WPF的位图效果实现的. 位图效果(BitmapEffect 对象)是简单的像素处理操作.位图效果将 Bi ...

  2. db2 锁表

    2019独角兽企业重金招聘Python工程师标准>>> 查询锁表情况 db2 => get snapshot for locks on databasename 可以看到什么表 ...

  3. Nakamori Akina

    听过中森明菜的歌以后,一直想写点什么.恰好前段时间看过她的一个访谈https://b23.tv/av13810011,节目里已经39岁左右的她看着已经有些衰老,但是那份属于她的天真却保持的很好. 节目 ...

  4. POJ - 3074 Sudoku (搜索)剪枝+位运算优化

    In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For exa ...

  5. js世家委托详解

    事件原理 通过div0.addElementListener来调用:用法:div0.addElementListener(事件类型,事件回调函数,是否捕获时执行){}.1.事件类型(type):必须是 ...

  6. 题目分享N

    题意:有辆车,有r行,s*2列,在第s列和第s+1列之间有个过道,出口在第r+1行的过道处,现在给出每个人的位置(行号和列号),每人每次只能动一格,问最少耗费多长时间全员才能逃出去 分析:假如车上只有 ...

  7. golang之channel

    Buffered Channels package main import "fmt" func main() { ch := make(chan int, 2) ch <- ...

  8. CentOS安装配置nginx和php

    今天买了台阿里云服务器用于日常开发测试(新人9块钱半年).系统版本CentOS 6.5 64位. 首先安装nginx: yum install nginx 参考文档: 在CentOS 6上搭建LNMP ...

  9. Codeforces Round #563 (Div. 2) A-D

    A. Ehab Fails to Be Thanos 这个A题很简单,就是排个序,然后看前面n个数和后面的n个数是不是相同,相同就输出-1 #include <cstdio> #inclu ...

  10. @Resource、@Autowired等几个注解的区别

    1.@Resource注解和@Autowired的区别 @Autowired注解:是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它required属性为false ...