mer30
Posts: 16
Joined: Thu Nov 19, 2015 8:51 pm

ssh Raspberry pi by using MAC adress

Thu Nov 19, 2015 9:14 pm

hi evryone,

i am a new user of Raspberry PI, i bought one a month ago for my project.
i have a problem i search alot but couldint find. my probleme is i cannect my raspberry to local network and i want to access using SSH and most of the time IP of raspberry is changing so every time i have to check the config to know the ip address of my Raspberry.
is there any solution like accessing to my raspberry by using mac adress to khow the IP adress ? or using a name instade of mac adress or ip with out using internet like dyndns ... only in local internet.

sorry for my poor english hope i am clear.

thanks in advance.

User avatar
DougieLawson
Posts: 36306
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: ssh Raspberry pi by using MAC adress

Thu Nov 19, 2015 11:35 pm

Add this stuff at the bottom of /etc/dhcpcd.conf

Code: Select all

interface eth0
static ip_address=192.168.1.xxx/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1 8.8.8.8
change xxx to a value between 2 and 254 that you'd like to use on your network. I'm assuming your home router uses 192.168.1.0 to 192.168.1.255 netmask 255.255.255.0 and not some other address range. You may need to check that.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

mer30
Posts: 16
Joined: Thu Nov 19, 2015 8:51 pm

Re: ssh Raspberry pi by using MAC adress

Fri Nov 20, 2015 9:45 am

thank you sir, i did it already but i had some problem with. i use my Raspberry PI in my University and there ip range is 10.1.0.0/16
so i cant set static ip adress. if i set static ip when i connect to local network may be the ip that i gave to my Raspberry is already is used by other.

ripat
Posts: 191
Joined: Tue Jul 31, 2012 11:51 am
Location: Belgium

Re: ssh Raspberry pi by using MAC adress

Fri Nov 20, 2015 12:54 pm

mer30 wrote:thank you sir, i did it already but i had some problem with. i use my Raspberry PI in my University and there ip range is 10.1.0.0/16
so i cant set static ip adress. if i set static ip when i connect to local network may be the ip that i gave to my Raspberry is already is used by other.
Correct. If you don't know the range used by the dhcp server to allocate ip addresses, you better not allocate a static ip yourself, this to avoid ip collision. Contact your network admin and ask him to allocate you a static address (server side) based on your Rpi hardware (mac) address.

If this is not possible and, depending on your skills and OS you use, there might be two solutions:

  • Install nmap and crawl your subnet segment with this command (as root or sudo):

    Code: Select all

    nmap -sP 10.1.0.0/16 | awk '/^Nmap/{ip=$NF}/B8:27:EB/{print ip}'
    This will find any RPI on your subnet. Be patient as you have a huge number of hosts to scan on your network: more than 65.000!
  • a more elegant solution would be to install a lightweight MTA (mail client - like ssmtp) on your RPI and get him send you an email on every change of ip address. To monitor the changes, install inotify-tools, a user space API for the kernel file monitoring system. It will trigger an action (send mail for example) on every change of the /var/lib/dhcp/dhclient.leases file. As an alternative to inotify, you could run a script that will extract the current ip, compare it to a previously stored ip and if different, send a you a mail. Script could run every 15 min. or so.
Using Linux command line usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.

JimmyN
Posts: 1109
Joined: Wed Mar 18, 2015 7:05 pm
Location: Virginia, USA

Re: ssh Raspberry pi by using MAC adress

Fri Nov 20, 2015 2:13 pm

What I did to take care of situations like that for my "traveling" RPi, was add a 16x2 LCD display. So when I power up the RPi the IP address is shown on the display along with other info like CPU load, etc.

ripat
Posts: 191
Joined: Tue Jul 31, 2012 11:51 am
Location: Belgium

Re: ssh Raspberry pi by using MAC adress

Fri Nov 20, 2015 4:39 pm

I just tested the idea of the rpi sending you a mail when its ip changes. Here are the steps to follow:
  • Install ssmtp and configure it. Don't install mailutils as mentionned in a lot of howto's about ssmtp. The mailutils dependencies are insane! It even install mysql-common an a lot of other useless stuff. Just run ssmtp command as in the script below.
  • make this script executable and run it:

    Code: Select all

    #!/bin/sh
    
    # check current ip and send mail if changed
    
    # user variables
    SEND_TO=your.address@example.com
    SENT_FROM=what.ever@example.com
    
    # Script variables
    IP_FILE=/tmp/previous.ip
    [ -e "$IP_FILE" ] && PREVIOUS_IP=$(cat "$IP_FILE")
    CURRENT_IP=$(ip route | awk '/src/ {print $NF}')
    MAIL_CMD=/usr/sbin/ssmtp
    MAIL_TXT="From:%s\nSubject:%s\n\nThe Rpi new IP is: %s"
    
    if [ "$CURRENT_IP" = "$PREVIOUS_IP" ];then
    	echo "same IP, nothing to do"
    else
    	printf "$MAIL_TXT" $SENT_FROM "Rpi IP" $CURRENT_IP | $MAIL_CMD $SEND_TO
    	echo $CURRENT_IP > $IP_FILE
    	echo "mail sent"
    fi
    
  • Make cron execute that script every 15 minutes or so.
Using Linux command line usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.

mer30
Posts: 16
Joined: Thu Nov 19, 2015 8:51 pm

Re: ssh Raspberry pi by using MAC adress

Fri Nov 20, 2015 5:18 pm

thanks ripat, actually your script is wonder full but as i work in local network if there is no internet connection so i wont recieve any mail. receiving email every 15min in 24h my inbox will be full :( anyway thanks once again for script.

i would like a method or script that let me ssh to my RPI by using MAC Address i don't know if it id possible.

ripat
Posts: 191
Joined: Tue Jul 31, 2012 11:51 am
Location: Belgium

Re: ssh Raspberry pi by using MAC adress

Fri Nov 20, 2015 5:36 pm

mer30 wrote:thanks ripat, actually your script is wonder full but as i work in local network if there is no internet connection so i wont recieve any mail. receiving email every 15min in 24h my inbox will be full :( anyway thanks once again for script.

i would like a method or script that let me ssh to my RPI by using MAC Address i don't know if it id possible.
No, not possible. Easiest way is to talk to your network admin or to run the ping scan (see my first post).

And by the way, the script above only sends an email when the IP changes. Not every 15 minutes!
Using Linux command line usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.

ripat
Posts: 191
Joined: Tue Jul 31, 2012 11:51 am
Location: Belgium

Re: ssh Raspberry pi by using MAC adress

Sat Nov 21, 2015 8:23 am

A ping scan on a /16 network will take ages to complete.

Having slept on it, I think I have an other solution. Assuming that both the rpi and your host are connected on the same subnet and that you can run tcpdump on your host, this is how to proceed.

General idea: force the rpi to automatically send ARP requests on the subnet and capture these requests on the host with tcpdump. ARP requests are broadcasted on the network and can therefore be captured by any host on the network.
  • On the rpi make a script that will flush its ARP table and send a request to an arbitrary ip address (any address) on your subnet

    Code: Select all

    #!/bin/sh
    arp -d 10.1.0.123 && ping -c3 10.1.0.123
    
    Make cron execute that script as root, say, every 15 minutes.
  • on your host's command line, type the following as root and wait. Replace b8:27:eb:87:ab:fd by your rpi's mac address and eno1 by the name of your interface on the host (eth0, eth1 etc...):

    Code: Select all

    # tcpdump -i eno1 -e ether src host b8:27:eb:87:ab:fd and arp
    
  • when the cron'ed script runs on the rpi, you should see the following on your host:

    Code: Select all

    tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
    listening on eno1, link-type EN10MB (Ethernet), capture size 262144 bytes
    09:15:29.361989 b8:27:eb:87:ab:fd > ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 60: Request who-has 10.1.0.123 tell 10.1.5.128, length 46
    
  • in my example, the rpi IP address is 10.1.5.128
Last edited by ripat on Sun Nov 22, 2015 7:09 am, edited 2 times in total.
Using Linux command line usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.

User avatar
ChAoS
Posts: 34
Joined: Wed Aug 31, 2011 4:57 am
Location: Hampshire, UK

Make It *Tell* You The Address?

Sat Nov 21, 2015 1:34 pm

Connect a pair of headphones and have it *tell* you its IP address:

Code: Select all

flite -voice kal16 -t "i p address is "
echo "$(hostname -I)" | sed 's/./& /g' | sed 's/\./ dot /g' | flite -voice kal16
Regards,

Mark.

P.S. I've no idea what it says if it doesn't *have* any IP address... :)

GordonTGopher
Posts: 70
Joined: Sat Dec 01, 2012 11:25 am
Location: Letchworth Garden City

Re: ssh Raspberry pi by using MAC adress

Sat Nov 21, 2015 2:11 pm

Something like this might help; http://4tronix.co.uk/store/index.php?rt ... uct_id=377

Gordon

ripat
Posts: 191
Joined: Tue Jul 31, 2012 11:51 am
Location: Belgium

Re: Make It *Tell* You The Address?

Sat Nov 21, 2015 5:06 pm

ChAoS wrote:Connect a pair of headphones and have it *tell* you its IP address:
And how to connect to the rpi if you don't know its IP? Oh wait, lets put that command in cron and let it tell that IP every minutes until you smash it with a hammer? ;)

Funny and simple solution anyway. Like it. And if the rpi is on top of a mast, will be even more funny. Want to see the pictures of the OP climbing up the mast with his headphones!
Using Linux command line usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.

emma1997
Posts: 264
Joined: Sun Nov 08, 2015 7:00 pm
Location: New England (not the old one)

Re: ssh Raspberry pi by using MAC adress

Sat Nov 21, 2015 5:34 pm

Is it possible to tell Pi to use TXD0 instead of HDMI? TTY something in CONFIG.TXT? IIRC original Unix used that by default. If so should be easy to get IP with any terminal, Arduino, or cheap Ebay serial display.

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

Re: ssh Raspberry pi by using MAC adress

Sat Nov 21, 2015 6:49 pm

emma1997 wrote:Is it possible to tell Pi to use TXD0 instead of HDMI? TTY something in CONFIG.TXT? IIRC original Unix used that by default. If so should be easy to get IP with any terminal, Arduino, or cheap Ebay serial display.
It does use TxD by default as well as the video output. You can connect a terminal, but it must use 3.3V logic, not 5V like Arduino or +/-12V like RS232.

User avatar
ChAoS
Posts: 34
Joined: Wed Aug 31, 2011 4:57 am
Location: Hampshire, UK

Re: Make It *Tell* You The Address?

Sat Nov 21, 2015 7:28 pm

ripat wrote:lets put that command in cron
Funny you should say that - that's *exactly* what my RPi does. It's a prototype Internet Radio and, in order to control it, I have to know its IP address. (That changes when it's connected to a phone-generated hotspot.)
Funny and simple solution anyway. Like it.
Glad you like it. :)
if the rpi is on top of a mast, will be even more funny. Want to see the pictures of the OP climbing up the mast with his headphones!
My "Internet Radio" *should* be in a box but, right now, it's "nailed" to a piece of wood:

Image

Regards,

Mark.

P.S. Jus' tryin' to fink outside of the box, guv'nor. :)

emma1997
Posts: 264
Joined: Sun Nov 08, 2015 7:00 pm
Location: New England (not the old one)

Re: ssh Raspberry pi by using MAC adress

Sun Nov 22, 2015 2:03 am

rpdom wrote:It does use TxD by default as well as the video output.
Wow. I was amazed to see this is actually the case. 115kbps active low. Like OP I want to fetch IP but without TV attached and this looks like a possible solution.

It looks like "raspberrypi login:" is the last line printed before GUI boots. Would there be an easy way to output result of hostname -I last instead by modifying config.txt or other script? Something without recompiling?

Return to “Beginners”