both, per-domainlist and per-iplist methods moved to main.py
cli arguments added
This commit is contained in:
125
main.py
125
main.py
@@ -1,23 +1,14 @@
|
|||||||
import paramiko, os, dns.resolver
|
import paramiko, os, dns.resolver
|
||||||
|
import contextvars
|
||||||
|
import argparse
|
||||||
|
|
||||||
router_ip = os.getenv("ROUTER_ADDRESS", "192.168.0.1")
|
router_ip = os.getenv("ROUTER_ADDRESS", "192.168.0.1")
|
||||||
router_username = os.getenv("ROUTER_USER", "admin")
|
router_username = os.getenv("ROUTER_USER", "admin")
|
||||||
router_password = os.getenv("ROUTER_PASSWORD")
|
router_password = os.getenv("ROUTER_PASSWORD")
|
||||||
interface_name = os.getenv("INTERFACE_NAME")
|
interface_name = os.getenv("INTERFACE_NAME")
|
||||||
|
|
||||||
auto_vpn_route_comment = "!auto-vpn"
|
auto_vpn_route_comment = contextvars.ContextVar('auto_vpn_route_comment')
|
||||||
|
ssh_client = contextvars.ContextVar('ssh_client')
|
||||||
ssh = paramiko.SSHClient()
|
|
||||||
|
|
||||||
# Load SSH host keys.
|
|
||||||
ssh.load_system_host_keys()
|
|
||||||
# Add SSH host key automatically if needed.
|
|
||||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
||||||
# Connect to router using username/password authentication.
|
|
||||||
ssh.connect(router_ip,
|
|
||||||
username=router_username,
|
|
||||||
password=router_password,
|
|
||||||
look_for_keys=False )
|
|
||||||
|
|
||||||
def get_ips_for_domain_name(domain_name):
|
def get_ips_for_domain_name(domain_name):
|
||||||
resolver = dns.resolver.Resolver()
|
resolver = dns.resolver.Resolver()
|
||||||
@@ -26,7 +17,7 @@ def get_ips_for_domain_name(domain_name):
|
|||||||
|
|
||||||
def exec_command(command):
|
def exec_command(command):
|
||||||
# Run command.
|
# Run command.
|
||||||
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(command)
|
ssh_stdin, ssh_stdout, ssh_stderr = ssh_client.get().exec_command(command)
|
||||||
ssh_stdin.close()
|
ssh_stdin.close()
|
||||||
|
|
||||||
# убираем лишние переносы строки из вывода
|
# убираем лишние переносы строки из вывода
|
||||||
@@ -40,17 +31,18 @@ def get_current_settings():
|
|||||||
return exec_command("more running-config")
|
return exec_command("more running-config")
|
||||||
|
|
||||||
def get_current_rules():
|
def get_current_rules():
|
||||||
rules = []
|
|
||||||
output = get_current_settings()
|
output = get_current_settings()
|
||||||
for line in output:
|
|
||||||
if auto_vpn_route_comment in line:
|
|
||||||
rules.append(line)
|
|
||||||
return rules
|
|
||||||
|
|
||||||
|
return [rule for rule in output if auto_vpn_route_comment.get() in rule]
|
||||||
|
|
||||||
|
def get_all_rules_by_interface():
|
||||||
|
output = get_current_settings()
|
||||||
|
|
||||||
|
return [rule for rule in output if interface_name in rule]
|
||||||
|
|
||||||
def add_vpn_route(ip):
|
def add_vpn_route(ip):
|
||||||
if not ip == "127.0.0.1":
|
if not ip == "127.0.0.1":
|
||||||
command = f"ip route {ip} {interface_name} auto {auto_vpn_route_comment}"
|
command = f"ip route {ip} {interface_name} auto {auto_vpn_route_comment.get()}"
|
||||||
output = "".join(exec_command(command)).strip()
|
output = "".join(exec_command(command)).strip()
|
||||||
|
|
||||||
return output
|
return output
|
||||||
@@ -58,30 +50,85 @@ def add_vpn_route(ip):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
def delete_vpn_route(ip):
|
def delete_vpn_route(ip):
|
||||||
command = f"no ip route {ip} {interface_name} {auto_vpn_route_comment}"
|
command = f"no ip route {ip} {interface_name} {auto_vpn_route_comment.get()}"
|
||||||
output = "".join(exec_command(command)).strip()
|
output = "".join(exec_command(command)).strip()
|
||||||
|
|
||||||
return output
|
return output
|
||||||
|
|
||||||
print("Getting current auto-vpn routes:")
|
def main():
|
||||||
print(get_current_rules())
|
parser = argparse.ArgumentParser(
|
||||||
print()
|
prog = 'Keenetic auto vpn',
|
||||||
|
description = '',
|
||||||
|
epilog = '')
|
||||||
|
|
||||||
for line in get_current_rules():
|
parser.add_argument('-i', '--ip-list')
|
||||||
ip = line.split(" ")[2]
|
parser.add_argument('-d', '--drop',
|
||||||
print(delete_vpn_route(ip))
|
help='Whether drop or not old rules by that tag',
|
||||||
|
action='store_true')
|
||||||
|
parser.add_argument('-D', '--drop-all',
|
||||||
|
help='Drops ALL current custom routes to given interface')
|
||||||
|
args = parser.parse_args()
|
||||||
|
print(type(args.ip_list))
|
||||||
|
|
||||||
with open('domains.txt') as f:
|
ssh = paramiko.SSHClient()
|
||||||
for line in f.readlines():
|
|
||||||
print(line.strip() + ":")
|
# Load SSH host keys.
|
||||||
try:
|
ssh.load_system_host_keys()
|
||||||
ips_list = get_ips_for_domain_name(line.strip())
|
# Add SSH host key automatically if needed.
|
||||||
except dns.resolver.NoAnswer:
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||||
pass
|
# Connect to router using username/password authentication.
|
||||||
print(ips_list)
|
ssh.connect(router_ip,
|
||||||
for ip in ips_list:
|
username=router_username,
|
||||||
print(add_vpn_route(ip))
|
password=router_password,
|
||||||
|
look_for_keys=False )
|
||||||
|
|
||||||
|
ssh_client.set(ssh)
|
||||||
|
|
||||||
|
|
||||||
# Close connection.
|
print(get_all_rules_by_interface())
|
||||||
ssh.close()
|
|
||||||
|
if args.drop:
|
||||||
|
for line in get_current_rules():
|
||||||
|
ip = line.split(" ")[2]
|
||||||
|
print(delete_vpn_route(ip))
|
||||||
|
|
||||||
|
if args.ip_list:
|
||||||
|
print("processing ips list")
|
||||||
|
if os.path.exists(args.ip_list):
|
||||||
|
print(f"using file: {args.ip_list}")
|
||||||
|
with open(args.ip_list) as f:
|
||||||
|
auto_vpn_route_comment.set(f"!{os.path.basename(args.ip_list)}")
|
||||||
|
for line in f.readlines():
|
||||||
|
print(line.strip())
|
||||||
|
print(add_vpn_route(line.strip()))
|
||||||
|
print(os.path.basename(args.ip_list))
|
||||||
|
else:
|
||||||
|
print("ip list file not found")
|
||||||
|
else:
|
||||||
|
print("processing domains list")
|
||||||
|
if os.path.exists('domains.txt'):
|
||||||
|
with open('domains.txt') as f:
|
||||||
|
auto_vpn_route_comment.set("!auto-vpn")
|
||||||
|
for line in f.readlines():
|
||||||
|
print(line.strip() + ":")
|
||||||
|
try:
|
||||||
|
ips_list = get_ips_for_domain_name(line.strip())
|
||||||
|
except dns.resolver.NoAnswer:
|
||||||
|
pass
|
||||||
|
print(ips_list)
|
||||||
|
for ip in ips_list:
|
||||||
|
print(add_vpn_route(ip))
|
||||||
|
else:
|
||||||
|
print("Please, create 'domains.txt' file filled with list of domain names to resolve")
|
||||||
|
|
||||||
|
# print("Getting current auto-vpn routes:")
|
||||||
|
# print(get_current_rules())
|
||||||
|
# print()
|
||||||
|
|
||||||
|
|
||||||
|
# Close connection.
|
||||||
|
ssh.close()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user