<p>Depuis un moment que je voulais faire ce petit script.</p> <p>Il permet d'afficher l'adresse IP interne (via <code>ifconfig</code>) et l'adresse IP externe (via <a href="http://www.opendns.com/" hreflang="en" title="OpenDNS ">OpenDNS</a> ou <a href="http://dyn.com/dns/" hreflang="en" title="Managed DNS: Rock Solid, Scalable, Reliable - Dyn">dyndns</a>) dans une infobulle (<code>notify-send</code> de la <code>libnotify</code>).</p> <p><img src="/public/Linux/Capture_myip.png" alt="Capture infobulle my_ip" style="display:block; margin:0 auto;" title="Capture infobulle my_ip, août 2011" /></p> <h2>Le script BASH</h2> <pre class="bash bash" style="font-family:inherit"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#My IP Notify v0.1</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#2011 - Mathdabomb (mathdabomb@gmail.com)</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#Commande : /path/to/my_ip.sh</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#Affiche par notification l'adresse IP interne et externe de l'ordinateur</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#Exemple : ./my_ip.sh</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#Licence :</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;"># </span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;"># "THE BEER-WARE LICENSE" (Revision 42):</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;"># &lt;phk@FreeBSD.ORG&gt; wrote this file. As long as you retain this notice you</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;"># can do whatever you want with this stuff. If we meet some day, and you think</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;"># this stuff is worth it, you can buy me a beer in return Poul-Henning Kamp</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;"># </span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#--</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#Recupere l'IP interne</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#--</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#Interface reseau à choisir suivant votre distribution</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #007800;">INT</span>=p10p1 <span style="color: #666666; font-style: italic;"># ou eth0</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #007800;">IPINT</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">ifconfig</span> <span style="color: #007800;">$INT</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">"inet adr"</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> -d: <span style="color: #660033;">-f2</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d</span><span style="color: #ff0000;">' '</span> -f1<span style="color: #000000; font-weight: bold;">`</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#--</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#Recupere l'IP externe</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#--</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #007800;">IPEXT</span>=<span style="color: #000000; font-weight: bold;">`</span>curl <span style="color: #660033;">-s</span> http:<span style="color: #000000; font-weight: bold;">//</span>myip.dnsomatic.com<span style="color: #000000; font-weight: bold;">/`</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#IPEXT=`curl http://ifconfig.me`</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#Affiche l'info-bulle</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#MESSAGE="IP interne sur $INT : $IPINT\nIP externe : $IPEXT"</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#--</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#$notify-send --help</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#Usage : notify-send OPTION... &lt;SUMMARY&gt; BODY - create a notification</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#Application Options:</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;"># -u, --urgency=LEVEL Specifies the urgency level (low, normal, critical). </span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;"># -t, --expire-time=TIME Specifies the timeout in milliseconds at which to expire the notification.</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;"># -i, --icon=ICON,ICON... Specifies an icon filename or stock icon to display.</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;"># -c, --category=TYPE,TYPE... Specifies the notification category.</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;"># -h, --hint=TYPE:NAME:VALUE Specifies basic extra data to pass. Valid types are int, double, string and byte.</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">#--</span></div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li> <li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>notify-send <span style="color: #ff0000;">"Adresses IP"</span> <span style="color: #ff0000;">"IP interne sur <span style="color: #007800;">$INT</span> : <span style="color: #007800;">$IPINT</span><span style="color: #000099; font-weight: bold;">\n</span>IP externe : <span style="color: #007800;">$IPEXT</span>"</span> <span style="color: #660033;">-u</span> normal <span style="color: #660033;">-i</span> gtk-dialog-info <span style="color: #660033;">-t</span> <span style="color: #000000;">50000</span></div></li> </ol> </pre> <h2>Quelques explications</h2> <p><strong>ligne 27 :</strong> il s'agit du choix de l'interface réseaux. Depuis Fedora 15 utilise les noms des ports réseaux en interrogeant le BIOS. Pour les autres systèmes, vous pouvez toujours utiliser la dénomination "ethX". La chaine de caractère choisie ("p10p1" ou "eth0 ou autres) est affecté à la variable INT.</p> <p><strong>ligne 28 :</strong> décomposons la ligne de commande <code>IPINT=`ifconfig $INT | grep "inet adr" | cut -d: -f2 | cut -d' ' -f1`</code>&nbsp;: <img src="/public/Linux/Capture-ifconfig.png" alt="capture ifcong" style="display:block; margin:0 auto;" title="capture ifcong, août 2011" /></p> <p>La commande <code>ifconfig $INT</code> permet de voir les caractéristiques de l'adaptateur réseau $INT dont l'adresse IP à la rubrique "inet adr" (ici&nbsp;: <code>inet adr:192.168.1.14</code>).</p> <p>La commande <code>ifconfig $INT | grep "inet adr"</code> permet donc de récupérer la ligne&nbsp;: <code>inet adr:192.168.1.14 Bcast:192.168.1.255 Masque:255.255.255.0</code>.</p> <p>Or ce qui nous intéresse est l'adresse IP interne ("inet adr"). Pour cela, on découper la chaine de caractères en plusieurs morceaux avec la commande <code>cut</code>.</p> <p>Avec la commande <code>cut -d: -f2</code>, on découpe la chaine "inet adr:192.168.1.14 Bcast:192.168.1.255 Masque:255.255.255.0" au niveau des ":" (d'où l’argument <code>-d:</code>). On obtient les morceaux de chaine suivants&nbsp;:</p> <ol> <li>inet adr</li> <li>192.168.1.14 Bcast</li> <li>192.168.1.255 Masque</li> <li>255.255.255.0</li> </ol> <p>Puis grâce à l'argument <code>-f2</code> on sélectionne le deuxième morceau de la chaine soit <code>192.168.1.14 Bcast</code>.</p> <p>Il nous reste plus qu'à se débarrasser de " Bcast". On réutilise la commande <code>cut -d' ' -f1</code> mais cette fois on coupe au caractère "espace", et on sélectionne la première chaine soit <code>192.168.1.14</code> que l'on affecte à la variable IPINT.</p> <p><strong>lignes 33 et 34 :</strong> il s'agit de récupérer son adresse IP externe. Pour cela 3 choix possibles de site internet le donnant (il doit y en avoir d'autres)&nbsp;:</p> <ul> <li>utiliser le site <a href="http://myip.dnsomatic.com" hreflang="en" title="http://myip.dnsomatic.com">http://myip.dnsomatic.com</a> fournit par <a href="http://www.opendns.com" hreflang="en" title="OpenDNS - DNS-Based Web Security">OpenDNS</a></li> <li>utiliser le site <a href="http://ifconfig.me" hreflang="en" title="http://ifconfig.me">http://ifconfig.me</a> (qui peut donner plus de renseignements que seulement l'adresse IP, à visiter donc)</li> </ul> <p>Dans ces 2 cas, j'utilise la commande <code>curl</code> qui permet de récupérer les données d'une URL (cf le <a href="http://curl.haxx.se/docs/manpage.html" hreflang="en" title="cURL - How To Use">curl.1 the man page</a>). Étant donné que ces données sont directement une chaine de caractère représentant votre adresse IP, il n'y a rien d'autre à faire que de l'affecter à la variable IPEXT.</p> <strong>ligne 52 :</strong> il ne reste plus qu'à afficher le résultat dans une info-bulle. Pour cela j'utilise la commande <code>notify-send</code> fournit avec la bibliothèque <strong>libnotify</strong>. Voici comment utiliser cette commande <pre>notify-send OPTION... &lt;SUMMARY&gt; BODY</pre> <ul> <li>OPTION... : représente les différentes options (temps d'affichage, niveau de criticité, icone à utiliser, ...)</li> <li>&lt;SUMMARY&gt;&nbsp;: est le titre de l'info bulle (dans ce cas&nbsp;: "Adresses IP")</li> <li>BODY : est le message à afficher c'est-à-dire les adresses IP. Pour passer à la ligne, il faut utiliser "\n".</li> </ul>