The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-25-2008
beeloo beeloo is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 20
Question relate to AWK

Hi,

I would like to setup a FOR loop script to find out all the existing linux workstation in the network w/ ip address, hostname and linux version.

I created a basic FOR loop script:

for i in $(seq 1 254)
do
echo 10.72.169.$i >> result
ssh -o ConnectTimeout=3 root@10.72.169.$i "hostname" >> result
ssh -o ConnectTimeout=3 root@10.72.169.$i "cat /etc/redhat-release" >> result
done


However, the output is like this:

.
.
10.72.169.21
lumines.devo.ilx.com
Fedora Core release 5 (Bordeaux)
10.72.169.22
10.72.169.23
10.72.169.24
copper.devo.ilx.com
10.72.169.25
frogger.devo.ilx.com
Fedora Core release 5 (Bordeaux)
10.72.169.26
afterlife.devo.ilx.com
Red Hat Linux release 9 (Shrike)
10.72.169.27
10.72.169.28
molybdenum
Red Hat Linux release 9 (Shrike)
10.72.169.29
Red Hat Linux release 7.2 (Enigma)
10.72.169.30
.
.
.

I want the output to be like :

10.72.169.21 lumines.devo.ilx.com Fedora Core release 5 (Bordeaux)
10.72.169.22 abc.devo.ilx.com Fedora Core release 5 (Bordeaux)
.
.

I think with the command AWK, i can do that. But I don't know how to make it to work. Can someone show it to me?

Thanks
BEELOO
  #2 (permalink)  
Old 04-25-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652

Code:
for i in $(seq 1 254)
do
    echo 10.72.169.$i `ssh -o ConnectTimeout=3 root@10.72.169.$i "hostname; cat /etc/redhat-release"
done >>result

  #3 (permalink)  
Old 04-25-2008
beeloo beeloo is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 20
Sorry, it doesn't work. it said:

test2: line 3: unexpected EOF while looking for matching `''
test2: line 5: syntax error: unexpected end of file
  #4 (permalink)  
Old 04-25-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Yeah, sorry, I left that off, you need one more backquote at the very end of the long line.
  #5 (permalink)  
Old 04-25-2008
fabtagon fabtagon is offline
Registered User
  
 

Join Date: Apr 2008
Location: European Union/Germany
Posts: 189
First of all it's bad practice to allow root logins via ssh. You have been warned ...

There's a backtick (`) missing at the end of the echo line.

(a better looking option would be the following inside the loop (no redirection at the end of the loop then)

echo -n 10.72.169.$i >> result
ssh -o ConnectTimeout=3 root@10.72.169.$i "hostname; cat /etc/redhat-release" >> result
  #6 (permalink)  
Old 04-25-2008
beeloo beeloo is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 20
Yes, I aware that I shouldn't need to put the root in the script. Thanks for your reminder

Back to the script.

Now it print out all one lines and keep on going. However, I want the format to go like this

10.72.169.1 bomber.abc.com fedore core 5
10.72.169.2 bomber2.abc.com fedora core 7

So it will display the ip address first, then hostname, then version, and it will go for the next ip address with new line.

How can we do it?

Please advise.

Thanks
BEELOO
  #7 (permalink)  
Old 04-25-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
The echo should do that. Any newlines returned by the backticks are turned into whitespace by the shell (when they're unquoted). Usually that's not a good thing, but here's a good opportunity to take advantage of that feature.

You can redirect inside the script, still, of course; to me, it's more elegant to do that once, at the end. Saves from opening and closing the file repeatedly.
Closed Thread

Bookmarks

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:01 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0