Skip to content

Instantly share code, notes, and snippets.

@mskf3000
Forked from s1037989/README.md
Created October 7, 2023 08:01
Show Gist options
  • Save mskf3000/f37a81c72fdf6c7ab65590737b437224 to your computer and use it in GitHub Desktop.
Save mskf3000/f37a81c72fdf6c7ab65590737b437224 to your computer and use it in GitHub Desktop.
ip-discover: Setup a device for discovery on a network using socat

ip-discover

Setup a device for discovery on a network using socat

On the device to be discovered:

  • Copy ip-discovery to /usr/bin and run chmod +x /usr/bin/ip-discovery
  • Copy ip-discovery.service to /etc/systemd/system, adjust the code-word, and run sudo systemctl daemon-reload && sudo systemctl start ip-discovery

On the device wanting to discover the device to be discovered:

  • Copy ip-discover to your home folder and run . ~/ip-discover
  • Discover the device to be discovered by running ip-discover code-word
  • Update your SSH config and see the changes by running ssh-ip-discover code-word
# $ sudo tcpdump -v -n -A "(src host 192.168.0.176 and dst port 1900) or (src host 192.168.0.251 and dst port 1900)"
use 5.010;
use IO::Socket::Multicast;
my $s = IO::Socket::Multicast->new(LocalPort=>1900, ReuseAddr=>1) or die $!;
$s->mcast_add('239.255.255.250');
$s->mcast_loopback(0);
$_ and say $_ while $_ = search($s);
sub search {
my $s = shift;
sleep 1;
$s->mcast_send(qq(M-SEARCH * HTTP/1.1\r\nMx: 5\r\nSt: ssdp:all\r\nMan: "ssdp:discover"\r\nUser-Agent: UPnP/1.1 mac/1.0 upnpx/1.0\r\nConnection: close\r\nHost: 239.255.255.250:1900\r\n), '239.255.255.250:1900');
$s->recv(my $data, 1024);
return $data;
}
From: https://github.com/dannybackx/arduino-upnp/blob/master/libraries/UPnP/examples/UPnP/scripts/how-to-socat
Monitor messages in the UPnP multicast group :
socat UDP4-RECVFROM:1900,ip-add-membership=239.255.255.250:0.0.0.0,fork -
Send a message to UPnP via multicast :
cat >x <<end
NOTIFY * HTTP/1.1
HOST: 239.255.255.250:1900
NT: upnp:rootdevice
NTS: ssdp:alive
CACHE-CONTROL: max-age=1200
SERVER: Arduino/1.0 UPNP/1.1 UPnP Motion Sensor Kit/32302000102
USN: uuid:38323636-4558-4dda-9188-cda0e606724c
LOCATION: http://192.168.1.99:80/this-is-fake-description.xml
end
cat x | socat - UDP4-DATAGRAM:239.255.255.250:1900,bind=:2900,ip-add-membership=239.255.255.250:eth0
ip-discover ()
{
which socat &>/dev/null || { echo "missing socat"; return 1; }
while :; do
ip="$(echo -n ${1:-ip-discovery} | socat STDIO UDP4-DATAGRAM:255.255.255.255:6666,broadcast,range=0.0.0.0/0)";
test -n "$ip" && {
echo $ip;
break
} || echo -n . >&2
sleep 1;
done
}
ssh-ip-discover ()
{
[ -z "$1" ] && { echo "Usage: $FUNCNAME host"; return 2; }
config=${SSH_CONFIG:-~/.ssh/config}
perl -pi.bak -E 'BEGIN { $/="\n\n"; ($host, $ip) = (shift @ARGV, shift @ARGV); } next unless /^Host $host/i; s/Hostname\s+([\d\.]+)/Hostname $ip/is' $1 $(ip-discover $1) $config
test -e $config.bak || return 1
echo "$config backed up to $config.bak"
diff $config.bak $config
}
#!/usr/bin/env bash
read key
[ "$key" == "${1:-${0##*/}}" ] && echo $SOCAT_IP_LOCADDR
[Unit]
Description=Allow discovery via broadcast UDP
After=network.target
[Service]
Type=simple
ExecStart=socat UDP4-RECVFROM:6666,fork,so-broadcast,so-timestamp,ip-pktinfo,ip-recverr,ip-recvopts,ip-recvtos SYSTEM:"/usr/bin/ip-discovery code-word"
# Discover IP using:
# $ echo -n code-word | socat STDIO UDP4-DATAGRAM:255.255.255.255:6666,broadcast,range=0.0.0.0/0
KillMode=process
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment