Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-19-2012
Registered User
 
Join Date: Mar 2010
Posts: 23
Thanks: 17
Thanked 0 Times in 0 Posts
[Solved] For Loop with 2 commands? (hostname prob)

Hi

I would like to ssh into serveral servers, print the hostname and do an ls (of files in a certain directory), and add these details to a file.

so i have this:

Code:
#!/usr/bin/bash

for host in hostnames_here
do

ssh $host echo "$HOSTNAME"  ls /directory_location > ~/new_file_name.txt

done

it doesn't work for me, it lists the files but doesn't echo/print the hostname.......which i need to distinguish between servers.

any help gladly received!
Sponsored Links
    #2  
Old 06-19-2012
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Location: Switzerland - GE
Posts: 4,636
Thanks: 118
Thanked 257 Times in 246 Posts
host is also a command so it is not wise at all to use it as a variable name...
Quote:
for host in hostnames_here
What is hostnames_here ? A file?
The Following User Says Thank You to vbe For This Useful Post:
horhif (06-19-2012)
Sponsored Links
    #3  
Old 06-19-2012
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Location: Switzerland - GE
Posts: 4,636
Thanks: 118
Thanked 257 Times in 246 Posts
And what doesnt work?
The Following User Says Thank You to vbe For This Useful Post:
horhif (06-19-2012)
    #4  
Old 06-19-2012
Registered User
 
Join Date: Mar 2010
Posts: 23
Thanks: 17
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by vbe View Post
host is also a command so it is not wise at all to use it as a variable name...
What is hostnames_here ? A file?

no, sorry i left out the names of the servers trying to help.

hostnames_here would be the server names i.e. server1 server2 server3 etc..

---------- Post updated at 06:49 AM ---------- Previous update was at 06:43 AM ----------

Quote:
Originally Posted by vbe View Post
And what doesnt work?
so what i'd like to see would be (in the output file)

servername1
file1_in_server1
file2_in_server1
(SPACE)
servername2
file1_in_server2
file2_in_server2
(SPACE)
etc etc

what i get now is just
filename1
filename2
filename3
filename4

Just one long list of file names.

I am not getting the name of the server in the list, so am not sure what files are from what server. There is no break in the list....i.e. not showing server1 / server 2 etc.
Sponsored Links
    #5  
Old 06-19-2012
jayan_jay's Avatar
Forum Advisor
 
Join Date: Jul 2008
Posts: 831
Thanks: 9
Thanked 185 Times in 176 Posts
If your HOSTNAME variable value is set in all servers, then you will retrieve it via ssh

Code:
$ env | grep -w HOSTNAME
HOSTNAME=xxxxxxxxx

Instead of it, go with the hostname command

Code:
ssh $host " hostname ; ls -ltr /dir_location "

Hope this helps ..
The Following User Says Thank You to jayan_jay For This Useful Post:
horhif (06-19-2012)
Sponsored Links
    #6  
Old 06-19-2012
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Location: Switzerland - GE
Posts: 4,636
Thanks: 118
Thanked 257 Times in 246 Posts
As Jayan_jay suggests HOSTNAME is not initialized or it is the one from source machine so you have to use a command: hostname then to get 2 command executed you need to use the double quotes to "make your command "look" as one.
The Following User Says Thank You to vbe For This Useful Post:
horhif (06-19-2012)
Sponsored Links
    #7  
Old 06-19-2012
Registered User
 
Join Date: Mar 2010
Posts: 23
Thanks: 17
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by jayan_jay View Post
If your HOSTNAME variable value is set in all servers, then you will retrieve it via ssh

Code:
$ env | grep -w HOSTNAME
HOSTNAME=xxxxxxxxx

Instead of it, go with the hostname command

Code:
ssh $host " hostname ; ls -ltr /dir_location "

Hope this helps ..
that did it for me! thanks very much everyone!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[Solved] Use of until loop for user confirmation anuragpgtgerman Shell Programming and Scripting 4 04-27-2012 11:38 AM
[Solved] Value of a variable is not recognised for commands comes from external file rbalaj16 Shell Programming and Scripting 5 11-01-2011 11:33 AM
[Solved] Problem with mail commands & format karumudi7 Shell Programming and Scripting 8 10-18-2011 01:57 PM
[SOLVED] for loop to process files graysky Shell Programming and Scripting 1 09-06-2011 04:03 PM
prob's with while loop pssandeep UNIX for Dummies Questions & Answers 2 01-11-2008 05:22 PM



All times are GMT -4. The time now is 08:21 PM.