sshpass append string to a remote file

1.8k views Asked by At

I need to append a variable string, via SSHPASS (bash script), to an existing file placed in the Remote Machine.

I tried

echo "$test" | sshpass -p $pass $host 'cat >> /remote/full/path/log/report.log'

sshpass -p $pass $host "echo $test" >> /remote/full/path/log/report.log

Nothing Works

3

There are 3 answers

0
DanieleO On BEST ANSWER

I figured out:

sshpass -p $pass' ssh -o StrictHostKeyChecking=no $host "cd /full/path/ && echo $test >> /full/path/report.log"
0
Fabio Marzocca On

Given that storing a password in a script is a great security flaw, this is sshpass syntax:

sshpass -p 'your_pass_here' ssh user@domain 'df -h'
0
Erez Binyamin On

You need to add the 'StrictHostkeyChecking=no' option to ssh:

echo "$test" | sshpass -p $pass ssh - o StrictHostkeyChecking=0 $host 'cat >> /remote/full/path/log/report.log'

I don't know why this works, but enjoy the bash magics. lol

ASIDE:

If you set the password of the user you're sshing into as the envar $SSHPASS instead of $pass, then you can use the sshpass -e option.

SSHPASS=$pass
echo "$test" | sshpass -e ssh - o StrictHostkeyChecking=0 $host 'cat >> /remote/full/path/log/report.log'