Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
    
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 Feb 27 '13 at 22:45
    
No, I am running this script as root. I'll be fine with whatever you can think of –  Sirens Feb 27 '13 at 22:48
1  
is this a fantastically elaborate way of doing `nmap -sP 192.168.0.1/24' ?? –  Sirex Feb 27 '13 at 22:56
    
No. I need this to work without installing stuff. Is there anyway you can run a command for each IP that is up? –  Sirens Feb 27 '13 at 22:58
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 Feb 27 '13 at 23:02

2 Answers 2

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.)

share|improve this answer
1  
That'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 Feb 27 '13 at 23:03

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.

share|improve this answer
    
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. –  grawity Feb 28 '13 at 1:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.