Script help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script help
# 1  
Old 06-27-2012
Script help

Hi there, I am new to scripting and need your help showing me how to create a script that does ssh on multiple servers run remote commands e.g. checking a process and report the outcome to one of the servers lets name it server1, regards..Bader

Last edited by zaxxon; 06-27-2012 at 10:40 AM.. Reason: Please don't make your font smaller at the half of the posting, thanks. Did not see any reason for that and harder to read ;)
# 2  
Old 06-27-2012
What have you done so far?
# 3  
Old 06-27-2012
from the server where I need the report on, I created a list of all servers that I need to generate the report from called it server list so wrote something like this
Code:
for i in `cat /opt/scripts/server list`
do, ssh $i "ps -ef | grep process name > /tmp/report"
done

---------- Post updated at 08:00 AM ---------- Previous update was at 07:49 AM ----------

Hi Zaxxon, is there a better way to do this, again, I am still learning this and don't have a big knowledge when it comes to writing scripts

Last edited by zaxxon; 06-27-2012 at 11:32 AM.. Reason: code tags
# 4  
Old 06-27-2012
Not sure if this is a typo, but a comma will not work to separate commands. Use a semicolon or start a new indented line do; ssh ....
Also the blank between "server list" might not work. I never use filenames with a blank when I have the choice and use a dot or underscore to enhance readability of them.

You write the output of grep to each server. Don't you want that centralized on the host you are issuing this command from?

Also please use code tags when posting code, data or logs etc.

Code:
$ cat server_list
host1
host2
host3
$ while read LINE; do
     printf "${LINE}: "
     ssh $LINE "ps -ef| grep <processnameinhere>" >> mylist.out
done < server_list

By adding the printf on the line that is being read, you know which element of your list is currently being processed. The output of the ssh command is written after the host name.
Try something like this. All goes to a local file from the machine where you issue the command.
If the ssh gets stuck after executing the first cycle, add a -n to the ssh call.
# 5  
Old 06-27-2012
ssh in a loop can always be a bugger when you're first starting off in scripting. Firstly, the reason it can be such a bugger is because of how ssh handles terminals, when the ssh command completes it exits out of the terminal which in turn will cause your script to fail. Very frustrating! Smilie

So, to resolve this you can do 1 of 2 things. You can either tell ssh to use a null terminal or you can use exec to read in your file which will in turn cause your loop to iterate appropriately.

Also, make sure that when you're doing your output redirection that you're appending (>>) vs. rewriting (>)
ssh null terminal
Code:
for i in $(cat serverlist.list)
do
   ssh -n $i "ps -ef | grep process_name" >> /tmp/report
done

exec ssh
Code:
exec < server.list
while read SERVER; do 
    ssh $SERVER "ps -ef | grep process_name" >> /tmp/report
done

---------- Post updated at 09:48 AM ---------- Previous update was at 09:46 AM ----------

Good point, I didn't catch that his quotes were around the whole of the command. Smilie. Yes, depending on where your quotes are... it will cause your output to either go to the remote box or to go to your local box.
# 6  
Old 06-27-2012
Thank you guys , you have been great.
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. Shell Programming and Scripting

Shell script works fine as a standalone script but not as part of a bigger script

Hello all, I am facing a weird issue while executing a code below - #!/bin/bash cd /wload/baot/home/baotasa0/sandboxes_finance/ext_ukba_bde/pset sh UKBA_publish.sh UKBA 28082015 3 if then echo "Param file conversion for all the areas are completed, please check in your home directory"... (2 Replies)
Discussion started by: ektubbe
2 Replies

3. UNIX for Dummies Questions & Answers

Calling a script from master script to get value from called script

I am trying to call a script(callingscript.sh) from a master script(masterscript.sh) to get string type value from calling script to master script. I have used scripts mentioned below. #masterscript.sh ./callingscript.sh echo $fileExist #callingscript.sh echo "The script is called"... (2 Replies)
Discussion started by: Raj Roy
2 Replies

4. Shell Programming and Scripting

Script will keep checking running status of another script and also restart called script at night

I am using blow script :-- #!/bin/bash FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not if then # echo "process found" exit 0; else echo "process not found" exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Discussion started by: ketanraut
1 Replies

5. Shell Programming and Scripting

create a shell script that calls another script and and an awk script

Hi guys I have a shell script that executes sql statemets and sends the output to a file.the script takes in parameters executes sql and sends the result to an output file. #!/bin/sh echo " $2 $3 $4 $5 $6 $7 isql -w400 -U$2 -S$5 -P$3 << xxx use $4 go print"**Changes to the table... (0 Replies)
Discussion started by: magikminox
0 Replies
Login or Register to Ask a Question