Find what network range user is on?

2

I am making a network scanner that will work on any network. This is my code so far

fd=$(date +%m%d%y%H%M%S)
echo -e "IP$fd\nReport of IP addresses on this intranet, test started at \n$(date)\n\nThe following IP addresses were found:" > IP$fd.txt
echo -e " Okay. Mind you, this could take a couple of minutes.\nI'll be scanning all 254 possibilities between 192.168.1.1 and 192.168.1.254\nI will ring the system bell when I am done.\nHere we go..."
for i in 192.168.0.{1..254}
do
echo "scanning $i"
if [ "$(ping -q -c1 $i)" ]
then
echo -e "AHA! Got one! ---- $i is up!"
fi
echo "END SCAN"
done
echo -e "That's all I got.\Test completed at\n$(date)\n" >> IP$fd.txt
echo -e \\a
echo -e "Your report is IP$fd.txt, and this is what it says:\n"
exit

Except the problem is that this will only work if your IP range is 192.168.0.1-255. It won't return anything if the ip is 10.10.10.1.

Is there anyway I can get the users network range and apply it to my script?

Edit: I am running as root

Sirens

Posted 2013-02-27T22:39:54.820

Reputation: 206

Can you do a DHCP request and from that gather an IP and network mask? (Though doing that from user mode might be interesting). – Hennes – 2013-02-27T22:45:29.723

No, I am running this script as root. I'll be fine with whatever you can think of – Sirens – 2013-02-27T22:48:43.503

1@TheDeveloper I think he means user mode as opposed to kernel mode. Kernel mode is lower-level and gives much more direct hardware access (as you could guess from the name), and is generally used by the kernel and drivers. User mode is more restricted for stability reasons - a kernel mode bug will crash the system. Even though you're root, you're still in user mode. – cpast – 2013-02-27T23:02:39.907

1is this a fantastically elaborate way of doing `nmap -sP 192.168.0.1/24' ?? – Sirex – 2013-02-27T22:56:51.390

No. I need this to work without installing stuff. Is there anyway you can run a command for each IP that is up? – Sirens – 2013-02-27T22:58:54.273

Well, DHCP is almost always done in user mode... On the other hand, it does require root privileges, to bind to the low ports. – user1686 – 2013-02-28T02:01:40.450

Answers

1

There is no portable way.

On Linux, you can list all IPv4 subnets the host belongs to using:

ip -4 route show scope link | awk '{print $1}'

(Note that there can be more than one.)

user1686

Posted 2013-02-27T22:39:54.820

Reputation: 283 655

1That's an important note: A computer can have as many IPs as you want. It's not restricted to a single network. Any method will have to deal with that. – cpast – 2013-02-27T23:03:21.293

0

You can extract the current network details from the output of ifconfig -a

From that you should be able to set up variables for your for-loop. It might be easier to work with integer addresses rather than dotted-quads.

There will be multiple address-ranges, some of them IPV4 and some IPV6.

To detect nearby networks to which the current machine is not directly connected would take a lot more work.

RedGrittyBrick

Posted 2013-02-27T22:39:54.820

Reputation: 70 632

I recommend avoiding ifconfig for this, as its output is not stable (it has changed a lot in net-tools 1.60), and it still does not support multiple IPv4 addresses per interface. – user1686 – 2013-02-28T01:14:07.860