Running part of bash script on a remote machine - bash

I need to run some commands locally and then some command on a remote machine all using a single local bash script.
For simplicity just say I want to do the following and execute it on my local desktop machine.
#!/bin/bash
#upload some files to a remote machine
cd /tmp
./upload-files.sh
#run commands on remote machine
ssh myuser:mypass#somewhere.com
cd /tmp/uploads <--- these commands don't run in the ssh connection
./process-uploads.sh
exit
#run command locally again.
cd -
echo 'complete!'
Any ideas of how to do this?

You can use here-doc with ssh command:
#!/bin/bash
#upload some files to a remote machine
cd /tmp
./upload-files.sh
#run commands on remote machine
ssh -t -t myuser:mypass#somewhere.com<<EOF
cd /tmp/uploads
./process-uploads.sh
exit
EOF
#run command locally again.
cd -
echo 'complete!'

If you want to run only one command:
ssh myuser:mypass#somewhere.com 'cd /tmp/uploads; ./process-uploads.sh'

Related

Script to copy data from cluster local to pod is neither working nor giving any error

The bash script I'm trying to run on the K8S cluster node from a proxy server is as below:
#!/usr/bin/bash
cd /home/ec2-user/PVs/clear-nginx-deployment
for file in $(ls)
do
kubectl -n migration cp $file clear-nginx-deployment-d6f5bc55c-sc92s:/var/www/html
done
This script is not copying data which is therein path /home/ec2-user/PVs/clear-nginx-deployment of the master node.
But it works fine when I try the same script manually on the destination cluster.
I am using python's paramiko.SSHClient() for executing the script remotely:
def ssh_connect(ip, user, password, command, port):
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(ip, username=user, password=password, port=port)
stdin, stdout, stderr = client.exec_command(command)
lines = stdout.readlines()
for line in lines:
print(line)
except Exception as error:
filename = os.path.basename(__file__)
error_handler.print_exception_message(error, filename)
return
To make sure the above function is working fine, I tried another script:
#!/usr/bin/bash
cd /home/ec2-user/PVs/clear-nginx-deployment
mkdir kk
This one runs fine with the same python function, and creates the directory 'kk' in desired path.
If you could please suggest the reason behind, or suggest an alternative to carry out this.
Thank you in advance.
The issue is now solved.
Actually, the issue was related to permissions which I got to know later. So what I did to resolve is, first scp the script to remote machine with:
scp script.sh user#ip:/path/on/remote
And then run the following command from the local machine to run the script remotely:
sshpass -p "passowrd" ssh user#ip "cd /path/on/remote ; sudo su -c './script.sh'"
And as I mentioned in question, I am using python for this.
I used the system function in os module of python to run the above commands on my local to both:
scp the script to remote:
import os
command = "scp script.sh user#ip:/path/on/remote"
os.system(command)
scp the script to remote:
import os
command = "sshpass -p \"passowrd\" ssh user#ip \"cd /path/on/remote ; sudo su -c './script.sh'\""
os.system(command)

sshpass: No such file or directory

Bellow command if i write inside a script (test.sh) and execute directly on the specific machine it works.
sshpass -p $HOST_PWD sftp testuser#host <<!
cd parent
mkdir test
bye
!
But when i try to run (directly below scrip or invoking the test.sh file in the specif path) in Jenkins with "Execute shall script on remote host using ssh" it failing with
sshpass: Failed to run command: No such file or directory
I have installed sshpass, lftp and rsync in the remote machine
Issue :
I have added export $HOST_PWD in .bashrc of specific machine as well as Jenkins but in not finding it
Script placed in specific machine, if directly executed the script in that machine it works even with $HOST_PWD. But not working if we invoke from jenkins either script or directly scrip using "Execute shall script on remote host using ssh"
Working with Changes :
Instead of $HOST_PWD if i added directly password it works.

Execute a sudo command through SSH on a remote server using Git Bash?

I am trying to use a .sh script on Windows 10 (through Git Bash) to restart my nginx server.
This is what I'm trying
$ ssh myname#myserver 'sudo /usr/sbin/nginx -s reload'
sudo: Command not found.
I'm not sure why this happens, I know sudo isn't defined in Git Bash but shouldn't the command execute on the server? When I ssh in manually and run the same command it works:
$ ssh myname#myserver
$ myserver:/home/myname[ 51 ] --> sudo /usr/sbin/nginx -s reload
Password:

run ssh script into ubuntu instance do something, when exit, stay in ubuntu

I am running a very simple script that will ssh into a remote ubuntu instance, move around the directory structure execute a few things, then I want the prompt to stay in Ubuntu. When the script ends, in ends back at the local prompt. How do I make modify the script so that it finishes with the remote prompt?
local$ ssh -i xxx.pem ubuntu#xxx.ap-region.compute.amazonaws.com \
"cd virtualenv; ls -lh;"
There are two things needed to be added to your commandline:
The bash command in the end starts the bash shell (you can start any other you want)
The -t switch will make sure the remote server will allocate you TTY and your shell will work as expected:
local$ ssh -t -i xxx.pem ubuntu#xxx.ap-region.compute.amazonaws.com \
"cd virtualenv; ls -lh; bash"

Jenkins not able to execute ssh script

I have below ssh script which I am trying to execute by Jenkins, it runs fine when I invoke it from shell.
#ssh to remote machine
sshpass ssh 10.40.94.36 -l root -o StrictHostKeyChecking=no
#Remove old slave.jar
rm -f slave.jar
#download slave.jar to that machine
wget http://10.40.95.14:8080/jnlpJars/slave.jar
pwd
#make new dir to that machine
mkdir //var//Jenkins
# make slave online
java -jar slave.jar -jnlpUrl http://10.40.95.14:8080/computer/nodeV/slave-agent.jnlp
When I execute this script through shell it downloads the jar file to remote machine and also makes a new directory. But When I invoke it by shell plugin of jenkins, every command runs seprately. so the jar gets downloaded at master and also directory get created at master.
Also I am using sshpass for passwordless automated login, which fails sometime. Is there any other way of doing this.

Resources