LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-17-2009, 11:48 AM   #1
lenny168
LQ Newbie
 
Registered: Apr 2009
Posts: 6

Rep: Reputation: 0
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

Last edited by lenny168; 04-17-2009 at 01:59 PM. Reason: type erroe
 
Old 04-17-2009, 12:33 PM   #2
malekmustaq
Senior Member
 
Registered: Dec 2008
Location: root
Distribution: Slackware & BSD
Posts: 1,614

Rep: Reputation: 440Reputation: 440Reputation: 440Reputation: 440Reputation: 440
"==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.
 
Old 04-17-2009, 12:46 PM   #3
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,036

Rep: Reputation: 96
Quote:
Originally Posted by lenny168 View Post
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
 
Old 04-17-2009, 02:04 PM   #4
lenny168
LQ Newbie
 
Registered: Apr 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
Old 04-20-2009, 02:12 AM   #5
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,036

Rep: Reputation: 96
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

Last edited by Disillusionist; 04-20-2009 at 02:18 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 05:11 AM
[SOLVED] bash : getopts problem in bash script. angel115 Programming 2 03-02-2009 11:53 AM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 07:00 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 08:01 PM
[bash] having trouble debugging this bash script. jons Programming 4 02-08-2007 07:51 AM


All times are GMT -5. The time now is 05:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration