I'm trying to write a script that takes a machine name as a command line argument and displays a message informing me whether the host is on the local network.
Here's what I have:
#!/bin/bash
gateway=$(ip route | grep default | cut -d' ' -f3)
if [[ $(ip route get "$1" | grep -q $gateway) ]]; then
echo "$1 is on the local network"
else
echo "$1 is not on the local network"
fi
Whenever I run it with my machine name (ubuntu) I get the following output:
Error: any valid prefix is expected rather than "ubuntu".
ubuntu is not on the local network
I'm not sure what the error means and why I'm getting it, as well as if the script is even doing what it should be doing. Any advice would be appreciated. Thanks!
ip route get
expects an IP prefix (e.g.,ip route get 192.168
), not a hostname. Convert the hostname to an IP, maybe.