#!/usr/bin/perl

use strict;

  ### Run ifconfig and store the data in the @Temp list. 
my @Temp = `/sbin/ifconfig`;

  #### Search for ppp
my $Search = "ppp";
  ### If you are looking for the ip address of your ethernet card, 
  ### uncomment the next line;
# $Search = "eth0";

  ### Make the line we find the ip address blank initially.
my $Match_Line = "";
my $Match_Device = "no";

  ## Search through the lines, if we find a match, save the lines until
  ## we find a blank line. 

foreach my $Line (@Temp)
  {
    ### If we have a match, abort. 
  if ($Match_Line ne "")   {@Temp = ();}
    ### else, see if we can find a match at the beginning of line;
  elsif ($Line =~ /^$Search/) {$Match_Device = "yes";}
    ### else, if we found the device, and we find the line we are looking for
  elsif (($Match_Device eq "yes") && ($Line =~ /^ +inet/)) 
    {$Match_Line = $Line;}  
  }

  ## If our $Match_Line is not blank, split it and get the ip address.
my $IP = "";
if ($Match_Line ne "") 
   {
    ### Get rid of stuff before addr:
   my ($Junk,$Good) = split(/addr\:/, $Match_Line,2);
    ### Get rid of stuff after the first space
   my ($Good,$Junk) = split(/ /, $Good,2);
   $IP = $Good;
   }

  ## If $IP is not blank, we have something. Save to file and transfer file
  ## to remote site. 
  ### Please don't use the /tmp to store this file, but some other location.
if ($IP ne "")
  {  
  open(FILE,">/tmp/IP.txt");
  print FILE "$IP\n";
  close FILE;
  system ('rsync -av -e ssh /tmp/IP.txt web1@somecomputer.com:public_html/IP.txt');
  }
   ### Else, we should send ourselves an email, or do something
   ### to let us know it didn't work. This is left as an exercise.
else {}

