Question relate to AWK


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Question relate to AWK
# 1  
Old 04-25-2008
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  
Old 04-25-2008
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  
Old 04-25-2008
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  
Old 04-25-2008
Yeah, sorry, I left that off, you need one more backquote at the very end of the long line.
# 5  
Old 04-25-2008
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  
Old 04-25-2008
Yes, I aware that I shouldn't need to put the root in the script. Thanks for your reminder Smilie

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  
Old 04-25-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to relate ipcs id or cpid to process?

Hi, we have multiple database instances running on solaris server like db1, db2 and db3. Below shown ipcs -pmb shared memory segment output. Using cpid value I want to relate to the database instances db1, db2 and db3. Please let me know how to do this? $ ipcs -pmb IPC status from <running... (9 Replies)
Discussion started by: baladelaware73
9 Replies

2. Shell Programming and Scripting

awk question : system output to awk variable.

Hi Experts, I am trying to get system output to capture inside awk , but not working: Please advise if this is possible : I am trying something like this but not working, the output is coming wrong: echo "" | awk '{d=system ("date") ; print "Current date is:" , d }' Thanks, (5 Replies)
Discussion started by: rveri
5 Replies

3. Shell Programming and Scripting

question about awk

Mike Harrington:(510) 548-1278:250:100:175 Christian Dobbins:(408) 538-2358:155:90:201 Susan Dalsass:(206) 654-6279:250:60:50 Archie McNichol:(206) 548-1348:250:100:175 Jody Savage:(206) 548-1278:15:188:150 Guy Quigley:(916)... (4 Replies)
Discussion started by: JA50
4 Replies

4. Programming

help with C++ code that relate the object with physical address

I need some help to write a C++ code that read and write the register of a sequencer. I have to make a code that relate the objects with the physical address but I am a bit confuse. Could someone suggest me how to proceed? in which parts do I split the code? thanks (1 Reply)
Discussion started by: silviafisica
1 Replies

5. Solaris

I have LUN ID, how to find disk relate to that LUN ID?

I have a list of LUN ID, my task is to find if disk has been added or not. How do I do that? I have been searching the forum and not able to find answer. thanks (4 Replies)
Discussion started by: uuontario
4 Replies

6. UNIX for Dummies Questions & Answers

Basic awk question...getting awk to act on $1 of the command itself

I have a script problem that I am not able to solve due my very limited understanding of unix/awk. This is the contents of test.sh awk '{print $1}' From the prompt if I enter: ./test.sh Hello World I would expect to see "Hello" but all I get is a blank line. Only then if I enter "Hello... (2 Replies)
Discussion started by: JasonHamm
2 Replies

7. Shell Programming and Scripting

Need to relate Radius log entries to DHCP ones

Hi, let say i have 2 log files(they are below) dhcp and radius. What i need is to put the information to a file just from good connections ( Auth: Login OK ) others just ignore. And the information should look like: time, login name, mac, ip, server. But the trouble is that from radius log i get... (10 Replies)
Discussion started by: wrwe
10 Replies

8. HP-UX

question on awk

i have bench of files and i have archeive in different names FOR EX: the files come in the directory as 1111_2222_3333_4444_5555_6666_7777.dat.xls.ZIP XXXX_VVVV_3333_4444_5555_6666_7777.dat.xls.ZIP I have to select the files from 4 th position to last position and concaTENATE THEM ... (1 Reply)
Discussion started by: r_satya7
1 Replies

9. Shell Programming and Scripting

awk .. question?

i have a little awk script that I use looks this: awk '{if (FNR==1){print FILENAME; print $0}else print $0}' file1...file2....fi... > bundled. i have completely forgotten how to unbundle this. I have tried several different approaches and still can not remember how to unbundle the file bundled.... (2 Replies)
Discussion started by: moxxx68
2 Replies

10. Shell Programming and Scripting

Another Awk Question! ;)

The variable "NF" holds the number of fields on the current record. Is there a variable that returns the current field within the record being processed? Given a record of "n" fields (variable number of fields for a given record), how can you tell which field you are actually on when you match a... (5 Replies)
Discussion started by: google
5 Replies
Login or Register to Ask a Question