separate temp script for adding ip ranges from predefined list
This commit is contained in:
82
add_from_list.py
Normal file
82
add_from_list.py
Normal file
@@ -0,0 +1,82 @@
|
||||
from tkinter import W
|
||||
import paramiko, os, dns.resolver
|
||||
|
||||
router_ip = os.getenv("ROUTER_ADDRESS", "192.168.0.1")
|
||||
router_username = os.getenv("ROUTER_USER", "admin")
|
||||
router_password = os.getenv("ROUTER_PASSWORD")
|
||||
interface_name = os.getenv("INTERFACE_NAME")
|
||||
|
||||
auto_vpn_route_comment = "!twitter"
|
||||
|
||||
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):
|
||||
resolver = dns.resolver.Resolver()
|
||||
result = resolver.resolve(domain_name, 'A')
|
||||
return [ip.to_text() for ip in result]
|
||||
|
||||
def exec_command(command):
|
||||
# Run command.
|
||||
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(command)
|
||||
ssh_stdin.close()
|
||||
|
||||
# убираем лишние переносы строки из вывода
|
||||
clean_lines = []
|
||||
for line in ssh_stdout.readlines():
|
||||
clean_lines.append(line.strip())
|
||||
|
||||
return clean_lines
|
||||
|
||||
def get_current_settings():
|
||||
return exec_command("more running-config")
|
||||
|
||||
def get_current_rules():
|
||||
rules = []
|
||||
output = get_current_settings()
|
||||
for line in output:
|
||||
if auto_vpn_route_comment in line:
|
||||
rules.append(line)
|
||||
return rules
|
||||
|
||||
|
||||
def add_vpn_route(ip):
|
||||
if not ip == "127.0.0.1":
|
||||
command = f"ip route {ip} {interface_name} auto {auto_vpn_route_comment}"
|
||||
output = "".join(exec_command(command)).strip()
|
||||
|
||||
return output
|
||||
else:
|
||||
return False
|
||||
|
||||
def delete_vpn_route(ip):
|
||||
command = f"no ip route {ip} {interface_name} {auto_vpn_route_comment}"
|
||||
output = "".join(exec_command(command)).strip()
|
||||
|
||||
return output
|
||||
|
||||
# print("Getting current auto-vpn routes:")
|
||||
# print(get_current_rules())
|
||||
# print()
|
||||
|
||||
# for line in get_current_rules():
|
||||
# ip = line.split(" ")[2]
|
||||
# print(delete_vpn_route(ip))
|
||||
|
||||
with open('ip_lists/twitter.txt') as f:
|
||||
for line in f.readlines():
|
||||
print(line.strip())
|
||||
print(add_vpn_route(line.strip()))
|
||||
|
||||
|
||||
# Close connection.
|
||||
ssh.close()
|
||||
Reference in New Issue
Block a user