Linux Iptables: Add / Delete An IP Address Remotely Using A Shell Script

To add or delete an IP address remotely using a shell script on Linux using iptables, you can use the following steps:

  1. Connect to the remote server using SSH or any other remote access method.
  2. Create a shell script using a text editor such as vi or nano.
  3. Add the following code to the shell script to add the IP address to iptables:
#!/bin/bash

# Add IP address to iptables
iptables -A INPUT -s <IP_ADDRESS> -j ACCEPT

# Save the iptables configuration
service iptables save

Replace <IP_ADDRESS> with the actual IP address that you want to add.

  1. Save the shell script and make it executable using the following command:
chmod +x /path/to/script.sh

Replace /path/to/script.sh with the actual path and filename of the shell script.

  1. Run the shell script using the following command:
./path/to/script.sh

Replace /path/to/script.sh with the actual path and filename of the shell script.

This will add the specified IP address to iptables on the remote server.

To delete an IP address from iptables, you can modify the script as follows:

#!/bin/bash

# Delete IP address from iptables
iptables -D INPUT -s <IP_ADDRESS> -j ACCEPT

# Save the iptables configuration
service iptables save

Again, replace <IP_ADDRESS> with the actual IP address that you want to delete.

Save and run the script to delete the specified IP address from iptables.

Leave a Comment