Bash script for checking if a host is on the local network

I'm trying to write a script that allows you to enter your machine name, and then lets you know if the host is on the local network. Here's what I have:

#!/bin/bash
echo "Please enter the host you would like to ping:"

read host

if ping -c 1 -W 1 "$host";

    then

        echo "$host is up"

    else

        echo "$host is down"

fi

This allows me to enter my machine name (ubuntu) but doesn't actually let you know if it's on the local network or not (aside from looking at the IP and knowing that it's local).

I asked my TA for advice and after a week, his only response was 'ruptime.'

I'm not too sure how to incorporate that into a script that does what the question is asking. Any help or advice would be appreciated. Thanks!

You would need to use grep to parse for the targets IP.
Then do some string regex to get the important part (127.* 192.*).

Further, this sounds like homework, is it?

Yes it is, should the question have been asked somewhere else?
And thanks for the tips on grep

Yes, there would have been - and still is:

Welcome & hth :slight_smile:

1 Like