LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help: Bash Script (https://www.linuxquestions.org/questions/linux-newbie-8/help-bash-script-719864/)

lenny168 04-17-2009 10:48 AM

Help: Bash Script
 
Hello all, i am new to bash scripting and have spent hours trying to figure out why the following script will not work.

for host in 192.168.1.{100..109};
do ping -c 1 -t 1 $host > /dev/null 2> /dev/null
if [ $? -eq 0 ];
echo $host
fi
done > hostup.txt

The script works fine when run on my own computer.

However i am trying to apply the same script at work where the ip address range is different, e.g. (192.168.1.{1..55}) upon running this script remotely from my system., i get the error 'ping unknown host 192.168.1.{1..55}. Any help on this matter would be greatly appreciated!

Thanks

malekmustaq 04-17-2009 11:33 AM

"==However i am trying to apply the same script at work where the ip address range is different, e.g. (192.168.1.{1..55}) upon running this script remotely from my system., i get the error 'ping unknown host 192.168.1.{100.109}. Any help on this matter would be greatly appreciated!=="

Did you change the 'for' line into effective ip block in that given subnet?

Does the given network has an effective dns?

192.168.xxx.xxx is an intranet class ip. If you ping an address beyond what the subnet block assigned it cannot resolve unless the router provides an effective dns to resolve the query.

Hope it helps.

Disillusionist 04-17-2009 11:46 AM

Quote:

Originally Posted by lenny168 (Post 3512312)
i get the error 'ping unknown host 192.168.1.{100.109}.

Is it possible that you only have one dot between the numbers?

EG:-
Code:

for IP in 192.168.1.{100.109}
do
  echo "IP: $IP"
done

Instead of
Code:

for IP in 192.168.1.{100..109}
do
  echo "IP: $IP"
done


lenny168 04-17-2009 01:04 PM

thanks for the replies! Basically when i remotely login into the system and carry out the same command in a terminal, it works with no problems, e.g. I get a text file called hostup.txt with the IP addresses that are up.

However if i save the commands in a script (test.sh). When i run it i get the ping error.

Disillusionist 04-20-2009 01:12 AM

The following code works as intended:
Code:

#!/bin/bash

for host in 192.168.1.{1..55}
do
  ping -c 1 -t 1 $host > /dev/null 2>&1

  if [ $? -eq 0 ]
  then
      echo $host
  fi
done > hostup.txt

I suspect you have a couple of typo's in your test.sh script.

Please compare your code with the version you have posted.

If your code was written the way you posted it, you would not get an error message, as they would be directed to /dev/null

If you run:
Code:

ping -c 1 -t 1 192.168.1.{1.55} > /dev/null
Then you get the stated error:
ping unknown host 192.168.1.{1.55}

If you run:
Code:

ping -c 1 -t 1 192.168.1.{1..55} > /dev/null
Then you get the usage information for ping.

If you run:
Code:

ping -c 1 -t 1 192.168.1.{1.55} > /dev/null 2>/dev/null
Then you get no output


All times are GMT -5. The time now is 10:30 PM.