![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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 |
|
||||
|
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 |
|
||||
|
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. |
![]() |
| Bookmarks |
| Tags |
| linux |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|