Run shell commands in a remote machine - shell

I would like to know how can I run shell commands in a remote machine.
I tried this:
ssh prdcrm1#${server} "grep -l 'Sometthing' *"
It is working, but I want to run more commands.
Do someone has an Idea?

You can run multiple commands on remote machine like,
Run date and hostname commands:
$ ssh user#host "date && hostname"
Run a script called /scripts/backup.sh
ssh user#host '/scripts/backup.sh'
Run sudo or su command using the following syntax
ssh user#host su --session-command="/sbin/service httpd restart"
ssh -t user#host 'sudo command1 arg1 arg2' ## su syntax ##
Multi-line command with variables expansion
VAR1="Variable 1"
ssh $HOST bash -c "'
ls
pwd
if true; then
echo $VAR1
else
echo "False"
fi
'"
Hope these helps you.

Related

Remote SSH run local script containing sudo command, -T is not working

I try to run a local script using ssh
ssh remotehost 'bash -s' < test.sh
test.sh is
#!/bin/bash
echo "Start Script"
sudo echo "test"
echo "End Script"
Also, in remote host, sudo logging is enabled in /etc/sudoers
Defaults logfile="/var/log/sudo.log"
Defaults log_input,log_output
After this, the output of ssh remotehost 'bash -s' < test.sh is
Start Script
test
Script got terminated after executing the first sudo command. But I need to get
Start Script
test
End Script
I also tried ssh -T remotehost 'bash -s' < test.sh, unfortunately it's not working.
What's the prope way to do this? Thanks

Running "sudo su" within a gitlab pipeline

I've installed some software on a server that my gitlab runner SSH's to, and one of the commands needs to be run after doing sudo su. If I run it as a regular user, but with sudo in front of it - it doesn't work. I have to first completely switch to the sudo user first.
This works fine when I SSH into the server and do the commands manually. But when I try it from the pipeline (rough code below):
my_script:
stage: stage
script:
- ssh -o -i id_rsa -tt user#1.1.1.1 << EOF
- sudo su
- run_special_command <blah blah>
- exit
# above exits from the SSH. below should stop the pipeline
- exit 0
- EOF
I get very weird output like the below:
$ sudo su
[user#1.1.1.1 user]$ sudo su
echo $'\x1b[32;1m$ run_special_command <blah blah>\x1b[0;m'
run_special_command <blah blah>
echo $'\x1b[32;1m$ exit\x1b[0;m'
exit
echo $'\x1b[32;1m$ exit 0\x1b[0;m'
exit 0
echo $'\x1b[32;1m$ EOF\x1b[0;m'
And what I'm seeing is that it doesn't even run the command at all - and I can't figure out why.
In this case, you need to put your script as a multi-line string in your YAML. Alternatively, commit a shell script to repo and execute that.
and one of the commands needs to be run after doing sudo su. If I run it as a regular user, but with sudo in front of it - it doesn't work.
As a side note, you can probably use sudo -E instead of sudo su before the command. But what you have should also work with the multi-line script.
MyJob:
script: |
ssh -o -i id_rsa -tt user#host << EOF
sudo -E my_command
EOF
exit 0
Alternatively, write your script into a shell script committed to the repository (with executable permissions set) and run it from your job:
MyJob:
script: “my_script.sh”

How to copy echo 'x' to file during an ssh connection

I have a script which starts an ssh-connection.
so the variable $ssh start the ssh connection.
so $SSH hostname gives the hostname of the host where I ssh to.
Now I try to echo something and copy the output of the echo to a file.
SSH="ssh -tt -i key.pem user#ec2-instance"
When I perform a manual ssh to the host and perform:
sudo sh -c "echo 'DEVS=/dev/xvdbb' >> /etc/sysconfig/docker-storage-setup"
it works.
But when I perform
${SSH} sudo sh -c "echo 'DEVS=/dev/xvdb' > /etc/sysconfig/docker-storage-setup"
it does not seem to work.
EDIT:
Also using tee is working fine after performing an ssh manually but does not seem to work after the ssh in the script.sh
The echo command after an ssh of the script is happening on my real host (from where I'm running the script, not the host where I'm performing an ssh to). So the file on my real host is being changed and not the file on my host where I've performed an ssh to.
The command passed to ssh will be executed by the remote shell, so you need to add one level of quoting:
${SSH} "sudo sh -c \"echo 'DEVS=/dev/xvdb' > /etc/sysconfig/docker-storage-setup\""
The only thing you really need on the server is the writing though, so if you don't have password prompts and such you can get rid of some of this nesting:
echo 'DEVS=/dev/xvdb' | $SSH 'sudo tee /etc/sysconfig/docker-storage-setup'

Heredoc for nested command in bash

I need to ssh into a machine and execute a bunch of commands under sudo bash. Here is what I've tried:
sshpass -p "vagrant" ssh vagrant#33.33.33.100 "sudo bash -i -c <<EOF
echo
ls
echo
EOF"
But it throws me 'bash: -c: option requires an argument\n'. How can I fix this?
You need to remove -c from your command line to make it accept heredoc:
sshpass -p "vagrant" ssh vagrant#33.33.33.100 "sudo bash <<EOF
echo
ls
echo
EOF"
Also you may remove -i (interactive) option too.
bash -c expects you to provide all commands on command line so this may work too:
sshpass -p "vagrant" ssh vagrant#33.33.33.100 "sudo bash -c 'echo; ls; echo'"

Mysqldump through SSH on remote windows server using bash script

I'm trying to write a bash script to migrate database from remote server to local. One of our servers is unfortunately windows server. I installed freesshd so I can use ssh.
When I run this from my ubuntu shell:
sshpass -p 'my_password' ssh user#host
'C:/wamp/bin/mysql/mysql5.1.36/bin/mysqldump
-u root -pmypassword mybase --result-file=C:/wamp/outfiles/mybase.sql'
It runs fine and it dumps the base.
Unfortunately when I do the same thing from the bash script - I got the permission denied feedback. Why? Is there any difference between command from script and regular shell command?
This is my bash script now:
#!/bin/bash
remoteHost=$1
remoteUser=$2
echo -n "Provide remote db password: "
read -s remoteDbPass
echo ""
echo -n "Provide remote server password: "
read -s remotePass
echo ""
dbName=$3
localDbName=$4
dumpPath=/var/lib/mysql/dumps/
winMysqlPath=C:/wamp/bin/mysql/mysql5.1.36/bin/
winDumpPath=C:/wamp/outfiles/
sshpass -p '$remotePass' ssh $remoteUser#$remoteHost '${winMysqlPath}mysqldump -u root -p$remoteDbPass $dbName --result-file=$winDumpPath$dbName.sql'
pscp -pw $remotePass $remoteUser#$remoteHost:$winDumpPath$dbName.sql $dumpPath$dbName.sql
mysql $localDbName < $dumpPath$dbName.sql
variable quoted with single quote ' will not be expanded.
use double quote " instead.

Resources