How to Find Out IP Address of PC Behind ADSL Router and Log in Using SSH

By David Legg, 2007. Back to Main Menu
You often find that you need to log into a remote machine over the Internet, but don't know where to find it, i.e. you don't know its IP number (and please would all Internet authors note that the word 'its' has *no* apostrophe.)
A reason that you might not know the IP address of your remote machine is that it is behind an ADSL router that gets a new IP number every time the service provider feels like it, or every time that the router is re-booted.
Below is a little script that looks up the PC's IP number, that is its IP number on the Internet and emails it to you using your normal SMTP mail server somewhere.
Tested and works nicely on Fedora Core 5 and Fedora Core 6 Linux. Email me if it works for you on other operating systems at dwlegg (at) gmail (dot) com.

Instructions:
1. Go to here and download then install SMTP_auth Perl module.
2. Copy and paste the program below into an editor.
3. Save it in a file (say hello.pl) and make it executable, e.g. chmod 755 hello.pl.
4. Note that it will probably contain a plain text password, so you may want to be tighter than 755 permissions, say 700 and owned by root.
5. Edit ' smtp.your.mail.server.net' to be your own service provider's mail server, that is his SMTP server.
6. Edit 'your_smtp_login_id' to be correct for you, and also ' your_smtp_password'.
7. Edit the three occurrences of 'you\@your_email_address.net', being careful to keep the '\' in front of the '@'.
8. Save the file and check that the permissions are correct as per #3 and #4 above.
9. Test the program by running it: ./hello.pl
10. An email should appear shortly thereafter. If not, look for clues in the program's output.
11. If you are still having trouble, turn on debugging by uncommenting the Debug=>1 line.
12. Install the program on your remote PC. Make it run by referencing it from /etc/rc.local, or run it as a cron job, whatever you prefer.
13. When you receive an email telling you the machine's IP address, you can log in to it over the Internet from another machine by typing ssh 123.456.78.9 -l root or similar.
14. Note that the statement: print $smtp->auth_types() lists all the authorisation types that your SMTP server supports, so you have the option of improving this program by choosing a type that is better than 'LOGIN' on the next line to something more secure.

#!/usr/bin/perl -w
#
# Find external, Internet, IP address and email it.
# D.W. Legg 22/6/2007
chdir("/tmp");
`wget -O out1 -o out2 -t 10 http://www.showmyip.com/simple/`;
my $external_ip = `cat out1 out2`;
unlink 'out1';
unlink 'out2';

my $uname = `uname -a`;

use Net::SMTP_auth;

$smtp = Net::SMTP_auth->new('smtp.your.mail.server.net',
#                           Debug=>1,
                           );
print $smtp->auth_types(), "\n";
$smtp->auth('LOGIN', 'your_smtp_login_id', 'your_smtp_password');

$smtp->mail('you\@your_email_address.net');
$smtp->to('dyou\@your_email_address.net ');

$smtp->data();
$smtp->datasend("To: you\@your_email_address.net\n");
$smtp->datasend("Subject: IP of $uname\n");
$smtp->datasend("\n");
$smtp->datasend("$external_ip\n");
$smtp->dataend();

$smtp->quit;

Back to Main Menu