1

I encountered a problem, I use sshpass and I need to write a script, but this command is not displayed in the script

If I enter this command into the terminal, then everything works for me and I get the output of all hidden folders on my server, but in the script itself it does not work

sshpass -p 'my_pswd' ssh srv@srv-server ls -a

This script displays an empty field, how would I get the output - ls -a

#!/bin/bash
value=$(sshpass -p 'my_pswd' ssh srv@srv-server ls -a)
echo $value

Thank you in advance :)

jww
  • 97,681
  • 90
  • 411
  • 885
  • Works for me, except that you should set the variable in the last line into double quotes, else the content of `value` will be printed in one line. Are you missing some important information here? Are you getting error messages? What's the output of the script if you remove the command substitution and variable assignment (`value=$(...)`)? – Murphy Sep 17 '19 at 11:18
  • @Murphy, without variables same problem and I realized that the script works if I run it ```./script```, but don't work with ```sudo ./script``` –  Sep 17 '19 at 11:22
  • I'd shy away from using sshpass as many hosts are set to record the commands that are entered, and your password would be all through those audit logs. – Calvin Taylor Sep 17 '19 at 11:49
  • @CalvinTaylor You probably mean the shell history (`~/.bash_history`)? This won't record commands from shell scripts, only the ones provided interactively. But in general you're right, though you can work around by preceeding the command with one or more spaces (if you think of it), or issuing `history -c`. Using the environment variable `SSHPASS` would only shift the problem to another command. I advise to read [section *Security Considerations* in the manpage](https://linux.die.net/man/1/sshpass). – Murphy Sep 17 '19 at 12:38

0 Answers0