How to run simple single command on multiple Linux servers?


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to run simple single command on multiple Linux servers?
# 1  
How to run simple single command on multiple Linux servers?

Hi All,

How can i run a single command on multiple servers with or without giving credentials.

I have a file(servers.txt) which has got list of servers and i want to run a command
Code:
lsb_release -dr

on all these servers and get output of those servers against each server.

I tried below code but it didn't work

Code:
tmpdir=${TMPDIR:-/tmp}/pssh.$$
mkdir -p $tmpdir
count=0
while IFS= read -r userhost; do
    ssh -n -o BatchMode=yes ${userhost} 'lsb_release -dr' > ${tmpdir}/${userhost} 2>&1 &
    count=`expr $count + 1`
done < servers.txt
while [ $count -gt 0 ]; do
    wait $pids
    count=`expr $count - 1`
done
echo "Output is in $tmpdir"



output of $ lsb_release -dr
Code:
Description:    Red Hat Enterprise Linux Server release 6.4 (Santiago)
Release:        6.4

is there any single line command which can give me the output as below
Code:
gbahelkm72.trp.expre.rmr   Release:        6.4
rtsahelkm98.trp.expre.rmr   Release:        6.4

# 2  
Hi,

Here is some code that you should be able to easily adapt, this was one of the subroutines used to collect data from over 600 servers prior to a DCM - just adapt and substitute your own bits.

Code:
for HOST in `cat ${CONFDIR}/svr-access.conf | grep -v '^#'`
do
        OUTFILE=details-${HOST}.csv
        echo "checking ${HOST}"
        MSG "checking ${HOST}"
        HOSTNAME=`ssh ${HOST} "uname -n"`
        OS=`ssh ${HOST} "uname"`
        # ssh_get_os_details ${HOST} ${OS}
	MSG "Found ${HOSTNAME} running ${OS}"
MSG "JOB STEP 2"
        if [ ${OS} = "AIX" ]
        then
MSG "JOB STEP 3"
                if [ `echo ${OSVER} | cut -c1` -ne 4 ]
                then
                        ssh_get_aix56_getdetails ${HOST} > ${DATADIR}/${OUTFILE}
                fi
	elif [ ${OS} = "SunOS" ]
	then
MSG "JOB STEP 4"
	${FUNCDIR}/ssh_solaris_getdetails ${HOST} > ${DATADIR}/${OUTFILE}
        fi
done

Regards

Gull04

---------- Post updated at 12:51 PM ---------- Previous update was at 12:35 PM ----------

Hi,

Just for some more information, here is one of the more simple routines that are called in the ssh calling script.

Code:
############################################################################################
#
# Get the Memory info.....
#
############################################################################################

get_mem()
{
GETMEM=`ssh ${SSHHOST} "/usr/sbin/prtconf 2>&1 | grep -v devinfo | grep 'Memory size' | /usr/xpg4/bin/awk '{ print $1 }'"`
MEM=`echo ${GETMEM} | awk '{ print $3 }'`
GBMEM=`echo ${MEM} /1024 | bc`
MSG "${SSHHOST} Memory Check Executed."
}

Regards

Gull04
# 3  
Note the awk path in the above example is only valid for Solaris. Not Linux.

/usr/bin/awk should be okay for Linux. Note: this likely is a symlink to /usr/bin/gawk which is the GNU version of awk.

Code:
ls -l /usr/bin/awk

will show you what is going on
# 4  
What's wrong? Do you get an error message?
I have omitted some stuff where I don't see a sense, and have prepended a hostname.
Code:
tmpdir=${TMPDIR:-/tmp}/pssh.$$
mkdir -p $tmpdir
echo "Output is in $tmpdir"
while read userhost
do
    (
    printf "%-29s " "$userhost"
    ssh -nx -o BatchMode=yes "$userhost" 'lsb_release -sd'
    ) </dev/null >"$tmpdir/$userhost" 2>&1 &
done < servers.txt
# wait until all bg jobs are done
wait

# 5  
Quote:
Originally Posted by MadeInGermany
What's wrong? Do you get an error message?
I have omitted some stuff where I don't see a sense, and have prepended a hostname.
Code:
tmpdir=${TMPDIR:-/tmp}/pssh.$$
mkdir -p $tmpdir
echo "Output is in $tmpdir"
while read userhost
do
    (
    printf "%-29s " "$userhost"
    ssh -nx -o BatchMode=yes "$userhost" 'lsb_release -sd'
    ) </dev/null >"$tmpdir/$userhost" 2>&1 &
done < servers.txt
# wait until all bg jobs are done
wait


It is giving me the same host files which are there in servers.txt
# 6  
Yes, I have assumed they are hostnames from servers.txt.
You did not say where gbahelkm72.trp.expre.rmr and rtsahelkm98.trp.expre.rmr come from.?
# 7  
Quote:
Originally Posted by MadeInGermany
Yes, I have assumed they are hostnames from servers.txt.
You did not say where gbahelkm72.trp.expre.rmr and rtsahelkm98.trp.expre.rmr come from.?

These are from servers.txt file.. this file has got hostnames
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #924
Difficulty: Medium
The present form of UTC, with leap seconds, is only defined starting from 1 January 1972.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run a script on multiple servers

I need to run a script on a bunch of remote servers. how can this be done without ssh into each individual server and run it its under /sbin/script.sh on each server (1 Reply)
Discussion started by: tdubb123
1 Replies

2. Shell Programming and Scripting

Logging in to multiple Linux servers and running the command.

Hi, I am trying to write a script to run a command on multiple linux based servers and get the o/p. I am using ssh to login. It is a celerra box and EMC NAS product. I am able login but i am not able to run nas command nas_pool -size -all the NAS server. I am getting the following error. ... (2 Replies)
Discussion started by: jpkumar10
2 Replies

3. Shell Programming and Scripting

Require single command to start script in multiple servers

I have 9 servers, on each server a script with common name is available. I send a token file to all server from 1 particular server. so when a daemon job checks that token file is available then it triggers the script.. I want to know is there any command or script which I will run/execute on... (16 Replies)
Discussion started by: mirwasim
16 Replies

4. UNIX for Dummies Questions & Answers

How to run multiple command in a single line?

Normally i would do this- cd abc ls -ltr I wish to run above command in a single line, like this- cd abc | ls -ltr But above command doesn't works, it simply runs the second command, ignoring the 1st one. :confused: (4 Replies)
Discussion started by: boy18nj
4 Replies

5. Shell Programming and Scripting

need a script that does a simple task on multiple unix servers.

hi guys, i need a script that does a simple task on multiple aix servers. if possible with both telnet and ssh. the simple task i wanna do is connect to a server and run "ifconfig -a" and get the output. nextweek i need to do similar jobs on like 50 servers... :( can anybody help me with making... (2 Replies)
Discussion started by: curtis911
2 Replies

6. Programming

Would you please give me some idea about single client and multiple servers

Hi, I have a program which needs to connect multiple servers at the same time. The program has to collect data from each of servers and then make a decision regarding to the data received. There are several requirements. 1. Server (s) may shutdown anytime without any ack (e.g.power... (1 Reply)
Discussion started by: sehang
1 Replies

7. Shell Programming and Scripting

Need a script to run on multiple mail servers..

Hello, I am a Unix newbie and I need a script in which I can run a command on multiple servers at work. The command is to start a storage process and I am sick of doing it manually on all servers.. Here's the command: /opt/bss/bin/snmptable -CB -v2c -c P67LzuBm hostname hrStorageTable... (4 Replies)
Discussion started by: kinyyy
4 Replies

8. HP-UX

Automatic execution of commands in multiple servers using single script.

Hi, I've to do a simple job many times whenever it has been asked, just i've to log in to all of fourtien HP servers and i've to execute ps -fu user > temp cat temp|sendmail "xyz@z.com" commands to send the statics of all of 14 servers over the mail to particular user id.. Though logging... (4 Replies)
Discussion started by: vickramshetty
4 Replies

9. SuSE

Regarding accessing multiple servers using single public ip address

Hello, Currently we are having different linux servers (for example: let's assume audio server, video server and text server) to handle requests from outside users. Suppose the outside users in different LAN (Local Area Network), other than the servers. For example user is in 20 series LAN and... (5 Replies)
Discussion started by: navneet_2009
5 Replies

10. UNIX for Dummies Questions & Answers

How to run multiple command in single command?

Dear Unix Guru, I have several directories as below /home/user/ dir1 dir2 dir3 Each directory has different size. I want to print each directory size (Solaris command du -hs .) Can you please guide me how to achieve this? Thanks Bala (2 Replies)
Discussion started by: baluchen
2 Replies

Featured Tech Videos