bash script to execute a command remote servers using ssh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash script to execute a command remote servers using ssh
# 1  
Old 04-03-2012
bash script to execute a command remote servers using ssh

Hello,

I am running into few issues, please suggest me what I am missing.

I am running this script on a linux host.

Main idea of this script is to, login to each host via ssh and get uid of user, service user that I trying to run this script, has already deployed ssh keys and provide sudo access to root with password less on all the server.

issue: /tmp/hostname.txt file contains list of IP addresses line by line, when I execute this script, I get the o/p of the first host successfully and stops executing right from that point.

Code:
# more userid
#!/bin/bash
while read line
do
        ssh $line '(echo "`hostname` UID for User v7866 is `getent passwd v7866 | cut -f3 -d:`")' >> /tmp/output
done < /tmp/hostname.txt
#
#more /tmp/hostname.txt
10.10.7.182
10.10.7.173
10.10.7.143
#

Thanks,
# 2  
Old 04-03-2012
ssh tries to read from the same standard input as your while loop, 'eating' your entire datafile.

Also, you don't need to re-open the same logfile 9,000 times to write 9,000 lines.

Also, the braces aren't necessary.

Code:
#!/bin/bash
while read line
do
        ssh $line 'echo "`hostname` UID for User v7866 is `getent passwd v7866 | cut -f3 -d:`"' </dev/null
done < /tmp/hostname.txt > /tmp/output


Last edited by Corona688; 04-04-2012 at 11:13 AM..
# 3  
Old 04-03-2012
A better way to accomplish this is to invoke the script which contains the commands that you want to be executed at the remote server
Code:
for ip_addr in $(cat hostname.txt); do
     ssh ${ip_addr} "bash -s" < script_to_be_invoked
done

As you'll see that, this way
Code:
ssh ip_addr "commands"

won't work properly while the "commands" is getting longer!
Look at this https://www.unix.com/shell-programmin...te-server.html
The reason why your commands will only be run on the first host connected to is detailed explained here
http://72.14.189.113/howto/shell/while-ssh/

Last edited by complex.invoke; 04-03-2012 at 11:31 PM..
This User Gave Thanks to complex.invoke For This Post:
# 4  
Old 04-05-2012
Thanks for the help,

I was able to run the script as suggested works fine !!!!
Code:
for ip_addr in $(cat hostname.txt); do
     ssh ${ip_addr} "bash -s" < script_to_be_invoked
done

The only annoying part is, once the user ssh into the each server, I see banner is being displayed on the terminal, how do we remove that from the output.

Thanks,
# 5  
Old 04-06-2012
Quote:
Originally Posted by bobby320
The only annoying part is, once the user ssh into the each server, I see banner is being displayed on the terminal, how do we remove that from the output.

Thanks,
Just map the ip addresses of your remote servers' with a unique name one by one and line by line in your /etc/hosts on the master server which can manage all your remote servers
Code:
192.168.0.17 node0
192.168.0.18 node1
192.168.0.19 node2

A more convenient way to perform your tasks with ssh at the remote servers is to invoke another script ssh.sh
Code:
ssh ${0##*/} "${@}"

just locate the script ssh.sh in /usr/local/sbin, then make some soft links:
Code:
# cd /usr/local/sbin
# ln -s ssh.sh node0
# ln -s ssh.sh node1
# ln -s ssh.sh node2

after you have done this, you can login to remote server,say, login to node1, you can just execute
Code:
# node1

instead of
Code:
# ssh node1

or
Code:
# ssh 192.168.0.18

and the same way to execute your commands
Code:
# node1 "commands"

instead of
Code:
# ssh node1 "commands"

and so
Code:
for node in node1 node2 node3; do
     ${node} "bash -s" < script_to_be_invoked
done

The script below is detailed explained what you can do with no-ssh needed remote commands execution
Code:
#!/bin/bash
#
# ssh.sh
# node1 "echo '${local_variable}'"
# node1 "cat local_big_archive.tar.bz2" | tar -jxpvf -
# tar -jcpvf - /path/to/data | node1 "cat > /dev/tape"
# node1 "cd /path/to/dir; tar -jxpvf -" < local_big_archive.tar.bz2
# node1 "dd if=/dev/sda bs=512 count=1" | node2 "cd /usr; dd of=mbr.bin"
# node1 "dd <  /dev/sda bs=512 count=1" | node2 "cd /usr; dd >  mbr.bin"
# node1 "mysqldump" | mysql
# node1 "mysqldump" > backup.sql
# node1 
# node1 "uptime"
# node1 "uptime" > local_file  
# node1 "uptime  > remote_file"
# node1 "bash -s" < script_to_run
# node1 "bash -s" < script_to_run > local_file
# node1 "bash -s  > remote_file" < script_to_run
# tar -jcpvf - /path/to/data | node1 "cd /target/dir; tar -jxpvf -"
# cd /target/dir; node1 "tar -jcpvf - /path/to/data" |  tar -jxpvf -
# rsync -Pvzrtopg /path/to/data/ node1:/root/
# rsync -Pvzrtopg node1:/path/to/data /root/

ssh ${0##*/} "${@}"


Last edited by complex.invoke; 04-06-2012 at 12:32 PM..
# 6  
Old 04-10-2012
When I tried to include sudo command in the place script to be executed I am getting this error messages, any idea how can I overcome !!!!
Code:
#!/bin/bash
for ip_addr in $(cat ipaddress.txt); do
ssh -t ${ip_addr} "bash -s" < /uploadscript.txt
done
#
#more /uploadscript.txt
sudo echo xy12ls1 | passwd --stdin martin

when I run this script I am getting this error message

$ ./remotescript
Pseudo-terminal will not be allocated because stdin is not a terminal.

Thanks,
# 7  
Old 04-10-2012
Try
Code:
-t -t

to force it to allocate a tty even though there's no local tty.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to execute setDomainEnv.sh in wblogic via ssh on remote server in shell script?

How to execute setDomainEnv.sh in wblogic via ssh on remote server in shell script we execute setDomainEnv.sh manually as . ./setDomainEnv.sh from its location ie /opt/SP/users/domian/bin" My approach is not working. Please advise. #!/bin/bash set -x base="/opt/SP/users/d1/bin"... (5 Replies)
Discussion started by: abhaydas
5 Replies

2. Shell Programming and Scripting

Execute ssh command with additional terminal command to any remote user not working script

Hello i am having an issue with bash script and this is the code now=$(cat hosts1.txt | awk '{print $2;}') while read n ;do ssh root@$now 'useradd test1; echo -e "test1\ntest1" | passwd test1 && echo "test1 ALL=(ALL:ALL) ALL" >> /etc/sudoers' When i execute only part with cat, it... (8 Replies)
Discussion started by: tomislav91
8 Replies

3. Shell Programming and Scripting

Ssh bash script exits without remote command completion

Hi, My goal is to connect from unix server A to windows server B and call a bat file on windows. I am able to succeed in remoting to windows and executing a command, the issue i am facing is the shell scrip is exiting without making sure of bat file success. Can you please help me in... (4 Replies)
Discussion started by: pxp018
4 Replies

4. Shell Programming and Scripting

Execute command on remote host via ssh

How should i make the following code working #!/bin/bash INPUTFILE="test.txt" while read STRING; do IP=`host -t A $STRING | awk '{print $NF}'` HOSTNAME=`ssh -oPasswordAuthentication=no -oStrictHostKeyChecking=no $IP "hostname"` echo $HOSTNAME > out.txt done < $INPUTFILE At this moment while... (3 Replies)
Discussion started by: urello
3 Replies

5. Shell Programming and Scripting

Shell script help to execute ssh remote commands

Hi, The below command is not giving me the count , Can somebody help me in re-writing this pls . Proc_Exist=`ssh -q -o "BatchMode=yes" -o "PasswordAuthentication=no" $OAUSER@${Primary_Node} ps -ef | grep -v grep | grep "${ICM_Proc}" |wc -l ` Also the same problem with below... (13 Replies)
Discussion started by: Y.balakrishna
13 Replies

6. Shell Programming and Scripting

Bash script connect to remote servers and become root

Hi, I need a script that will connect to a list of servers and first sudo to root and then run a couple of commands. For security reasons, we can't setup ssh keys as root. Manually I have to login to a server as user and then sudo to root. It's not possible to use root@servername , because of... (8 Replies)
Discussion started by: misterx12345
8 Replies

7. Shell Programming and Scripting

Using ssh to execute a remote script in the background

Help please!! I want to use ssh to execute a remote exe and while it's running I want to query for the process ID of the exe (2 different ssh commands) 1. sshpass -p "<passwd>" ssh -f -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@<ipaddress> nohup /tmp/mmds_asyn & 2.... (0 Replies)
Discussion started by: rvompoluTMW
0 Replies

8. Shell Programming and Scripting

SSH execute remote command (with jump)

Hi all, I am facing the following issue: Host A should execute a remote command (say pwd) on host B2. B2 is not directly reacheable but you have to connect from a to B1, then from B1 you can execute the command ssh user@B2 pwd. B1 and B2 are directly connected: A => B1 => B2 | ... (3 Replies)
Discussion started by: Evan
3 Replies

9. Shell Programming and Scripting

ssh to remote host and execute command

Hi, could anyone please tell me how to ssh to remote host foo and execute command on it and print the result on local host? Thanks, Paresh (1 Reply)
Discussion started by: masaniparesh
1 Replies

10. Shell Programming and Scripting

How to execute remote ssh command - Perl and CGI

Hi, I am having nightmare issue-ing remote ssh command from a CGI perl script. It just won't run on debug message: It says permission denied. Can I even do this? as the apache server running under DAEMON account probably can't execute it? Is this the case of what's going on? Here is my... (3 Replies)
Discussion started by: Dabheeruz
3 Replies
Login or Register to Ask a Question