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.

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:

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

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.

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:

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"):

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.

Thanks a lot Chubler_XL. This worked perfect for me but my doubts seems to have no end :frowning:

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

I was able to generate ssh keys and thus avoid need to enter password while doing ssh from server1 to server2. :slight_smile:
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. :frowning: