Skip to content

Instantly share code, notes, and snippets.

@manesec
Last active July 1, 2023 22:19
Show Gist options
  • Save manesec/724ec30dd321f28d6ddad3c4874a52bf to your computer and use it in GitHub Desktop.
Save manesec/724ec30dd321f28d6ddad3c4874a52bf to your computer and use it in GitHub Desktop.
A auto change openwrt in docker simple script.
#! /bin/python3
# add crontab
# * * * * * /bin/update_openwrt | tee /update_openwrt.log
import subprocess,sys,time,os
from netaddr import IPAddress,IPNetwork
# simple config openwrt in docker
# by manesec
# apt install curl net-tools python3-pip
# pip3 install netaddr
# Display daye
print("=" * 40)
os.system("date")
print("=" * 40)
# check oldip \
update_docker = False
if (len(sys.argv) == 2):
if (sys.argv[1] == "force"):
update_docker = True
# Run program
last_file = "/last_gateway.txt"
last_gw = ""
if (os.path.exists(last_file)):
f = open(last_file,"r")
last_gw = f.read().strip()
f.close()
print("Last gateway address: "+ last_gw)
print("-" * 40)
# Query interface, default gateway
query_result = subprocess.getoutput("/sbin/ip route | awk '/default/ { print $3,$5 }'").strip()
if (query_result == ""):
print("Can not found default gateway")
print(query_result)
sys.exit(-1)
default_router,default_interface = query_result.split(" ")
print("Default Interface: " + default_interface)
print("Default Router: " + default_router)
# Save to last file
if (last_gw != default_router):
update_docker = True
f = open(last_file,"w")
f.write(default_router)
f.close()
# Query get current ipaddr
default_ip = subprocess.getoutput("ifconfig "+default_interface+" | awk '/inet / {print $2;}'").strip()
print("Default IP: " + default_ip)
# Query subnetmask
query_result = subprocess.getoutput("/sbin/ifconfig " + default_interface +" | awk '/netmask /{ print $4;} '").strip()
default_netmask = query_result;
print("Default Netmask: " + default_netmask)
# Query network
default_prefix = IPAddress(default_netmask).netmask_bits()
print("Default Prefix: " + str(default_prefix))
tmp = IPNetwork(default_ip)
tmp.prefixlen = default_prefix
default_network = str(tmp.network)
print("Default Network: " + default_network)
# Find next ip address
tmp_ip = IPAddress(default_ip)
next_ip = ""
for i in list(tmp):
if i > tmp_ip:
next_ip = i
break
next_ip = str(next_ip)
if (next_ip == ""):
print("Find Next IP Failed!")
sys.exit(-1)
print("WRT IP: " + next_ip)
print("-" * 40)
# Run Docker Network
commands = """
docker network disconnect openwrtbrnet openwrt;
docker network rm openwrtbrnet;
docker network create -d macvlan --subnet=%s/%s --gateway=%s -o parent=%s openwrtbrnet;
docker network connect openwrtbrnet openwrt;
docker restart openwrt
docker exec openwrt uci set network.lan.netmask='%s'
docker exec openwrt uci set network.lan.dns='%s'
docker exec openwrt uci set network.lan.gateway='%s'
docker exec openwrt uci set network.lan.ipaddr='%s'
docker exec openwrt uci commit
docker exec openwrt uci show network.lan
""" % (default_network,default_prefix,default_router,default_interface,
default_netmask,default_router,default_router,next_ip)
if (update_docker):
for command in commands.split("\n"):
if (command.strip() == ""): continue
print("$ " + command)
os.system(command)
else:
print("No need to update the docker !")
#! /bin/python3
# the other version
import subprocess,sys,time,os
from netaddr import IPAddress,IPNetwork
# simple config openwrt in docker
# by manesec
# apt install curl net-tools python3-pip
# pip3 install netaddr
# Display daye
print("=" * 40)
os.system("date")
print("=" * 40)
# check oldip \
update_docker = False
if (len(sys.argv) == 2):
if (sys.argv[1] == "force"):
update_docker = True
# Run program
last_file = "/last_gateway.txt"
last_gw = ""
if (os.path.exists(last_file)):
f = open(last_file,"r")
last_gw = f.read().strip()
f.close()
print("Last gateway address: "+ last_gw)
# check network
print("Waiting for network connection ...")
while True:
curl_code = subprocess.getoutput("curl http://baidu.com -I -sw '%{http_code}'").strip()
if (curl_code != 000):
break
time.sleep(5)
print("Internet connected, continue to get ip address ...")
print("-" * 40)
# Query interface, default gateway
query_result = subprocess.getoutput("/sbin/ip route | awk '/default/ { print $3,$5 }'").strip()
if (query_result == ""):
print("Can not found default gateway")
sys.exit(-1)
default_router,default_interface = query_result.split(" ")
print("Default Interface: " + default_interface)
print("Default Router: " + default_router)
# Save to last file
if (last_gw != default_router):
update_docker = True
f = open(last_file,"w")
f.write(default_router)
f.close()
# Query get current ipaddr
default_ip = subprocess.getoutput("ifconfig "+default_interface+" | awk '/inet / {print $2;}'").strip()
print("Default IP: " + default_ip)
# Query subnetmask
query_result = subprocess.getoutput("/sbin/ifconfig " + default_interface +" | awk '/netmask /{ print $4;} '").strip()
default_netmask = query_result;
print("Default Netmask: " + default_netmask)
# Query network
default_prefix = IPAddress(default_netmask).netmask_bits()
print("Default Prefix: " + str(default_prefix))
tmp = IPNetwork(default_ip)
tmp.prefixlen = default_prefix
default_network = str(tmp.network)
print("Default Network: " + default_network)
# Find next ip address
tmp_ip = IPAddress(default_ip)
next_ip = ""
for i in list(tmp):
if i > tmp_ip:
next_ip = i
break
next_ip = str(next_ip)
if (next_ip == ""):
print("Find Next IP Failed!")
sys.exit(-1)
print("WRT IP: " + next_ip)
print("-" * 40)
# Run Docker Network
commands = """
docker network disconnect openwrtbrnet openwrt;
docker network rm openwrtbrnet;
docker network create -d macvlan --subnet=%s/%s --gateway=%s -o parent=%s openwrtbrnet;
docker network connect openwrtbrnet openwrt;
docker restart openwrt
docker exec openwrt uci set network.lan.netmask='%s'
docker exec openwrt uci set network.lan.dns='%s'
docker exec openwrt uci set network.lan.gateway='%s'
docker exec openwrt uci set network.lan.ipaddr='%s'
docker exec openwrt uci commit
docker exec openwrt uci show network.lan
""" % (default_network,default_prefix,default_router,default_interface,
default_netmask,default_router,default_router,next_ip)
if (update_docker):
for command in commands.split("\n"):
if (command.strip() == ""): continue
print("$ " + command)
os.system(command)
else:
print("No need to update the docker !")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment