How to run multiple commands with SSH on a remote server in a bash script? [duplicate] - bash

This question already has answers here:
What is the cleanest way to ssh and run multiple commands in Bash?
(14 answers)
Shell script: Run function from script over ssh
(3 answers)
Execute bash command stored in associative array over SSH, store result
(2 answers)
Closed 3 years ago.
I want to run a script that checks if the specific folder exists on a remote server then greps a specific line from a specific file in that server to the local machine.
if ssh -t -t user#server [ -d /etc/nginx ]; then
ssh -t -t user#server
ls -1a /etc/nginx/conf.d | grep $1 | xargs cat | grep specific_line | grep .specific-extension | awk '{print $2}'
fi
I use awk '{print $2}' to print out the second line of the grepd line
SO I want this to be an output in my local machine or even better I want to put that in a variable in the script.
I haven't find anything on the internet that solves even the simplified version of this.
I have PSK enabled on the servers so I don't have to enter the password when I ssh.

I just did something similar using paramiko in Python. I test the sudo privileges of many accounts over hundreds of IPs in a few minutes. You can run the command and get stdin, stdout, and stderr. That should get you started in the right direction 😉
You can use logins and certs with it too if that helps.

Related

How Can I Run Commands In A SFTP Session Via A Bash Script? [duplicate]

This question already has answers here:
SFTP bash shell script to copy the file from source to destination
(3 answers)
Closed 1 year ago.
I am trying to run the cd command in a bash script to a SFTP session. My code currently looks like
#!/bin/bash
sftp $1#$2
and then I want to use cd within the SFTP session and other commands too but cd is fine for now. How can I do this?
Try the batch mode. Quoting the man page:
-b batchfile
Batch mode reads a series of commands from an input
batchfile instead of stdin. [...] A batchfile of ‘-’ may be used to indicate standard input.
You can use batch mode to run commands in sequence, e.g.
#!/bin/bash
echo << EOF > sftp-commands-to-run.txt
ls
put myfile.txt
... more commands ...
EOF
sftp -b sftp-commands-to-run.txt $1#$2
You can also pass the commands to run via stdin, e.g.
#!/bin/bash
echo ls myfile.txt | sftp -b - $1#$2

Diffrence between bash script.sh and ./script.sh [duplicate]

This question already has answers here:
History command works in a terminal, but doesn't when written as a bash script
(3 answers)
Closed 2 years ago.
Suppose we have env.sh file that contains:
echo $(history | tail -n2 | head -n1) | sed 's/[0-9]* //' #looking for the last typed command
when executing this script with bash env.sh, the output will be empty:
but when we execute the script with ./env.sh, we get the last typed command:
I just want to know the diffrence between them
Notice that if we add #!/bin/bash at the beginning of the script, the ./env.sh will no longer output anything.
History is disabled by BASH in non-interactive shells by-default. If you want to enable it however, you can do so like this:
#!/bin/bash
echo $HISTFILE # will be empty in non-iteractive shell
HISTFILE=~/.bash_history # set it again
set -o history
# the command will work now
history
The reason this is done is to avoid cluttering the history by any commands being run by any shell scripts.
Adding hashbang (meaning the file is to be interpreted as a script by the program specified in your hashbang) to your script when being run via ./env.sh invokes your script using the binary /bin/bash i.e. run via bash, thus again printing no history.

SMBClient from multiple ip [duplicate]

This question already has answers here:
Pass commands as input to another command (su, ssh, sh, etc)
(3 answers)
Script fails with spaces in directory names
(1 answer)
Closed 2 years ago.
I would like to make a bash script where I read a list of IP addresses and run the following command:
smbclient \\\\ $ ip \\ ipc $ -U ". \ User" --pw-nt-hash
which does an exit and try with another IP, regardless of that it throws a message if the connection was successful, it does not execute with the IPs that are inside the list, it only tries with the first one in the list.
#/bin/bash
IPLIST="ip"
for ip in $(cat ip)
do
smbclient \\\\$ip\\C$ -U ".\user" --pw-nt-hash "user"
exit
done
If you don't want the script to exit after the first smbclient, drop the exit command.
smbclient \ $ ip \ ipc $ -U ". \ User" --pw-nt-hash, which does an exit
This exit is not done by smbclient, but rather by the script; therefore it ends.
You seem to assume that the exit gets passed as input to smbclient, but that's not how this works. You run smbclient and when it finishes, your script continues, and executes the exit. See Pass commands as input to another command (su, ssh, sh, etc) for a fuller discussion.
Also, don't read lines with for.
#/bin/bash
while read -r ip; do
smbclient \\\\$ip\\C$ -U ".\user" --pw-nt-hash "user" <<<exit
done <ip

SSH authentication verification with Bash Shell script on multiple hosts [duplicate]

This question already has answers here:
While loop stops reading after the first line in Bash
(5 answers)
Pass commands as input to another command (su, ssh, sh, etc)
(3 answers)
Closed 2 years ago.
I know there are other solution such as expect, or Python paramiko, but bash shell is the only option available for now.
Out of these 3 IPs, only 172.16.1.2 has SSH server installed and they have similar password.
wolf#linux:~$ cat ip.txt
172.16.1.1
172.16.1.2
172.16.1.3
wolf#linux:~$
This is the Bash script
wolf#linux:~$ cat sshSession.sh
while read host
do
export SSH_ASKPASS='~/ePass'
setsid ssh -T user#$host
if [ $? = 0 ]; then
exit
echo "$host | SSH Authentication OK"
else
echo "$host | SSH Authentication PROBLEM"
fi
done < ip.txt
wolf#linux:~$
Output
wolf#linux:~$ ./sshSession.sh
ssh: connect to host 172.16.1.1 port 22: No route to host
172.16.1.1 | SSH Authentication PROBLEM
Welcome to Ubuntu Server
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
-bash: line 1: 172.16.1.3: command not found
172.16.1.2 | SSH Authentication PROBLEM
wolf#linux:~$
There are a few problems here.
I just want to get the authentication status only, not to log in into the server.
There is something wrong with the output -bash: line 1: 172.16.1.3: command not found
172.16.1.2 | SSH Authentication PROBLEM - This is the only host installed with SSH, while others not.
Desired Output
wolf#linux:~$ ./sshSession.sh
172.16.1.1 | SSH Authentication PROBLEM
172.16.1.2 | SSH Authentication OK
172.16.1.3 | SSH Authentication PROBLEM
What's wrong in the script and how to fix it?
You seem to be assuming that the exit gets written into the ssh session, but that's not how it works. You execute exit after the ssh process terminates. See Pass commands as input to another command (su, ssh, sh, etc)
Also, you seem to be passing standard input from your while loop into the running ssh instance. See Shell script while read line loop stops after the first line
Also, Why is testing "$?" to see if a command succeeded or not, an anti-pattern?
Also, use read -r as a general principle (though I guess it doesn't really matter here). And don't repeatedly set SSH_ASKPASS to the same value. (And generally don't export a variable more than once; it's harmless but usually reveals more than you like about your understanding of what export actually does.) And, quote your variables.
export SSH_ASKPASS='~/ePass'
while read -r host
do
if setsid ssh -T -n user#"$host" true; then
echo "$host | SSH Authentication OK"
else
echo "$host | SSH Authentication PROBLEM"
fi
done < ip.txt

Bash Script SSH Commands on Remote Server Not Executing as Expected [duplicate]

This question already has answers here:
Shell script: Run function from script over ssh
(3 answers)
Nested grep with SSH
(1 answer)
Closed 4 years ago.
So I've got a bash script in which I want to SSH onto one of my remote servers and run some commands. This is my code:
MYFUNCTION="
function my_function
{
VAR=$(readlink -f current | sed 's/[^)
}
my_function
"
ssh -l ${USERNAME} ${HOSTNAME} "${MYFUNCTION}"
The problem is that the VAR variable is not being populated with the command output as it should. I've run the exact same command myself, and I get the desired output, but when doing it through SSH in the bash script, it doesn't work as expected. What am I doing wrong here?
You are putting the code in double quotes, so the variables and commands are being executed on your local machine. Do echo "$MYFUNCTION" and you'll probably be surprised.
Try using a quoted here document:
# Note the single quotes in the next line
ssh -l "$USERNAME" "$HOSTNAME" <<'END_CODE'
function my_function
{
cd www
VAR=$(readlink -f current | sed 's/[^0-9]*//g')
VAR2=$(find . -maxdepth 1 ! -newer "$VAR" ! -name "$VAR"| sort | sed '$!d')
}
my_function
END_CODE
Note also all the quoted variables.

Resources