Run a script in parts on 2 servers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Run a script in parts on 2 servers
# 1  
Old 07-30-2012
Run a script in parts on 2 servers

Hi all,

I have a task for which I need to run some commands on one server1 and then jump from the server1 (using ssh and asking user to enter login credentials manually for server2 server) to server2 and run some commands there and exit.

I know the script I need here but problem is when I jump to server2 I am not able to execte anything over there from script that is on the server1.
I could write another script on the server2 (only for the part that needs to be done on server2) but I am not sure how to execute it when I login to it from server1.

Please provide some inputs to me if this is possible or not and if yes then can I do it.

Thanks in advance.
# 2  
Old 07-30-2012
Well you could use sftp to transfer your script over to server2 then run it but this will require 2 password prompts (one for the sftp transfer and one for ssh to execute the script). You could consider setting up passwordless ssh/sftp to remove the password prompt part.

Another option is to use cat to write a local script to server2 and the execute it eg:

Code:
server1$ cat local_script
ls -l | wc -c
 
server1$ ssh user@server2 "cat > temp_script.sh <<temp_script_EOF
$(cat local_script)
temp_script_EOF
chmod +rx temp_script.sh
./temp_script.sh
rm temp_script.sh"
user@server2's password:
  10364

# 3  
Old 08-01-2012
Thank you Chubler_XL.

I think second option is more suitable for me where I am running a script on server2 by logging onto it from server1.
Can you please do me another favour please... can you jut explain me the code which you have suggested? I am not very good at UNIX so want to know the code before I try to use it. And it will also help me to modify it as per my need if required.

Thanks again.
# 4  
Old 08-01-2012
OK,

the $(cat local_script) inline command is evaluated on server1 before ssh is executed. This is done by the shell as part of parseing the command line.

This basically dumps the contents of local_script into the command line at the tagged point so the command line is:

Code:
ssh user@server2 "cat > temp_script.sh <<temp_script_EOF
ls -l | wc -c
temp_script_EOF
chmod +rx temp_script.sh
./temp_script.sh
rm temp_script.sh"

ssh will prompt for a password for user and then execute the following on server2 (as user "user"):
Code:
cat > temp_script.sh <<temp_script_EOF
ls -l | wc -c
temp_script_EOF
chmod +rx temp_script.sh
./temp_script.sh
rm temp_script.sh

A "here document" is being passed into cat and redirected out to a (temprary file) "temp_script.sh". The end result here is that local_script from server1 is copied over to server2 as temp_script.sh. chmod makes the script readable and executable then the script is run (with ./temp_script.sh) and the finally deleted.
# 5  
Old 09-25-2012
Thanks a lot Chubler_XL. This worked perfect for me but my doubts seems to have no end Smilie

Now after I can login to server2 and execute code. But After I login to server2 I have to sudo as another user (because only this user has permission to execute commnds I want to run).

I tried to add the sudo command in code after line
chmod +rx temp_script.sh

But it prompts for password and do sudo as the user and stops there. It is not executing next lines after it does sudo.

How can I continue executing the commands after I sudo as different user than what I have logged in as in ssh in first line of code?

ssh user1@server2 "cat > temp_script.sh <<temp_script_EOF
# 6  
Old 09-27-2012
I was able to generate ssh keys and thus avoid need to enter password while doing ssh from server1 to server2. :-)
But, I am trying to do sudo from local_script which I think is the proper way to do it but facing same problem, it doesn't show prompt after sudo command to enter password. May be because I am sending this code from one server to other.
Error that I am getting is
sudo: no tty present and no askpass program specified

Any suggestions please?

---------- Post updated at 09:43 PM ---------- Previous update was at 09:26 PM ----------

One thing to mention here is I don't want to enter password from script. It is ok if it prompts for password and wait till user enters password manually.
My problem here is that scripts doesn't stop/wait on password prompt for user to enter the password. :-(
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run a shell script on all 15 servers at the same time?

We have 15 servers. Hostnames for these 15 servers are stored in a text files and loop through each server to connect to the remote server and run a command, but this loop process runs the command one after another. However, the requirement is to run the same command on all 15 servers at the same... (10 Replies)
Discussion started by: laknar
10 Replies

2. 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

3. Shell Programming and Scripting

Incrementing parts of ten digits number by parts

I have number in file which contains date and serial number: 2013101000. The last two digits are serial number (00). So maximum of serial number is 100. After reaching 100 it becomes 00 with incrementing 10 which is day with max 31. after reaching 31 it becomes 00 and increments 10... (31 Replies)
Discussion started by: Natalie
31 Replies

4. AIX

How to write a script to run without password on a batch of servers?

I need run a command such as ps -ef |grep xxx on a batch of servers, how to write a script to run it without password? don't need go in each server to check? Thanks (7 Replies)
Discussion started by: rainbow_bean
7 Replies

5. Shell Programming and Scripting

run vi/vim encrypted shell script without decryption on multiple servers

Hello Everyone, How do we run vi/vim encrypted shell script without decryption on multiple servers. It is a simple bash script and vim -nx <filename> has been used to encrypt with desired password. Now I have few errors, the syntax is absolutely fine as I have run that script multiple times on... (0 Replies)
Discussion started by: lovesaikrishna
0 Replies

6. 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

7. Shell Programming and Scripting

How to run same script on multiples servers

Hi All, I have some script that run some commands and send results to my email. I want to run same script on mulitiple servers. How can I do that. I know there is an option "ssh". But I'm not quite sure how to use it in the script. And also. scripts has some parameters like following, if :... (10 Replies)
Discussion started by: s_linux
10 Replies

8. AIX

is it ok to run AIX mksysb script on vio servers?

Hi to all now we running mksysb to nim on aix servers. we are planning to take mksysb from vio servers. can we use the same script for vio servers. thanks (10 Replies)
Discussion started by: honeym210
10 Replies

9. Shell Programming and Scripting

ksh to run servers

I want to write a Kshell program which will start the servers(Oracle,DataIntegrator). Can anybody help me with this? I would appreciate your help. Thanks in advance (0 Replies)
Discussion started by: pari111222
0 Replies
Login or Register to Ask a Question