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.

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 and make it executable, e.g. chmod 755
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 '' 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\', 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: ./
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
`wget -O out1 -o out2 -t 10`;
my $external_ip = `cat out1 out2`;
unlink 'out1';
unlink 'out2';

my $uname = `uname -a`;

use Net::SMTP_auth;

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

$smtp->to('dyou\ ');

$smtp->datasend("To: you\\n");
$smtp->datasend("Subject: IP of $uname\n");


Back to Main Menu