SMBClient from multiple ip [duplicate] - bash

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

Related

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

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

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.

How to use tee when using sudo [duplicate]

This question already has answers here:
How do I use sudo to redirect output to a location I don't have permission to write to? [closed]
(15 answers)
Closed 1 year ago.
UBuntu 16.04
Bash 4.4
In 4-bash-update.sh line 158:
cd "$drive00" && sudo -H -u myuser bash -c "timeout 2s ./binaryfile -gentoken" > "${save_log_dir}"/update-"${now}".log;
^-- SC2024: sudo doesn't affect redirects. Use ..| sudo tee file
I tried a few times and each time my file gets eaten.
You don't need to use tee, just put the redirection inside the command that's executed with bash -c:
sudo -H -u myuser bash -c 'timeout 2s ./binaryfile -gentoken > "$1"' _ "${save_log_dir}/update-${now}.log"
If you redirect outside, your original shell is trying to open the file, but it doesn't have permission. Putting it inside the bash argument executes it in the target user's shell, with their permissions.
The _ in the command line is a dummy value for the $0 parameter of the shell. You need that placeholder to be able to supply the filename as $1.

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.

Chain together commands with SSH on ProCurve

I need to back up the configuration of a HP ProCurve and I need to do it through SSH.
The ProCurve shell is interactive and I can't put something like:
$ ssh user#ip 'command1; command2'
...because the shell gives me an error — likely because the machine doesn’t actually have a Unix–like shell. I need to connect, input the password, and execute two commands.
With Procurve/Cisco devices you can not simply run ssh hostname command. You need to use some tool that can work with interactive ssh sessions, i.e. expect, pexpect or something like this. Another solution is to use socat:
(sleep 5; echo ${PASSWORD}; sleep 2; echo ; echo command1; sleep 2) \
| socat - EXEC:"ssh ${SWITCH}",setsid,pty,ctty
I'm not sure I entirely understand your question. Are you trying to run multiple commands?
Have you tried sh -c ?
ssh user#ip sh -c "command1; command2"
From man sh:
-c Read commands from the command_string operand instead of from the standard input. Special parameter 0 will be set from the command_name operand
and the positional parameters ($1, $2, etc.) set from the remaining argument operands.

Resources