Raspberry pi host ip

If you use DHCP on raspberry PI, here is a convenient way to get Raspberry Pi IP.
At gui startup, Raspberry start a python script.
Script send POST request to PHP page, who log data to file.

1) Make dir for python script

sudo mkdir /home/pi/code

2) edit post_ip.py

sudo nano /home/pi/code/post_ip.py

3) copy code into editor and exit with ctrl+c

import urllib2
import urllib
import re

# Print the IP address
import socket
import fcntl
import struct

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])

#get_ip_address('eth0')  # '192.168.0.110'
myip=get_ip_address('eth0')

query_args = { 'ip':myip,'nokkel':'yourkey' }
url = 'http://url to regip.php'
data = urllib.urlencode(query_args)
request = urllib2.Request(url, data)
response = urllib2.urlopen(request).read()
print response

4) edit autostart

sudo nano /etc/xdg/lxsession/LXDE-pi/autostart

5) add code to autostart.

@midori
@python /home/pi/code/post_ip.py

6) create regip.php on your server


<?php

//CHECK NUL VALUES
 if (isset($_POST['ip'],$_POST["nokkel"]) == false){ 
  echo "Values not set.";
  exit();//stopp her
  } 


//*******************************************************
//ABORT BAD STRINGS
function contains_bad_str($str_to_test) {
  $bad_strings = array(
                "content-type:"
                ,"mime-version:"
                ,"multipart/mixed"
                ,"Content-Transfer-Encoding:"
                ,"bcc:"
                ,"cc:"
                ,"to:"
  );  
  foreach($bad_strings as $bad_string) {
    if(eregi($bad_string, strtolower($str_to_test))) {
      echo $bad_string . " found. Suspected injection attempt - mail not being sent.";
      exit;
    }
  }
}

//*******************************************************
//ABORT NEWLINES
function contains_newlines($str_to_test) {
//%0A=HEX \\n=string for enter
   if(preg_match("/(%0A|%0D|\\n+|\\r+)/i", $str_to_test) != 0) {
  echo "Newline found. Suspected injection attempt - mail not being sent.";
  //  echo "newline found in " . $str_to_test . " Suspected injection attempt - mail not being sent.";
  exit;
   }
} 
//*******************************************************
//ABORT IF NOT POST
if($_SERVER['REQUEST_METHOD'] != "POST"){
   echo("Unauthorized attempt to access page.");
   exit;
}




contains_bad_str($user);
contains_bad_str($mac);
contains_newlines($user);
contains_newlines($mac);  

//ABORT IF BAD KEY
include '/path outside public html/pass.php';//passord lagret utenfor public_html
if ($_POST['nokkel'] != $passSaved){ //$passSaved is included from pass.php
   echo "wrong password";
   exit();//stopp her
   }

$filename = 'ips.dat';  

//SETT TIDS SONE
$dt = new DateTime('-1 day', new DateTimeZone('Europe/Berlin')); 
$somecontent = $dt->format('Y-m-d H:i:s') . ", " . $_POST['ip'] . "\r\n";
// . = & i vb


//WRITE TO FILE
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file. maks 5 bokstaver
    if (fwrite($handle, $somecontent,200) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($somecontent) to file ($filename)";

    fclose($handle);

} else {
    echo "The file $filename is not writable";
}
?>

7) Create emty file ips.dat on your server to store log.