Restart iptables after proxy configuration
This commit is contained in:
@@ -39,46 +39,46 @@ def configure_moxa_firewall_pexpect(router_ip):
|
||||
print(f"\n>>> SUCCESSFULLY CONNECTED! Base prompt is: '{base_prompt}'")
|
||||
|
||||
# --- Check for existing rules ---
|
||||
# print("\n--- Checking for existing rules...")
|
||||
# child.sendline("terminal length 0")
|
||||
# child.expect(base_prompt)
|
||||
# child.sendline("show running-config")
|
||||
# child.expect(base_prompt)
|
||||
# running_config = child.before
|
||||
print("\n--- Checking for existing rules...")
|
||||
child.sendline("terminal length 0")
|
||||
child.expect(base_prompt)
|
||||
child.sendline("show running-config")
|
||||
child.expect(base_prompt)
|
||||
running_config = child.before
|
||||
|
||||
# if "name proxy_retour" in running_config and "name proxy_aller" in running_config:
|
||||
# print("--- Firewall rules already exist. No action needed.")
|
||||
# child.sendline("exit")
|
||||
# child.close()
|
||||
# return
|
||||
if "name proxy_retour" in running_config and "name proxy_aller" in running_config:
|
||||
print("--- Firewall rules already exist. No action needed.")
|
||||
child.sendline("exit")
|
||||
child.close()
|
||||
return
|
||||
|
||||
# print("--- Rules not found. Proceeding with configuration.")
|
||||
print("--- Rules not found. Proceeding with configuration.")
|
||||
|
||||
# --- Enter Configuration Mode ---
|
||||
# print("\n>>> Entering configuration mode...")
|
||||
# child.sendline("configure")
|
||||
# config_prompt_re = r"\(config\)#\s*$"
|
||||
# child.expect(config_prompt_re)
|
||||
print("\n>>> Entering configuration mode...")
|
||||
child.sendline("configure")
|
||||
config_prompt_re = r"\(config\)#\s*$"
|
||||
child.expect(config_prompt_re)
|
||||
|
||||
# --- Apply Rule 1 ---
|
||||
# print("\n>>> Applying Rule 1: proxy_retour")
|
||||
# for cmd in FIREWALL_RULE_1_CMDS:
|
||||
# child.sendline(cmd)
|
||||
# child.expect([r"\(config-firewall\)#\s*$", config_prompt_re])
|
||||
# time.sleep(0.2)
|
||||
print("\n>>> Applying Rule 1: proxy_retour")
|
||||
for cmd in FIREWALL_RULE_1_CMDS:
|
||||
child.sendline(cmd)
|
||||
child.expect([r"\(config-firewall\)#\s*$", config_prompt_re])
|
||||
time.sleep(0.2)
|
||||
|
||||
# --- Apply Rule 2 ---
|
||||
# print("\n>>> Applying Rule 2: proxy_aller")
|
||||
# for cmd in FIREWALL_RULE_2_CMDS:
|
||||
# child.sendline(cmd)
|
||||
# child.expect([r"\(config-firewall\)#\s*$", config_prompt_re])
|
||||
# time.sleep(0.2)
|
||||
print("\n>>> Applying Rule 2: proxy_aller")
|
||||
for cmd in FIREWALL_RULE_2_CMDS:
|
||||
child.sendline(cmd)
|
||||
child.expect([r"\(config-firewall\)#\s*$", config_prompt_re])
|
||||
time.sleep(0.2)
|
||||
|
||||
# --- THE CORRECTED SAVE LOGIC ---
|
||||
# 1. Exit from configuration mode to return to the base prompt
|
||||
# print("\n>>> Exiting configuration mode to save...")
|
||||
# child.sendline("exit")
|
||||
# child.expect(base_prompt)
|
||||
print("\n>>> Exiting configuration mode to save...")
|
||||
child.sendline("exit")
|
||||
child.expect(base_prompt)
|
||||
|
||||
# 2. Now, from the base prompt, issue the save command
|
||||
print("\n>>> Saving configuration...")
|
||||
@@ -99,8 +99,12 @@ def configure_moxa_firewall_pexpect(router_ip):
|
||||
print(f"\n!!! An unexpected error occurred: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
ip_prefix = "10.81.60."
|
||||
ip_suffixes = list(range(194, 215))
|
||||
ip_prefix = "10.81.35."
|
||||
ip_suffixes = list(range(74, 75))
|
||||
ip_suffixes.append(85)
|
||||
for suffix in ip_suffixes:
|
||||
router_ip = ip_prefix + str(suffix)
|
||||
configure_moxa_firewall_pexpect(router_ip)
|
||||
print(f"Configuration of firewall {router_ip}...")
|
||||
configure_moxa_firewall_pexpect(router_ip)
|
||||
print(f"Done!")
|
||||
answer = input(f"Continue?")
|
||||
Reference in New Issue
Block a user