Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 and\nI will ring the system bell when I am done.\nHere we go..."
for i in 192.168.0.{1..254}
echo "scanning $i"
if [ "$(ping -q -c1 $i)" ]
echo -e "AHA! Got one! ---- $i is up!"
echo "END SCAN"
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"

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

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
is this a fantastically elaborate way of doing `nmap -sP' ?? – 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
@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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .