# \[RESOLU\] Test firewall iptables dans un script bash

**URL:** <https://www.debian-fr.org/t/resolu-test-firewall-iptables-dans-un-script-bash/21698>\
**Category:** Programmation\
**Created:** [Février 20, 2016, 8:33 UTC](https://www.debian-fr.org/t/resolu-test-firewall-iptables-dans-un-script-bash/21698 "2016-02-20T20:33:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Realpeace](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/r/b4bc9f/32.png) [@Realpeace](https://www.debian-fr.org/u/Realpeace)\
**Post date:** [Février 20, 2016, 8:33 UTC](https://www.debian-fr.org/t/resolu-test-firewall-iptables-dans-un-script-bash/21698/1 "2016-02-20T20:33:17Z")

</div>

Bonjour,

J’ai une petite question en rapport avec iptables

J’ai fais un petit script qui est sensé tester si le firewall sur ma machine est bien actif.

Etant donnée que “DROP” est dans mon script de firewall, j’ai fais ceci pour le tester.

[code]FIREWALL=“ON”  
…

# Test du service firewall

if [[$FIREWALL = “ON”]] ; then  
FIREWALL=`iptables -L | grep "DROP "`  
if [[$FIREWALL = “”]] ; then  
FIREWALL='STOP’  
DEFAUT="OUI"  
else  
FIREWALL='START’  
fi  
fi  
echo $FIREWALL[/code]

Lorsque je le lance à la main, pas de problème, par contre des que le lance dans un crontab, la variable $FIREWALL est vide.

Avez vous une idée de la cause du problème :S?

Merci 😉

---

<div class="post-metadata">

**Author:** ![Realpeace](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/r/b4bc9f/32.png) [@Realpeace](https://www.debian-fr.org/u/Realpeace)\
**Post date:** [Février 20, 2016, 8:33 UTC](https://www.debian-fr.org/t/resolu-test-firewall-iptables-dans-un-script-bash/21698/2 "2016-02-20T20:33:20Z")

</div>

J’ai finalement trouvé :  
Il faut donnée le chemin complet pour le programme.

```auto

```

Merci quand même 😉

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.debian-fr.org/user_avatar/www.debian-fr.org/ricardo/32/3864_2.png) [@ricardo](https://www.debian-fr.org/u/ricardo)\
**Post date:** [Février 20, 2016, 8:33 UTC](https://www.debian-fr.org/t/resolu-test-firewall-iptables-dans-un-script-bash/21698/3 "2016-02-20T20:33:21Z")

</div>

Merci à toi d’avoir pris la peine de donner la solution, même si tu l’as trouvée toi-même.  
C’est, malheureusement, pas toujours ce que font certains membres, dommage !

---

<div class="post-metadata">

**Author:** ![lol](https://www.debian-fr.org/user_avatar/www.debian-fr.org/lol/32/15629_2.png) [@lol](https://www.debian-fr.org/u/lol)\
**Post date:** [Février 20, 2016, 8:33 UTC](https://www.debian-fr.org/t/resolu-test-firewall-iptables-dans-un-script-bash/21698/4 "2016-02-20T20:33:21Z")

</div>

[quote=“ricardo”]Merci à toi d’avoir pris la peine de donner la solution, même si tu l’as trouvée toi-même.  
C’est, malheureusement, pas toujours ce que font certains membres, dommage ![/quote]  
+1 ![:mrgreen:](https://www.debian-fr.org/uploads/default/original/1X/cc83e9b583138e48ff45f96609046b06d06c7f9d.gif "Mr. Green")

Très pratique ce petit script !

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.debian-fr.org/user_avatar/www.debian-fr.org/ricardo/32/3864_2.png) [@ricardo](https://www.debian-fr.org/u/ricardo)\
**Post date:** [Février 20, 2016, 8:33 UTC](https://www.debian-fr.org/t/resolu-test-firewall-iptables-dans-un-script-bash/21698/5 "2016-02-20T20:33:21Z")

</div>

[quote=“lol”][quote=“ricardo”]Merci à toi d’avoir pris la peine de donner la solution, même si tu l’as trouvée toi-même.  
C’est, malheureusement, pas toujours ce que font certains membres, dommage ![/quote]  
+1 ![:mrgreen:](https://www.debian-fr.org/uploads/default/original/1X/cc83e9b583138e48ff45f96609046b06d06c7f9d.gif "Mr. Green")

Très pratique ce petit script ![/quote]  
Oui, il m’intéresse aussi mais à modifier “à ma sauce”.

---

<div class="post-metadata">

**Author:** ![lol](https://www.debian-fr.org/user_avatar/www.debian-fr.org/lol/32/15629_2.png) [@lol](https://www.debian-fr.org/u/lol)\
**Post date:** [Février 20, 2016, 8:33 UTC](https://www.debian-fr.org/t/resolu-test-firewall-iptables-dans-un-script-bash/21698/6 "2016-02-20T20:33:22Z")

</div>

> [@](#):
>
> Oui, il m’intéresse aussi mais à modifier “à ma sauce”.

Les sauce “maison” ont toujours plus de goût !  
C’est malheureusement un truc que je ne sais pas encore faire… Mais je m’accroche ! ![:smt006](https://www.debian-fr.org/uploads/default/original/1X/924eb3db8bedad6a0d333b02031d3c6a9af9d9c5.gif)

---

<div class="post-metadata">

**Author:** ![chris38](https://www.debian-fr.org/user_avatar/www.debian-fr.org/chris38/32/12918_2.png) [@chris38](https://www.debian-fr.org/u/chris38)\
**Post date:** [Février 20, 2016, 8:34 UTC](https://www.debian-fr.org/t/resolu-test-firewall-iptables-dans-un-script-bash/21698/7 "2016-02-20T20:34:22Z")

</div>

J’ai testé ce petit script, mon FW est bien actif et pourtant le script me renvoie “STOP” 😮

---

<div class="post-metadata">

**Author:** ![MisterFreez](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/m/bb73d2/32.png) [@MisterFreez](https://www.debian-fr.org/u/MisterFreez)\
**Post date:** [Février 20, 2016, 8:34 UTC](https://www.debian-fr.org/t/resolu-test-firewall-iptables-dans-un-script-bash/21698/8 "2016-02-20T20:34:22Z")

</div>

Tu le configure comment ton firewall ? Si quand il est actif il y a que INPUT qui est en DROP par défaut alors fait plutôt ça :

```auto

```

Ou sinon pour tout prendre

```auto

```

Ensuite au lieu de ça :

`FIREWALL=`iptables -L | grep '^Chain INPUT (policy DROP)$'`
        if [[$FIREWALL = ""]] ; then`  
Tu peut faire ça :

`iptables -L | grep -q '^Chain INPUT (policy DROP)$'
if [$? -eq 0]; then`  
ou

```auto

```

---

<div class="post-metadata">

**Author:** ![chris38](https://www.debian-fr.org/user_avatar/www.debian-fr.org/chris38/32/12918_2.png) [@chris38](https://www.debian-fr.org/u/chris38)\
**Post date:** [Février 20, 2016, 8:34 UTC](https://www.debian-fr.org/t/resolu-test-firewall-iptables-dans-un-script-bash/21698/9 "2016-02-20T20:34:24Z")

</div>

> [@](#):
>
> Tu le configure comment ton firewall ? Si quand il est actif il y a que INPUT qui est en DROP …

Tout est en `DROP`

[code]lagache:/home/chris# iptables -L  
Chain INPUT (policy DROP)  
target prot opt source destination  
ACCEPT all – anywhere anywhere  
ACCEPT all – anywhere anywhere state RELATED,ESTABLISHED  
ACCEPT all – anywhere anywhere state NEW  
ACCEPT tcp – anywhere anywhere state NEW tcp dpt:58400  
ACCEPT tcp – anywhere anywhere state NEW tcp dpt:58410  
ACCEPT tcp – anywhere anywhere state NEW tcp dpt:www  
ACCEPT tcp – anywhere anywhere state NEW tcp dpt:ftp

Chain FORWARD (policy DROP)  
target prot opt source destination  
REJECT all – 192.168.30.2 anywhere reject-with icmp-admin-prohibited  
ACCEPT all – anywhere anywhere state NEW,RELATED,ESTABLISHED  
ACCEPT all – anywhere anywhere state RELATED,ESTABLISHED

Chain OUTPUT (policy DROP)  
target prot opt source destination  
ACCEPT all – anywhere anywhere  
ACCEPT all – anywhere anywhere state RELATED,ESTABLISHED  
ACCEPT all – anywhere anywhere  
ACCEPT tcp – anywhere anywhere state NEW tcp dpt:5900  
ACCEPT tcp – anywhere anywhere state NEW tcp dpt:ssh  
ACCEPT tcp – anywhere anywhere state NEW tcp dpt:rsync[/code]

---

<div class="post-metadata">

**Author:** ![MisterFreez](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/m/bb73d2/32.png) [@MisterFreez](https://www.debian-fr.org/u/MisterFreez)\
**Post date:** [Février 20, 2016, 8:34 UTC](https://www.debian-fr.org/t/resolu-test-firewall-iptables-dans-un-script-bash/21698/10 "2016-02-20T20:34:24Z")

</div>

`VAR=$(iptables -L | grep -q '^Chain [A-Z]* (policy DROP)$' | wc -l )
if [$VAR -eq 3]; then
    FIREWALL='START';
else
    FIREWALL='STOP';
fi`  
Ça ça doit fonctionner.

---

<div class="post-metadata">

**Author:** ![chris38](https://www.debian-fr.org/user_avatar/www.debian-fr.org/chris38/32/12918_2.png) [@chris38](https://www.debian-fr.org/u/chris38)\
**Post date:** [Février 20, 2016, 8:34 UTC](https://www.debian-fr.org/t/resolu-test-firewall-iptables-dans-un-script-bash/21698/11 "2016-02-20T20:34:24Z")

</div>

aucunes réponses !

---

<div class="post-metadata">

**Author:** ![MisterFreez](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/m/bb73d2/32.png) [@MisterFreez](https://www.debian-fr.org/u/MisterFreez)\
**Post date:** [Février 20, 2016, 8:34 UTC](https://www.debian-fr.org/t/resolu-test-firewall-iptables-dans-un-script-bash/21698/12 "2016-02-20T20:34:25Z")

</div>

Mais encore ?

---

<div class="post-metadata">

**Author:** ![chris38](https://www.debian-fr.org/user_avatar/www.debian-fr.org/chris38/32/12918_2.png) [@chris38](https://www.debian-fr.org/u/chris38)\
**Post date:** [Février 20, 2016, 8:34 UTC](https://www.debian-fr.org/t/resolu-test-firewall-iptables-dans-un-script-bash/21698/13 "2016-02-20T20:34:27Z")

</div>

> [@](#):
>
> Mais encore ?

Pas de répons de la commande …

---

<div class="post-metadata">

**Author:** ![MisterFreez](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/m/bb73d2/32.png) [@MisterFreez](https://www.debian-fr.org/u/MisterFreez)\
**Post date:** [Février 20, 2016, 8:34 UTC](https://www.debian-fr.org/t/resolu-test-firewall-iptables-dans-un-script-bash/21698/14 "2016-02-20T20:34:28Z")

</div>

Quand tu tape ça ?

`VAR=$(iptables -L | grep -q '^Chain [A-Z]* (policy DROP)$' | wc -l )
if [$VAR -eq 3]; then
    FIREWALL='START';
else
    FIREWALL='STOP';
fi`

Alors tape ça :

[code]VAR=$(iptables -L | grep -q ‘^Chain [A-Z]\* (policy DROP)$’ | wc -l )  
if [$VAR -eq 3]; then  
FIREWALL=‘START’;  
else  
FIREWALL=‘STOP’;  
fi

echo “Firewall : $FIREWALL”[/code]

---

<div class="post-metadata">

**Author:** ![chris38](https://www.debian-fr.org/user_avatar/www.debian-fr.org/chris38/32/12918_2.png) [@chris38](https://www.debian-fr.org/u/chris38)\
**Post date:** [Février 20, 2016, 8:34 UTC](https://www.debian-fr.org/t/resolu-test-firewall-iptables-dans-un-script-bash/21698/15 "2016-02-20T20:34:39Z")

</div>

J’ai une erreur :

[code]VAR=$(iptables -L | grep -q ‘^Chain [A-Z]\* (policy DROP)$’ | wc -l )  
if [$VAR -eq 3]; then  
FIREWALL=‘START’;  
else  
FIREWALL=‘STOP’;  
fi

echo “Firewall : $FIREWALL”[/code]

==\>`iptables: command not found
`

---

<div class="post-metadata">

**Author:** ![MisterFreez](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/m/bb73d2/32.png) [@MisterFreez](https://www.debian-fr.org/u/MisterFreez)\
**Post date:** [Février 20, 2016, 8:34 UTC](https://www.debian-fr.org/t/resolu-test-firewall-iptables-dans-un-script-bash/21698/16 "2016-02-20T20:34:40Z")

</div>

Tu le lance en root ?

---

<div class="post-metadata">

**Author:** ![chris38](https://www.debian-fr.org/user_avatar/www.debian-fr.org/chris38/32/12918_2.png) [@chris38](https://www.debian-fr.org/u/chris38)\
**Post date:** [Février 20, 2016, 8:34 UTC](https://www.debian-fr.org/t/resolu-test-firewall-iptables-dans-un-script-bash/21698/17 "2016-02-20T20:34:51Z")

</div>

> [@](#):
>
> Tu le lance en root ?

Oop’s 😊

Mais ne fonctionne toujours pas chez moi ==\> Ne renvoie pas de résultat

---

<div class="post-metadata">

**Author:** ![MisterFreez](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/m/bb73d2/32.png) [@MisterFreez](https://www.debian-fr.org/u/MisterFreez)\
**Post date:** [Février 20, 2016, 8:34 UTC](https://www.debian-fr.org/t/resolu-test-firewall-iptables-dans-un-script-bash/21698/18 "2016-02-20T20:34:52Z")

</div>

Tu met bien tout ça dans un fichier ?  
Si oui à la première ligne écris ça :

```auto

```

Comme ça tu verras ce qu’il fait.

---

<div class="post-metadata">

**Author:** ![chris38](https://www.debian-fr.org/user_avatar/www.debian-fr.org/chris38/32/12918_2.png) [@chris38](https://www.debian-fr.org/u/chris38)\
**Post date:** [Février 20, 2016, 8:34 UTC](https://www.debian-fr.org/t/resolu-test-firewall-iptables-dans-un-script-bash/21698/19 "2016-02-20T20:34:52Z")

</div>

[quote=“MisterFreez”]Tu met bien tout ça dans un fichier ?  
Si oui à la première ligne écris ça :

```auto
#!/bin/sh -x
```

Comme ça tu verras ce qu’il fait.[/quote]

Oui, il est bien dans un fichier ,je le lance bien en root et toujours pas de résultats ![:smt006](https://www.debian-fr.org/uploads/default/original/1X/924eb3db8bedad6a0d333b02031d3c6a9af9d9c5.gif) .

---

<div class="post-metadata">

**Author:** ![MisterFreez](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/m/bb73d2/32.png) [@MisterFreez](https://www.debian-fr.org/u/MisterFreez)\
**Post date:** [Février 20, 2016, 8:34 UTC](https://www.debian-fr.org/t/resolu-test-firewall-iptables-dans-un-script-bash/21698/20 "2016-02-20T20:34:52Z")

</div>

C’est de l’option -x dont je te parlais. Elle permet de voir l’exécution du script. 🙂

[Page suivante](https://www.debian-fr.org/t/resolu-test-firewall-iptables-dans-un-script-bash/21698.md?page=2)
