Perform SSH remote cmd exec on multiple local servers from input (sshpass?) [duplicate] - linux

This question already has answers here:
Loop through a file with colon-separated strings
(2 answers)
Using a variable's value as password for scp, ssh etc. instead of prompting for user input every time
(10 answers)
How to automate password entry?
(5 answers)
Closed 4 years ago.
I am currently looking for a solution for executing remote commands on multiple local servers from an input file containing the 'user : password' in the following format:
jboss5:manager:192.168.1.101
database1:db01:192.168.20.6
server8:localnet:192.168.31.83
x:z:192.168.1.151
test:mynet:192.168.35.44
.... and others
Some commands I wish to execute remotely:
cd $HOME; ./start_script.sh; wget 192.168.1.110/monitor.sh; chmod +x monitor.sh; ./monitor.sh
I know there is a utility called "sshpass" but not sure how I could apply this utility for my needs.
I am open to any ideas in order to fulfill my need, any help would be very appreciated!
Thanks

Did you think about using ssh-keys (check man ssh-keygen)? You will be able to connect without password input ...
However if you cant, just try:
for i in $(< your_file); do
user=$(echo $i | cut -d: -f1)
pass=$(echo $i | cut -d: -f2)
ip=$(echo $i | cut -d: -f3)
sshpass -p $pass ssh $user#$ip bash -c "you commands &"
done
Instead of using cd $HOME use the full path with yours scripts names.And dont forget the & for send the process in background...

Related

How to execute bash script that requires multiply answers? [duplicate]

This question already has answers here:
Pipe multiple verbatim lines into command in bash
(2 answers)
Closed 2 months ago.
Here is the case. I have bash script that requires a couple of command to be executed. First of all, it requires sudo, then answer (y/n) and then password one more time. What I want to do is I want to execute it in one command.
Let's say I have my bash script - myscript.sh. This script requires sudo to be executed. So, to execute it in one line I can write:
echo 'mypassword' | sudo -S bash myscript.sh
And this will work. But after script is executed I need to answer y and type password one more time. How can I do that?
Here is what I have tried:
printf '%s\n mypassword y mypassword' | sudo -S bash myscript.sh
echo 'y\nmypassword\n' | echo 'mypassword' | sudo -S bash myscript.sh
And there were a couple more of what I have tried, but it didn't work.
You can:
printf '%s\n' mypassword y mypassword | ...
( echo mypassword; echo y; echo mypassword ) | ...
This script requires sudo to
Note that typing that on the command line or in a script will publish your password. Instead, configure sudoers to allow the user to login without a password. Consider configuring credential caching in sudoers, so you don't have to type password twice.

Transfer multiple files on single sftp connection in bash [duplicate]

This question already has answers here:
Unix sftp - mput command - transfer all files with a specific prefix
(2 answers)
Closed 3 years ago.
I want to transfer mutiple files on single sftp connection from a folder, where new files are continuously generating, through shell script.
I'm taking approach from this answer using heredocs but failed.
Loop inside "heredoc" in shell scripting
Something like this below code
sftp -P 8922 <server> <<EOF
while [[ true ]]
do
listOfFiles=$(ls -1)
if [[ ! -z $listOfFiles ]]
then
put * /somedir
fi
done
EOF
How can i achieve this?
sftp is not a shell, it doesn't execute scripts like this.
You need to execute a script that prints all the put commands, and pipe it to sftp.
for i in *
do
echo "put $i /somedir"
done | sftp -P 8922 <server>

ssh and execute several commands as another user through a heredoc [duplicate]

This question already has answers here:
Usage of expect command within a heredoc
(1 answer)
Pass commands as input to another command (su, ssh, sh, etc)
(3 answers)
Closed 4 years ago.
I have a script that I need to execute through ssh as another use, is there a way to pass whole script like this:
ssh -t user#server.com sudo -u user2 sh -c << EOF
cd /home
ls
dir=$(pwd)
echo "$dir"
echo "hello"
....
EOF
Returns: sh: -c: option requires an argument
ssh'ing and sudo'ing separately is not an option and putting .sh file directly on the machine is not possible.
sh -c requires a command string as the argument. Since you are reading the commands from standard input (through heredoc), you need to use sh -s option:
ssh -t user#server.com sudo -u user2 sh -s << 'EOF'
cd /home
ls
dir=$(pwd)
echo "$dir"
echo "hello"
...
EOF
From man sh:
-c
string If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0.
-s
If the -s option is present, or if no arguments remain after option processing, then commands are read from the standard input. This option allows the positional parameters to be set when invoking an interactive shell.
Need to quote the heredoc marker to prevent the parent shell from interpreting the content.

Bash command substitution on remote host [duplicate]

This question already has answers here:
How to cat <<EOF >> a file containing code?
(5 answers)
Closed 7 years ago.
I'm trying to run a bash script that ssh's onto a remote host and stops the single docker container that is running.
#!/usr/bin/env bash
set -e
ssh <machine> <<EOF
container=$(docker ps | awk 'NR==2' | awk '{print $1;}')
docker stop $container
EOF
However, I get the following error:
stop.sh: line 4: docker: command not found
When I do this manually (ssh to the machine, run the commands) all is fine, but when trying to do so by means of a script I get the error. I guess that my command substitution syntax is incorrect and I've searched and tried all kinds of quotes etc but to no avail.
Can anyone point me to where I'm going wrong?
Use <<'EOF' (or <<\EOF -- quoting only the first character will have the same effect) when starting your heredoc to prevent its expansions from being evaluated locally.
BTW, personally, I'd write this a bit differently:
#!/bin/sh -e
ssh "$1" bash <<'EOF'
{ read; read container _; } < <(docker ps)
docker stop "$container"
EOF
The first read consumes the first line of docker ps output; the second extracts only the first column -- using bash builtins only.

Bash cat on the last created remote file [duplicate]

This question already has answers here:
Shell Script error: "head: invalid trailing option -- 1"
(3 answers)
Closed 8 years ago.
I am trying to recover the content of the last created file on a remote server.
When connected to the remote server I do this:
cat `ls -t /mypath/*.csv | head -1`
CMD="cat `ls -t /mypath/*.txt | head -1`"
But when I try to use the same command:
ssh#XX.XX.XX.XX $CMD
I get an error: ls cannot access /mypath/*.csv No such file or directory.
The ` is forcing to execute the ls on the local system on not the remote.
Is there another way?
Thank you
Your command is failing is because the backticks in $CMD are expanded locally when you create the variable, rather than being expanded on the remote side. So ssh#XX.XX.XX.XX $CMD is actually going to look something like ssh#XX.XX.XX.XX "cat /mypath/local_file" (and local_file may not exists on the remote host, and is probably not the file you want).
You can prevent this local expansion by providing the command directly to ssh.
ssh user#host 'cat /mypath/$(ls -t /mypath/*.txt | head -1)'
ls returns the pathname relative to the directory so you will also need to include the path of the base directory /mypath/ in your cat invocation. To avoid this hardcoding pass the -d flag to ls.
ssh user#host 'cat $(ls -dt /mypath/*.txt | head -1)'

Resources