Bosse_B
Posts: 777
Joined: Thu Jan 30, 2014 9:53 am

I need to find out the IP address of a remote RPi3....?

Tue Mar 15, 2016 11:21 pm

I am just wondering if there is shellscript support to send email from the Pi "out of the box".
I would like the Pi to be able to send a message containing the Ethernet IP address it has been given by DHCP so I can access it.
The Pi will be sitting in a local network on a remote location, but it will have Internet access via the router, which also hands out the IP address via DHCP.
I can VPN into that network and so also connect to it if I only knew the IP address....
Emailing the IP in a script executed shortly after boot would be one solution.
If that fails another solution could be to send a TCP command to a PHP script on my webserver with the IP address as argument and then the webserver could send the email to me.
But is there "built in" capabilities for such things in Raspbian Jessie? I do not want to write a program to do it.
Any advice?
Bo Berglund
Sweden

HankB
Posts: 123
Joined: Fri Jan 01, 2016 2:45 pm

Re: I need to find out the IP address of a remote RPi3....?

Tue Mar 15, 2016 11:40 pm

Email takes a bit to get configured, If you're up to that then it sending the email is nearly trivial.

What about a GET or POST to your web server? Your logs or whatever should then include the required information. Unfortunately if the LAN is NATed the IP address in the server log won't reflect the actual IP address for the Pi.

klricks
Posts: 6547
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: I need to find out the IP address of a remote RPi3....?

Tue Mar 15, 2016 11:51 pm

Nothing built in.
Look at the connected device list in your router.
OR there are lots of IP scanner apps for your smartphone

I use the DHCP reservation feature in my router to assign an IP to the MAC address of the RPi. This way the router always assigns the same IP from the DHCP pool with no configuration changes in the RPi.
Last edited by klricks on Wed Mar 16, 2016 1:09 am, edited 1 time in total.
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: I need to find out the IP address of a remote RPi3....?

Wed Mar 16, 2016 12:04 am

You could try this:

Code: Select all

`ifconfig eth0|grep "inet addr" | sed 's#^ *inet addr:#wget http://my.computer.com/script.php\&#;s/  B.*//' -`
How it works:
  • The whole thing is wrapped in grave accents. That means that the output of the command line is executed as a command.
  • "ifconfig eth0" reports the configuration of the eth0 interface. One line of which contains the ip address.
  • The grep command snips out just the line that we are interested in.
  • The sed command massages that line into a wget command:
  • - It replaces the first part of the line with the first part of the wget command.
  • - It replaces everything after the ip address with nothing.
The result is a command that looks like:

Code: Select all

wget http://my.computer.com/script.php&192.168.1.61
Which is then executed and sends a request for that page to your computer. I've forgotten the format of a URL; that may need to be a % rather than a &.

Test it without the grave accents until you get it right.

Bosse_B
Posts: 777
Joined: Thu Jan 30, 2014 9:53 am

Re: I need to find out the IP address of a remote RPi3....?

Wed Mar 16, 2016 12:26 am

Thanks rurwin,
will do that test tomorrow night after I get home.

klricks:
Unfortunately I do not have access to the router at the remote network. It is 6000 km away from where I am...
My whole point (without expressly writing that) was to get hold of the IP address without using anything but what is available on Raspbian Jessie by itself.

HankB:
The call to a website script is what I also had in mind, but did not know how to accomplish. But it seems like rurwin's suggestion solves that problem.

I already have emailing php code on my website so I have just to add an interface file to call using wget (or should it be wpost?).
Bo Berglund
Sweden

HankB
Posts: 123
Joined: Fri Jan 01, 2016 2:45 pm

Re: I need to find out the IP address of a remote RPi3....?

Wed Mar 16, 2016 12:52 am

Bosse_B wrote: HankB:
The call to a website script is what I also had in mind, but did not know how to accomplish. But it seems like rurwin's suggestion solves that problem.
ruwin closed that gap very well. The only comment I have is that I think the separator should be a '?'

Actually... I was debugging a web client just yesterday and found a test server for POST requests. I'm not sure if that makes a difference for your server. The test server is described at https://www.posttestserver.com/ (And no, that Henry is not me! :) ) I used 'curl' for testing because I found an example that showed how to send a POST request using 'curl'. Both 'wget' and 'curl' will be available in Raspbian.

One of the very cool things about Linux and the shell is just how much you can accomplish on the command line. ruwin provides a taste of that in his post. If that kind of thing interests you, the book I learned from is "The Unix Programming Environment" By Kernighan and Pike. (Yikes! a bit pricey on Amazon and it looks like it has not been updated since originally published in '83. It was current when I read it. ;) ) I suppose there are equally good tutorials on the Internet.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: I need to find out the IP address of a remote RPi3....?

Wed Mar 16, 2016 8:15 am

This is Linux.

Here's a free* book: http://courseweb.pitt.edu/bbcswebdav/in ... 202009.pdf

That's a mirrored version because the Sourceforge download appears to be broken. For what it's worth, here is the official link: http://linuxcommand.org/tlcl.php


-----
* free as in beer; Creative Commons non-commercial, no-derivatives.

aoakley
Posts: 66
Joined: Fri May 11, 2012 10:27 pm
Contact: Website

Re: I need to find out the IP address of a remote RPi3....?

Wed Mar 16, 2016 8:43 am

It's Linux. There is always another way...

If your goal is to be able to ping/ssh/connect to your machine from your local area network, then you can use Avahi, also known as Zeroconf and Bonjour.

Your Raspberry Pi will need to be the only machine on the network with its hostname. By default the hostname is raspberrypi , so if you have more than one Raspberry Pi on your network, you will need to change the hostnames so that each is unique. You can change the hostname by going into Menu - Preferences - Raspberry Pi Configuration - Hostname (or from the command line: sudo raspi-config - advanced - hostname).

Reboot and go to your laptop or workstation or whatever you're trying to connect *from*.

Open up a terminal or command line and do ONE of the following:

Code: Select all

ping raspberrypi.lan
ping raspberrypi.local
ping raspberrypi
If you have changed your hostname, replace raspberrypi with your new hostname.

Whether you need to put .lan or .local (or nothing) after the hostname, depends on what operating system you are trying to connect from. Ubuntu does it one way, Windows does it another... one of those three will work.

If it doesn't work on Windows, then install Apple iTunes, which will install the Bonjour protocol drivers. Reboot and try the pings again.

You can now connect over your LAN using the hostname as the domain name, without needing to know the IP address first. Note that this won't help if you're trying to connect from the outside world. But it works wonderfully from inside a LAN.

Zeroconf can do a lot more than just automatically resolve hostnames. For example it can automatically tell computers what printers are available on the network.

https://en.wikipedia.org/wiki/Zero-conf ... networking
Andrew Oakley - Cotswold Raspberry Jam, Gloucestershire - http://www.cotswoldjam.org

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: I need to find out the IP address of a remote RPi3....?

Wed Mar 16, 2016 9:17 am

Whether you can use Avahi depends on how the network is arranged. The remote Pi is 6000 miles away and therefore it is not on the same Ethernet segment. However there are ways that it could look as if it is. Running a VPN on the local computer is one way that could happen but a VPN on a separate router probably would not look enough like the same network for the Avahi multicast packets to pass over it.

Bosse_B
Posts: 777
Joined: Thu Jan 30, 2014 9:53 am

Re: I need to find out the IP address of a remote RPi3....?

Wed Mar 16, 2016 10:28 pm

rurwin wrote:This is Linux.
Here's a free* book: http://courseweb.pitt.edu/bbcswebdav/in ... 202009.pdf
That was a good document, thanks for it!

But it is quite big, 522 pages, so I would like to ask a "simple" syntax question first.
I have started to write a small script that will extract the IP address and then send it to the website.
It will be run by cron.
This is my starting code:

Code: Select all

#!/bin/bash
# ---- Script used to report the RPi IP address using the script www.someserver.com/php/reportip.php -----
#ifconfig eth0|grep "inet addr"
#          inet addr:192.168.0.148  Bcast:192.168.0.255  Mask:255.255.255.0

IPAddr=$(ifconfig eth0|grep "inet addr")
#Strip leading part of line, 20 chars
IPAddr=${IPAddr:20}
#For debugging:
echo $IPAddr
#Now strip everything following the IP address
#What to put here?
#wget http://www.someserver.com/php/reportip.php?rpiaddr=$IPAddr
exit 0
This is just test code but is getting there....
I want to stuff the IP address into the $IPAddr variable and the current code strips everything before char 20 from the line produced by the ifconfig call. It uses a fixed position that I suspect will never change.
Next I want to strip off everything from the string following the IP address, i.e. including the white-space between IP address and the word Bcast:.
So what command should I send $IPAddr to in order to get rid of the end of the line?
If it is sed then I am out on thin ice, because I cannot yet decode the instructions on how to use sed...
Bo Berglund
Sweden

asavah
Posts: 362
Joined: Thu Aug 14, 2014 12:49 am

Re: I need to find out the IP address of a remote RPi3....?

Wed Mar 16, 2016 11:59 pm

Code: Select all

wget "http://www.someserver.com/php/reportip.php?rpiaddr=$(ip -4 addr sh dev eth0 |  sed -rn 's:\s*inet\s+([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/.+:\1:p')"
This is not a proper ip regex as it would match from 0.0.0.0 to 999.999.999.999 but it should get you going.

Another example with grep and awk:

Code: Select all

wget "http://www.someserver.com/php/reportip.php?rpiaddr=$(ip -4 addr sh dev eth0 | grep inet | awk '{print $2}' | awk -F '/' '{print $1}')"

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: I need to find out the IP address of a remote RPi3....?

Thu Mar 17, 2016 12:48 am

By far the most useful sed command is "s". It's format is "s/pattern/replacement/"
The "/" can be any character, which is useful if you have a pattern or replacement with a / in it. asavah used : above, for example.

The pattern can be a regular expression. They are quite a learning curve but immensely useful and not only for command-line stuff.

The commands ipconfig|grep "inet addr" get us a line that looks like:

Code: Select all

          inet addr:192.168.1.61  Bcast:192.168.1.255  Mask:255.255.255.0
So we need to take off the beginning and end of that and be left with just the IP address. To take off the beginning we need to match from the first character to "inet addr:" and replace it with nothing:

Code: Select all

s/ *inet addr://
The "*" matches 0 or more occurrences of the character that precedes it. So zero or more spaces followed by that text.
Next we want to match everything that follows the address (That's a space followed by anything.) and replace it with nothing. The character "." matches any character.

Code: Select all

s/ .*//
You can put commands on the sed command line by putting quotes around them and separating them with ";". So the complete line is:

Code: Select all

sed 's/ *inet addr://;s/ .*//'
So:

Code: Select all

IPAddr=$(ifconfig eth0|grep "inet addr"|sed 's/ *inet addr://;s/ .*//')
wget http://www.someserver.com/php/reportip.php?rpiaddr=$IPAddr
You can see how it works by trying each bit out:

Code: Select all

ifconfig eth0
ifconfig eth0|grep "inet addr"
ifconfig eth0|grep "inet addr"|sed 's/ *inet addr://'
ifconfig eth0|grep "inet addr"|sed 's/ *inet addr://;s/ .*//'
IPAddr=$(ifconfig eth0|grep "inet addr"|sed 's/ *inet addr://;s/ .*//')
echo $IPAddr
wget http://www.someserver.com/php/reportip.php?rpiaddr=$IPAddr
asavah is using a different command to start the whole thing off: "ip -4 addr sh dev eth0" But I'm not aware of any particular advantage to it, just whatever you fancy and can see how to handle.

Bosse_B
Posts: 777
Joined: Thu Jan 30, 2014 9:53 am

Re: I need to find out the IP address of a remote RPi3....?

Thu Mar 17, 2016 9:06 am

rurwin wrote:By far the most useful sed command is "s". It's format is "s/pattern/replacement/"
The "/" can be any character, which is useful if you have a pattern or replacement with a / in it. asavah used : above, for example.
.....
Amazing!
That short description of yours explains more than what I managed to get reading the sed chapter in the Linux book pointed to above!
And for simple stuff like what I am doing it works wonders!
Thanks a million!
Bo Berglund
Sweden

Bosse_B
Posts: 777
Joined: Thu Jan 30, 2014 9:53 am

Re: I need to find out the IP address of a remote RPi3....?

Thu Mar 17, 2016 12:17 pm

OK,
so now I am able to compose the wget data:

Code: Select all

 wget http://www.someserver.com/php/ipreport.php?rpiaddr=192.168.0.148&rpiname=rpi3-jessie
I have also prepared a php script that will retrieve the two arguments and put it into an email to me.
The script works fine from a test webpage where it is set as the form handler.
I successfully get the expected email.

But when I execute the command on the command line of the pi I get some strange feedback and then the command never returns to the Pi command line. I have to hit Ctrl-C to get back out of wget.

Code: Select all

 wget http://www.someserver.com/php/ipreport.php?rpiaddr=192.168.0.148&rpiname=rpi3-jessie
... some initial connection lines ...
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘ipreport.php?rpiaddr=192.168.0.148.3’

ipreport.php?rpiaddr=192.168.0.148     [ <=>                                                              ]      32  --.-KB/s   in 0s

2016-03-17 13:10:04 (324 KB/s) - ‘ipreport.php?rpiaddr=192.168.0.148.3’ saved [32]

^C
[1]+  Done                    wget http://www.someserver.com/php/ipreport.php?rpiaddr=192.168.0.148
So obviously I am not using the correct command syntax/format for a POST, which this actually is...
Is wget really the right command to use in order to send input data to a web php script, which will just store these data into an email and not return anything back to the client?
Bo Berglund
Sweden

Bosse_B
Posts: 777
Joined: Thu Jan 30, 2014 9:53 am

Re: I need to find out the IP address of a remote RPi3....?

Thu Mar 17, 2016 1:01 pm

HA!
CURL is my friend!
Did this on the command line:

Code: Select all

 curl --data "rpiaddr=192.168.0.148&rpiname=rpi3-jessie" http://www.someserver.com/php/ipreport.php
The command returned to the prompt without any extra fluff and after a little while I received an email with the following content:

Code: Select all

Below is the result of the RPi address report.
Date of submission: 2016-03-17 08:51:12
---------------------------------------------------------------------------
IP of host network =  83.209.58.xxx
Host domain name = h83-209-58-xxx.dynamic.se.alltele.net
RPi hostname = rpi3-jessie
RPi local IP = 192.168.0.148
---------------------------------------------------------------------------
Remote browser: curl/7.38.0
So this is what I should use!
Seems like I am done now, except I have to check with my private webhosting company why the php script does not work with their system when it does on our company server...
Bo Berglund
Sweden

asavah
Posts: 362
Joined: Thu Aug 14, 2014 12:49 am

Re: I need to find out the IP address of a remote RPi3....?

Thu Mar 17, 2016 4:27 pm

Bosse_B wrote: Is wget really the right command to use in order to send input data to a web php script, which will just store these data into an email and not return anything back to the client?
Yes, but please learn shell basics about quoting and escaping,
did't you see that I quoted the url in wget request?


wget "http://blah/balah?blah=foo&bar=lol"

That's because you need to hide some symbols from the shell,
unquoted & is interpreted as "detach from current shell",
that's why you see that weird output.

That also depends on how the php script was coded, if it expects GET, POST or both.
asavah wrote:asavah is using a different command to start the whole thing off: "ip -4 addr sh dev eth0" But I'm not aware of any particular advantage to it, just whatever you fancy and can see how to handle.
I'm used to use "ip" syntax, ifconfig is supposed to be deprecated,
ip from iproute2 is much more versatile, but for this case it's irrelevant which to use.

Bosse_B
Posts: 777
Joined: Thu Jan 30, 2014 9:53 am

Re: I need to find out the IP address of a remote RPi3....?

Thu Mar 17, 2016 5:16 pm

asavah wrote: Yes, but please learn shell basics about quoting and escaping,
did't you see that I quoted the url in wget request?

wget "http://blah/balah?blah=foo&bar=lol"

That's because you need to hide some symbols from the shell,
unquoted & is interpreted as "detach from current shell",
that's why you see that weird output.
I am running the commands from within a shellscript ipreport.sh.
It looks like the quotes get eaten there because when I execute the exact same text as is present in the script file but on the command line it works!
As you say there seems to be a problem with the & character and it was needed in wget in order to send the two parameters out. With curl I could add each parameter separately with a new --data switch and then it worked both on the command line and inside the script file.
That also depends on how the php script was coded, if it expects GET, POST or both.
It expects POST data, just like it was coming from a webpage with a form.
In fact I created a simple form page for the purpose of testing the script execution.
This script works fine now:

Code: Select all

#!/bin/bash
RPIname=$(hostname)
IPAddr=$(ifconfig eth0|grep "inet addr"|sed 's/ *inet addr://;s/ .*//')
echo "Extracted IP address: $IPAddr "
echo "Extracted hostname: $RPIname"
#Send request
curl --data "rpiaddr=$IPAddr" --data "rpiname=$RPIname" http://www.myownsite.com/php/ipreport.php
exit 0
Bo Berglund
Sweden

User avatar
rpdom
Posts: 14995
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: I need to find out the IP address of a remote RPi3....?

Thu Mar 17, 2016 7:13 pm

Basically this won't work because of the "&"

Code: Select all

wget http://www.someserver.com/php/ipreport.php?rpiaddr=192.168.0.148&rpiname=rpi3-jessie
This will work because the address is contained within quotes and the "&" won't be interpreted by the shell as "Run this command in the background"

Code: Select all

wget "http://www.someserver.com/php/ipreport.php?rpiaddr=192.168.0.148&rpiname=rpi3-jessie"

Return to “General discussion”