Passing file name to get uptimes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing file name to get uptimes
# 1  
Old 03-14-2012
Passing file name to get uptimes

Hi:

I am very new to shell scripting, and started writing a script today. I am trying to read a server name from a list that stores server name and passing each server name to get the uptime. What i would to achieve is that, i should get a file with server names along with their uptimes on a separate line.
For example:
Code:
Server1 Uptime
server2 Uptime

..and so on

My script:
Code:
server_list=`/tmp/hosts.list`

for server in $server_list
do
    while read line
    do
       cut -f1 -d" " //hosts.list has other info like IP address
       upt=`/usr/local/bin/ssh $server uptime`
       printf $server $upt
    done<$server
done

when i run the script i get the following error:
Code:
/tmp/hosts.list[1] : server1: not found
/tmp/hosts.list[2] : server2: not found

...and so on

Any help is greatly appreciated!

Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples, thank you.

Last edited by Franklin52; 03-14-2012 at 05:26 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 03-14-2012
That's a useless use of backticks and not even correct syntax for that.

I'd try this:

Code:
# First column goes in server, all the rest go in OTHERSTUFF
while read SERVER OTHERSTUFF
do
        /usr/local/bin/ssh "$SERVER" printf "${SERVER}: " ';' uptime
done < /tmp/hosts.list

This will print the server's name on the server itself, then run uptime, doing everything in one neat operation with no backticks whatsoever. The quotes around the semicolon are mandatory -- without that, uptime will run locally instead of being considered part of the ssh command...
These 2 Users Gave Thanks to Corona688 For This Post:
# 3  
Old 03-15-2012
Thanks Corona...can you also help me here

the updates you made to the script displays only one row and i wanted all 50 servers to be displayed.
My changes are not working either:

Code:
server_list='/tmp/hosts.list'

for SERVER in $server_list
do
    while read line
    do
    /usr/local/bin/ssh "$SERVER" printf "${server} : " ';' uptime
    done < $server_list
done

Errors i am getting:
Code:
ssh: /tmp/hosts.list: no address associated with name
ssh: /tmp/hosts.list: no address associated with name
ssh: /tmp/hosts.list: no address associated with name
.....

# 4  
Old 03-15-2012
ignore this...

see my other post...(below)

Last edited by itkamaraj; 03-15-2012 at 10:59 AM..
# 5  
Old 03-15-2012
still the similar error:

Code:
ssh: no address associated with name
ssh: no address associated with name
ssh: no address associated with name
....

# 6  
Old 03-15-2012
Code:
server_list='/tmp/hosts.list'
while read server
do
   uptime=$(ssh $server "uptime")
   echo "$server $uptime" >> output.txt
done < $server_list

# 7  
Old 03-15-2012
this doesnt work either
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Passing a file name to a variable

Below is the command mv AP_FLEXCUBE_INTERFACE10.txt FTPYMNTE_`date "+%Y%m%d%H%M%S" | tr '' ''`.TXT it is changing the file name to a different name according to time stamp dynamically. I want to capture that dynamic file name present in the directory to a variable . After that i want to... (6 Replies)
Discussion started by: sujit das
6 Replies

2. Shell Programming and Scripting

Help with passing parameters from a file

Hello Everyone, I have developed a shell script which takes schema id and password as parameter to login into database using sqlplus,runs a query and mails the result. My requirement is that, I dont want to pass userid and password as parameters.Instead,I want to pass say Environment... (4 Replies)
Discussion started by: karthik adiga
4 Replies

3. Shell Programming and Scripting

Passing parameter through file

Hi , I am passing date parameter through file my shell script testing.sh is #set -x #set -v asd=$1 asd1=$2 echo $asd echo $asd1 Passing parameter as below sh testing.sh `cat file1.txt` Output (2 Replies)
Discussion started by: kaushik02018
2 Replies

4. UNIX for Dummies Questions & Answers

passing command arguments from a file?

Hi Im trying to run zip shell command from an Oracle job, but this has limitations. This should take a few of explanaition,.. Oracle allows me to execute a command and then I can set up a fixed number of arguments. Ex: (summarizing in something like..): JOB DEFINITION job_name: test... (4 Replies)
Discussion started by: vegatripy
4 Replies

5. Shell Programming and Scripting

Reading a string and passing passing arguments to a while loop

I have an for loop that reads the following file cat param.cfg val1:env1:opt1 val2:env2:opt2 val3:env3:opt3 val4:env4:opt4 . . The for loop extracts the each line of the file so that at any one point, the value of i is val1:env1:opt1 etc... I would like to extract each... (19 Replies)
Discussion started by: goddevil
19 Replies

6. Shell Programming and Scripting

Splitting a file in to multiple files and passing each individual file to a command

I have an input file with contents like: MainFile.dat: 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 ... (4 Replies)
Discussion started by: rkrish
4 Replies

7. UNIX for Dummies Questions & Answers

Passing the parameters through a file

Hi All, I have written a shell script and the script is working fine, I am passing my MIT_ID(NUMBER VALUE) to the shell script from the command prompt and my script is executing as expected. Now I have to pass all the MIT_ID's from a .txt file to the shell script,as I am automating this I... (6 Replies)
Discussion started by: gaur.deepti
6 Replies

8. UNIX for Dummies Questions & Answers

Reading from a file(passing the file as input parameter)

hi I have a shell script say primary.sh . There is a file called params my scenario is primary.sh should read all the values and echo it for example i should pass like $primary.sh params output would be Abc ... (2 Replies)
Discussion started by: ssuresh1999
2 Replies

9. Shell Programming and Scripting

Passing a file descriptor

I am trying to right a function which uses a file descriptor to write to a log file. The problem is that the on the print statement the file descriptor is called bad. Now when I first open the file and print to it in the f_open function by passing the descriptor to f_print_log all works well,... (6 Replies)
Discussion started by: robotball
6 Replies

10. Shell Programming and Scripting

Passing parameters through a file

I have created a script "myscript.sh" I need to run this script with diffrent parameters. The parameters are stored in a file myparam.txt. I need to run myscript.sh script for every parameter supplied in myparam.txt. Example: If myparam.txt contains following lines: param1 param2 param3... (3 Replies)
Discussion started by: chawlaaman
3 Replies
Login or Register to Ask a Question