Score:2

How to write a Device Discovery Tool using Bash

ng flag

I have an embedded PC running Linux server and a simple web server which is used in agriculture industry, my customers buy these PCs and use them in their fields. ( they install it on a pole 6 or 7 meters above ground level and it is very hard to access) sometimes they change the range of these PCs IP address from original one to their own desired IP Range. for example I ship them with 192.168.10.10/24 but they might change it to something like 172.16.12.19/29 or 10.10.200.10/8 or ....

the problem is that after a while they forget the PC IP Address and we want to give them a tool to detect and show them the current IP Address.

I thought maybe using Broadcast IP address and sending a message over local network might help to solve this problem. but after some search I realized I have to know the range at least for example (172.16.12.X or 10.10.200.X) which is not possible for me because the farmers forgot the network range too. Is there any tool like "NC" in Linux which can help me to write a bash script to find their IP ? I have network access to the PC but I cannot connect a Monitor to the PC so the only way is to find the IP using the local network.

I want something like this :

echo "What is your IP Address ? " | nc -w1 -u 4545

and I put something on the PC which answers to this message whenever they got the exact phrase What is your IP Address ? "

nc -l -u -p 4545

but for the nc I have to specify a network range which is not possible for me.

Edit:

if you have worked with Device Discovery tool (DDT) for IP Cameras you know that they will find the camera IP independent of the PC which has the DDT on it. I want something exactly like those program but with bash commands.

CrazyTux avatar
us flag
Are those pcs connected to the internet? Or they are all in the same local lan? Forthemore, after forgetting the ip and the ip range, does they have a shell access to the servers?
ng flag
@CrazyTux they are only connected to local lan, after forgetting the ip they dont' have access to the shell
CrazyTux avatar
us flag
Is there a server that all other servers or cpstumers have access to? Or there is an smtp service on the servers?, i think i may have a solution, lets chat.
ng flag
@CrazyTux no These customers doesn't have access to each other, these PCs are private to each customer and they have their own configuration. unfortunately they don't have smtp server,
CrazyTux avatar
us flag
So lets say that we create a Device Discovery tool, if they don't use it and they will forget their ip, will they lose the access to their pc? or they could connect it somehow? if they will lose the access to the pc, the tool should send a message with the current ip to some other device somehow, if they still have an access to the server after they forget their ip, then they can just check it with `ifconfig` is it correct or i am missing something?
sudodus avatar
jp flag
Is it possible, without climbing the poles, to reset those devices to 'factory settings', that you have in your records? If not, I think it would help if you manage to make that possible.
Unix avatar
cn flag
Could it be possible to use bash aliases via ssh? For example: `alias ipa="nc -w1 -u 4545"`, so they don't have to enter an exact phrase, just the alias `ipa`.
sancho.s ReinstateMonicaCellio avatar
pl flag
What version of Ubuntu are they running?
sancho.s ReinstateMonicaCellio avatar
pl flag
Is it an acceptable solution that, instead of getting a way to know the new IP, they recover the old IP? If so, you could set a crontab task that checks the current IP, and whenever it deviates from a set value, changes it back.
sudodus avatar
jp flag
`@reboot` or at some interval? Would the farmers accept, that they cannot change the IP address on their own, but must stay with the preset value set by your company and remembered by your company?
James S. avatar
de flag
How are the IP addesses being assigned to this embedded device? Static or DHCP? Who has access to the LAN these devices connect to? can they just look at the ARP table on the router, grep for the MAC address of your embedded device, and voila?
James S. avatar
de flag
netcat (`nc`) is the wrong tool to use here. Have you tried `nmap`?
Robert Riedl avatar
us flag
how do they get the alerts or sensor data, if they forget the IP ?
ng flag
@RobertRiedl The sensors are not connected using LAN, they are connected using RS485 and CAN Bus, the PC is only used to monitor the sensors
ng flag
@JamesS. Dear James, as I told earlier the sensors are connected using serial communication bus to the PC, they don't have any LAN connection. No DHCP server is used here. the farmers connect the PC we have with their own LAPTOP/PC to monitor the sensors.
ng flag
@JamesS. how to use the nmap here ?
Robert Riedl avatar
us flag
So, in order to check the sensors, they connect via their own Laptop/PC to this Ubuntu Server.. via SSH/Web (?) to the IP address ? Or is there an app that loads the sensor data on the Farmer's PC ? The best way to keep track of the IP would be, if the Ubuntu Server reports the IP somewhere, regularly.
sancho.s ReinstateMonicaCellio avatar
pl flag
@RobertRiedl - This is part of [this answer](https://askubuntu.com/a/1359269/226614).
Robert Riedl avatar
us flag
@sancho.sReinstateMonicaCellio you mean the "broadcast" part of your answer ? Well, yes, partly. For me it makes a difference if the User has to type in the IP into the browser, or if there is a DNS name or if they fire up an app etc. If they have an app, then maybe we can somehow display the IP in the app.. if its a DNS name, then this should be resolvable etc.. otherwise, see my answer on how to broadcast with `netcat`
Score:1
cn flag

I guess the farmers are reconfiguring it because their local network address changes. In which case, why not allow the server to get it's address over DHCP, you could make a note of the Mac address (which won't change) and then search the DHCP servers tables or even try a lookup on the DNS server to find the pc. Unpluging the cable from the end at the switch and plugging it back in should initiate a DHCP configuration session. Most DHCP servers allow you to assign an address based on a Mac address so you have some control over it at the dhcp server end.

ng flag
they don't have any DHCP server, they have only this PC with so many sensors like (gas, temperature and ....)
SEWTGIYWTKHNTDS avatar
cn flag
In that case, make Your Server the DHCP server and hand out IP addresses to the clients that wish to connect. Then there is no problem finding the IP address.
jpbrain avatar
ca flag
Hi @Parsa, From reading this seems that the easiest way is to setup a DHCP/mdns server on the server. That way, you connect to it with DHCP on the client and you wiil be able to scan the subnet or just connect to the default route.
Score:1
pe flag

If you use avahi on your embedded PC you won't have to re-invent/re-implement service-discovery

On embedded PC:s:

apt-get install avahi-daemon
systemctl enable avahi-daemon --now
un-comment #publish-workstation=yes in /etc/avahi/avahi-daemon.conf
systemctl reload avahi-daemon

On machine used to discover embedded PC:s

apt-get install avahi-utils
avahi-browse -atr #  will output names and ip:s of devices on network to terminal
Score:1
pl flag

As posted, the problem looks too wide. As I understand, you have two different problems:

  1. Generate a method to avoid the problem in future cases.
  2. Access to the servers that currently have the problem, to implement solution 1.

For problem #1, instead of getting a way to know the new IP, you could have the servers recover the old IP to a fixed value. You could set a crontab task that checks the current IP, and whenever it deviates from a set value, it changes the IP back to the target. You will of course have time windows of "network-blindness", depending on the frequency set in crontab.

Alternatively, a crontab task could broadcast/send email/etc. the new IP. The right mechanisms depend very much on what are you willing to operate, how are you accessing the servers.

All this seems a workaround. If correctly configured, they should not "forget the PC IP address". This is the point to fix, I guess. If you could give more information on the current configuration, that will narrow the problem.

For problem #2, if that is your target please mention it. I have a "similar" problem. A server that I am accessing remotely changes IP once in a while, since it is assigned by DHCP. Whenever I have problems sshing to the server, I execute a small script that, by a combination of ping and ssh (within a loop of IPs), and checking on the response, can get the current IP. Depending on many aspects of your network configuration this may work for you or not.

Score:1
us flag

With netcat you could do something like a cronjob on your "pole-mounted servers" that does something like this every X minutes:

#!/bin/bash

# replace eth0 with the name of your interface or get the IP via other means

ip="$(ifconfig | grep -A 1 'eth0' | tail -1 | cut -d ' ' -f 10)"
datum=$(date)
echo "This is my IP: "$ip | netcat -q 2 -b -u 255.255.255.255 12001

The User/Farmer then only has to listen like this:

echo | nc -ulp 12001

And after X minutes they should get something like this:

This is my IP: 192.168.1.121

This only works if the "Farmers PC" and your "pole-mounted servers" are in the same subnet, though.


Explanation:

The script will send a UDP broadcast on Port 12001 and exit nc, after the payload (our echo command) + 2 seconds. The IP doesn't have to be got via ifconfig obviously, but its quite easy to parse..

Sending it to 255.255.255.255 means broadcast to the whole subnet, regardless of size.

So we don't have to know which network we are on.

On the client side, we listen with nc until we get a payload. I found nc terminates cleanly in combination with a line-break (via the echo).

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.