run a local script as root on remote server [duplicate] - bash

This question already has answers here:
How to use SSH to run a local shell script on a remote machine?
(22 answers)
Closed 5 years ago.
I try to run a local script on multiple remote servers as root. I don't have su to root on those but just can run root commands using sudo. So far I tried:
for host in $(cat hosts_list); do ssh -tt $host "echo mypassword | sudo bash -s" < ./myscript.sh
And in myscript.sh there is something like:
echo "test test123" >> /etc/tests
exit 0
But it looks like not working and won't change the file. What is the proper way to run this script as root and without typing password separately for each host?

Ok, then why do you "echo mypassword" ?
Can't you add your SSH account to the sudoers file with NOPASSWD ?
From man sudoers:
authenticate If set, users must authenticate themselves via a password (or other means
of authentication) before they may run commands. This default may be
overridden via the PASSWD and NOPASSWD tags. This flag is on by default.

Related

SSH sudo inside script different behaviour

I'm trying to set some automation inside local network and started working with some shell scripting and something that I saw - very strange behaviour SSH inside script according to how script running(with or without sudo):
What I have:
ComputerA and ComputerB.
Inside ComputerA:
A shell script script.sh:
cp /dir1/file1 /dir2/file2
ssh username#ComputerB "sudo reboot"
/etc/ssh/ssh_config file with some configurations to work without ssh-keys (they always changes on ComputerB):
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null
GlobalKnownHostsFile=/dev/null
Inside ComputerB:
In /etc/sudoers file:
username ALL=(ALL:ALL) NOPASSWD:ALL
When I connecting through SSH to ComputerA and running script.sh without sudo, I get permission error to write to /dir2 (it's OK) and next command on ComputerB executes normally (reboots ComputerB), but I'm running sudo script.sh. It copy file and then I got strange - SSH asks me username password. Tried different variants to change ssh command to something like:
ssh -t username#ComputerB "echo user_pass | sudo -S reboot"
but nothing helped.
So I need help to figure out what happens and what to do to execute sudo script.sh without entering password for ssh command inside.
Thanks!
Don't run script.sh with sudo on computerA; instead modify the script like so:
sudo cp /dir1/file1 /dir2/file2
ssh username#ComputerB "sudo reboot"
The reason that you're seeing the strange behaviour is that you're actually becoming root on computerA (I assume you have a keypair set-up for your regular user and expect to connect to computerB passwordless?), and that root on computerA doesn't have a keypair that computerB knows about.

execute commands on remote server using shell script [duplicate]

This question already has answers here:
How to use SSH to run a local shell script on a remote machine?
(22 answers)
Closed 5 years ago.
I have a deployment script located on a build server. Each time I need to generate the build I need to login to the server using ssh and then trigger the deployment script.
I managed to generate the public/private keys so that I need not enter the password to login to the build server. But still I need to login and run the deployment script.
Is there a way where to automate the login, executing the deployment script on the build server and then exit from in one local script. How to achieve this
You can use a pipe (assuming you are using a *nix OS):
echo "your --command --here" | ssh user#host
Usually it's just
ssh buildserver /path/to/build.sh
You may need to tweak the options though.
Just do ssh <HOST> <COMMAND> in a single line. If you can already
log in using keys you won't have to type a password. Example:
$ ssh localhost 'echo hi'
hi
This <COMMAND> is run synchronously. That means that ssh won't
finish until <COMMAND> run on the remote server has finished. See yourself:
$ ssh localhost 'sleep 10'
This command will wait for 10 seconds and you won't be able to type new commands until it's finished.

Bash Script : Execute unix commands inside a remote server

I am trying to login to Server B from Server A and perform simple UNIX commands on Server B using a shell script. The code is as follows. But ls -al is displaying the result of Server A and not the one that is logged on to i.e Server B. Any inputs are highly appreciated. Thanks
#!/bin/bash
clear
sshpass -p password ssh hostname
ls -al
exit
When the shell interprets a script file, it creates a child process to
execute each command line. So, the command lines after sshpass -p password ssh hostname are not actually executed inside the ssh
session to hostname, but in the host where the bash instance is
running.
To achieve what you want, you can check ssh(1) usage line and note that there is a [command] argument, that says:
If command is specified, it is executed on the remote host instead
of a login shell.
So, one way to do it is sshpass -p password ssh hostname ls -la. Another way which can provide some more flexibility is:
#!/bin/bash
clear
cat | sshpass -p password ssh hostname <<EOF
ls -la
EOF
Which would make ssh start a login shell in the remote host and pass
to its stdin the lines provided in the Here Document. The remote
shell would then interpret those strings as commands and execute them.
If you just want to run ls -al on the remote server, put it on the same line as the ssh command like
sshpass -p password ssh hostname ls -al
it will automatically exit when it gets to the end of the command so you don't need to put exit
Also, if you're going to be doing this and don't want to interactively enter the password, you might want to look at sharing public/private keys and using that so it won't ever ask for a password (unless you password protect your private key)

Sftp using a remote user without shell [duplicate]

This question already has answers here:
How to run the sftp command with a password from Bash script?
(12 answers)
Closed 8 years ago.
I need to make a shell script that will transfer files to a remote server however, the account given to me has no shell mainly because they want to restrict the access to sftp only. I already have a shell script my only problem is that I cannot automate it. The script stops on the password prompt. I read on most of the passwordless sftp tutorials that I need to generate a keypair but like i've said I cannot do this as the remote account has no shell access. What are the alternatives for passwordless sftp considering an account without shell? The remote server has no 'expect' installed.
Thank you very much.
EDIT: Please also note that I cannot install anything in the local server. I MAY be able to do something on the remote server but not on the local.
On Debian and Ubuntu (maybe other systems also) you can use sshpass
apt-get install sshpass
sshpass -p 'YourPassword' ssh user#host
OR
sshpass -p 'YourPassword' sftp user#host
But the you will have your password printed in your bash_history..
So you might want to pass the password from a file or an environment-variable.
Greets, Eric

Execute a shell script in another unix machine using SSH [duplicate]

This question already has answers here:
SSH in shell script with password
(3 answers)
Closed 9 years ago.
I am trying to execute a script which is present in another unix machine from my unix box using below command:
HOST=myhostname
USER=myuser
ssh -o StrictHostKeyChecking=no -l $USER $HOST "/tmp/myscript.sh"
But the command prompts me to enter the password, but I don't want the command to prompt for password, instead of that I want to pass the password as parameter for my command. But I am not able to find an option to pass the password as parameter to SSH command.
Please help me on how to send password as part of command, instead of the command to prompt it. I am using BASH shell script.
Use EXPECT
Save this as mylogin.exp (or some other name you like), and change the names and password:
#!/usr/bin/expect -f
spawn ssh myname#some_server.com
expect {
password: {send "mypassword\n"}
}
interact
Then just run the command:
./mylogin.exp
That will just get you logged in. If you want to run a command instead, you can just put that at the end of the ssh command.

Resources