Block local and remote port with iptables - Script BASH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Block local and remote port with iptables - Script BASH
# 1  
Old 03-26-2012
Block local and remote port with iptables - Script BASH

Hello

I'm beginner in the linux scripting and i would like to get help. I want to create a script that can block one or more Port even see all the TCP port. The ports must be blocked even when starting my machine.
Of course requires a second script which will allow the ports that you want to unlock.

I put online my script can you help me please?

Cordially

Script 1 : Close
Code:
#!/bin/bash

read -p 'Quelle port voulez-vous désactiver ?' port1

iptables -A INPUT -p tcp --dport $port1 -j REJECT
iptables -A OUTPUT -p tcp --dport $port1 -j REJECT


read -p 'Voulez-vous désactiver un autre port ?(oui/non) ' rep




if [ $rep = 'oui' ]
                then
                        read -p 'Numéro du deuxiéme port ? ' port2
                        iptables -A INPUT -p tcp --dport $port2 -j REJECT
                        iptables -A OUTPUT -p tcp --dport $port2 -j REJECT
                        echo "Les port $port1 et $port2 sont bien désactivés !"
                        echo -e "\niptables -A INPUT -p tcp --dport $port1 -j REJECT\niptables -A OUTPUT -p tcp --dport $port1 -j REJECT\niptables -A INPUT -p tcp --dport $port2 -j REJECT\niptables -A OUTPUT -p tcp --dport $port2 -j REJECT\n" >> /etc/rc.local


exit 0
elif [ $rep = 'non' ]
        then
        echo "Le port $port1 est bien désactivé !"
echo -e "\niptables -A INPUT -p tcp --dport $port1 -j REJECT\niptables -A OUTPUT -p tcp --dport $port1 -j REJECT\n" >> /etc/rc.local
        exit 0
fi
exit 0

Script 2 : Open
Code:
#!/bin/bash

read -p 'Quelle port voulez-vous activer ?' port

iptables -D INPUT -p tcp --dport $port -j REJECT
iptables -D OUTPUT -p tcp --dport $port -j REJECT

echo "Le port $port est bien ouvert !"

echo -n "
iptables -D INPUT -p tcp --dport $port -j REJECT
iptables -D OUTPUT -p tcp --dport $port -j REJECT" >> /etc/rc.local

Moderator's Comments:
Mod Comment How to use code tags when posting data and code samples.

Last edited by Franklin52; 03-26-2012 at 08:43 AM.. Reason: Please use code tags for data and code samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash script, find the next closed (not in use) port from some port range.

hi, i would like to create a bash script that check which port in my Linux server are closed (not in use) from a specific range, port range (3000-3010). the print output need to be only 1 port, and it will be nice if the output will be saved as a variable or in same file. my code is: ... (2 Replies)
Discussion started by: yossi
2 Replies

2. Shell Programming and Scripting

Except script to run a local shell script on remote server using root access

local script: cat > first.sh cd /tmp echo $PWD echo `whoami` cd /tmp/123 tar -cvf 789.tar 456 sleep 10 except script: cat > first #!/usr/bin/expect set ip 10.5.15.20 set user "xyz123" set password "123456" set script first.sh spawn sh -c "ssh $user@$ip bash < $script" (1 Reply)
Discussion started by: Aditya Avanth
1 Replies

3. Red Hat

iptables applied in local machine, can't ssh remote machine after chain changed to DROP

I want to SSH to 192.168.1.15 Server from my machine, my ip was 192.168.1.99 Source Destination was UP, with IP 192.168.1.15. This is LAN Network there are 30 Machine's Connected to the network and working fine, I'm Playing around the local machine's because I need to apply the same rules in... (2 Replies)
Discussion started by: babinlonston
2 Replies

4. UNIX for Dummies Questions & Answers

iptables to block port 25 only to a certain range

I want to limit all *outbound* traffic on eth0 (or all *.*) on port 25 to a specific (allowed) range... I.E. 192.168.1.5 (local ip) tries to connect to 1.2.3.4:25 (outside real world ip) It can proceed because 1.2.3.0/24 is the allowed range Now, 192.168.1.5 (local ip) tries to connect to... (1 Reply)
Discussion started by: holyearth
1 Replies

5. Shell Programming and Scripting

To run a local shell script in a remote machine by passing arguments to the local shell script

I need to run a local shell script on a remote machine. I am able to achieve that by executing the command > ssh -qtt user@host < test.sh However, when I try to pass arguments to test.sh it fails. Any pointers would be appreciated. (7 Replies)
Discussion started by: Sree10
7 Replies

6. Shell Programming and Scripting

Execute a local script against a remote server

I am unable to run the below script against a remote server due to syntax error (then unexpected), but i am able to run it locally. Am i executing it correctly or is there any other way to execute it. ssh username@servernname ksh -s < scriptname #!/bin/ksh function record { ((end =... (5 Replies)
Discussion started by: NarayanaPrakash
5 Replies

7. UNIX for Dummies Questions & Answers

SQL block in a Shell Script connecting to a local and remote DB

Hi All, In a Shell scriipt with a SQL block I want to issue a query against a local DB and a remote DB on a remote server. The shell script is running locally. This is how I connect to the local server. But I want the query to reference remote table in the join. Question can I specify a... (1 Reply)
Discussion started by: daveu7
1 Replies

8. Shell Programming and Scripting

How to stop a script running in remote server from local script

Hi, I have googled for quite some time and couldn't able to get what exactly I am looking for.. My query is "how to stop a shell script which is running inside a remote server, using a script"??? can any one give some suggestions to sort this out. (1 Reply)
Discussion started by: mannepalli
1 Replies

9. Shell Programming and Scripting

executing a remote location script from local server

hi i am having two servers one is local and remote(FTP)server.from local server i have to connect to remote server and execute a shell script i want to run a shell script(remote location) from my local server i am having some knowledge on ftp but i am not getting the result .please give ... (2 Replies)
Discussion started by: srivsn
2 Replies

10. UNIX for Advanced & Expert Users

TCP port scanner for remote or for local

I am unable to find any TCP scanner for data captruing for a Remote or local server. Can anybody please help. i need it to read TCP port and capture the incoming/outgoing data , (3 Replies)
Discussion started by: fahadsiddiqui
3 Replies
Login or Register to Ask a Question