HOWTO: Check you external IP Address from the command line
I hope you know about the possibility to find out your IP Address by using visiting the site http://www.whatismyip.com . I needed a way to check this from the command line and this is what I came up with:
For this, you need the packages wget, grep and sed so make sure you have them by checking for them in Synaptic or running the following in a terminal:
Code:
sudo apt-get install wget grep sed
Then, as long as whatismyip.com prints out the IP-Address in the HTML page title, you can use this:
Create shell script:
Code:
cd ~
mkdir bin
nano bin/whatismyip.sh
Insert this into nano (Copy, then paste into nano by pressing Ctrl + Shift + V):
Code:
#!/bin/bash
echo Your external IP Address is:
wget http://Www.whatismyip.com -O - -o /dev/null | grep '<TITLE>' | sed -r 's/<TITLE>WhatIsMyIP\.com \- //g' | sed -r 's/<\/TITLE>//g'
exit 0
(You can also use gedit instead of nano, but I chose to use nano above in case this is done through a remote text-based connection)
Press Ctrl + O, Enter, Ctrl + X, then run:
Code:
chmod u+x bin/whatismyip.sh
Now you can check your external IP Address by running:
Code:
~/bin/whatismyip.sh
...in a terminal.
Re: HOWTO: Check you external IP Address from the command line
dude, kick *** little app, got any more little bash scripts like this?
good work, ive been looking for something like this
defcon
Re: HOWTO: Check you external IP Address from the command line
Woah. I asked for something like these nearly half a year ago (with another account) and go no response. I guess I should be glad someone finally had an answer. :)
Thanks d00d!
Re: HOWTO: Check you external IP Address from the command line
An update on this useful hack: As of this September, if you look at the source of WhatIsMyIP.com, you'll notice it says
Code:
Please set your code to scrape your IP from www.whatismyip.com/automation/n09230945.asp
Please set your code to hit this page at a REASONABLE pace.
The full content of the script can now simply be
Code:
#!/bin/bash
wget www.whatismyip.com/automation/n09230945.asp -O - -o /dev/null
exit 0
Or, if you want it to keep wget from logging, and arrange the output so that the IP address isn't squashed up against the prompt:
Code:
#!/bin/bash
wget www.whatismyip.com/automation/n09230945.asp -O - -q
echo
exit 0
Re: HOWTO: Check you external IP Address from the command line
No need to apt-get wget, grep or sed, they are included in the base install.
The below also work well in Conky.
You can also use dyndns.org for wan ip address:
Code:
wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'
lan ip address:
Code:
ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'
Re: HOWTO: Check you external IP Address from the command line
Thanks everyone, this is very helpful! Both of your contributions make this nice little script for me:
Code:
#!/bin/bash
echo "\nExternal IP: "
wget www.whatismyip.com/automation/n09230945.asp -O - -q
echo "\n\n Internal IP: "
ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'
echo
exit 0
Re: HOWTO: Check you external IP Address from the command line
I know this is an old post but I would like to say thanks for the help. I've been trying to figure out how to traceroute. This is much simpler.
Just in case anyone wants it, I've created the following PERL script I keep in cron to check my IP and compare it to the previous and email me if it has changed.
Code:
#!/usr/bin/perl
$IP_FILE = "/home/c8h10n4o2/scripts/ipemail.result";
my $newip = `wget www.whatismyip.com/automation/n09230945.asp -O - -q`;
my $oldip = getoldip();
my $send_to = "To: me\@me.com\n";
my $reply_to = "Reply-to: me\@me.com\n";
my $from = "From: me\@me.com\n";
my $subject = "Subject: IP Address: $newip\n";
my $content = "<h1>$newip</h1>";
if($newip ne $oldip)
{
writenewip($newip);
unless(open (MAIL, "|/usr/sbin/sendmail me\@me.com"))
{
print "error.\n";
warn "Error starting sendmail: $!";
}
else{
print MAIL $from;
print MAIL $reply_to;
print MAIL $subject;
print MAIL $send_to;
print MAIL "Content-type: text/html\n\n";
print MAIL $content;
close(MAIL) || warn "Error closing mail: $!";
print "Mail sent\n";
}
}
sub writenewip
{
my($newip) = @_;
print ("new IP = $newip\n");
open IPFILE, ">$IP_FILE";
print IPFILE "$newip\n";
close IPFILE;
}
sub getoldip
{
open IPFILE, "$IP_FILE";
my($line) = 0;
while ( <IPFILE> )
{
chomp;
$line = $_;
}
close IPFILE;
return($line);
}
Re: HOWTO: Check you external IP Address from the command line
Nice thanks for this.. Works perfect the shorter code! :D
Re: HOWTO: Check you external IP Address from the command line
This is a little bash script I made to email me and my friend (in CC, -c option in mail command) about IP changes for DNS record changes. Includes a logger to check if the cronjob is indeed running every day.
Code:
#!/bin/bash
IPFILE=/etc/ipaddress
CURRENT_IP=$(wget -q -O - http://www.whatismyip.com/automation/n09230945.asp)
MAIL_FROM="From: noreply@domain.org"
if [ -f $IPFILE ]; then
KNOWN_IP=$(cat $IPFILE)
else
KNOWN_IP=
fi
if [ "$CURRENT_IP" != "$KNOWN_IP" ]; then
echo $CURRENT_IP > $IPFILE
MAIL_SUBJECT="IP address for domain.org changed"
MAIL_BODY="The IP address for domain.org has been changed to $CURRENT_IP. Please change the nameserver records accordingly."
echo $MAIL_BODY | mail -a "$MAIL_FROM" -s "$MAIL_SUBJECT" -c secondmoo@cows.org foobar@domain.org
logger -t ipcheck -- IP changed to $CURRENT_IP
else
logger -t ipcheck -- No IP change
fi
Re: HOWTO: Check you external IP Address from the command line
I now use:
Code:
#!/bin/bash
wget http://Www.whatismyip.org -O - -o /dev/null
echo
exit 0
Best!