Setup a webserver on the Pi that returns "This is the Pi of iam_agf". Or even a dynamic page that returns the IP, something like "192.168.0.15 iam_agf". This is to make sure that it is your device and not he printer or coffee machine.
You can use nmap to see which IPs have a webserver running, and then check those to see if the contain your keyword and the IP.
Once you get back an IP, place it in your /etc/hosts file, so you can easily access the pi by name.
Simple check if it works without nmap, might take some time:
for i in $(seq 1 254); do
curl "http://192.168.0.${i}" | grep iam_igf
done
There's a plugin for nmap called ssh-brute, you can try this with one fixed username and password combination. https://nmap.org/nsedoc/scripts/ssh-brute.html
Also what you were probably looking for initially:
#this will open an ssh connection and wait for you to type stuff:
ssh user@IP
#this will run a command after the connection is open, then close it
ssh user@IP command
# example
ssh user@IP echo ok
# or just use the true command to avoid output
ssh user@IP true
# so this loop might do what you need, (untested, make backups of /etc/hosts first!)
for i in $(seq 1 254); do
if ssh "[email protected].${i}" true;
then
sed -i "/[0-9.]* mypi/d" /etc/hosts
echo "192.168.0.${i} mypi" >> /etc/hosts
break
fi
done
May