Variable Expansion in Sudo over SSH with Passwords

The trick is to set the ssh command as a variable so that the expansion happens at the correct time.

#!/bin/bash
 cmd="echo XXXXXXXX | sudo -S mongo 127.0.0.1/admin -u admin -p $pass --eval 'printjson(rs.conf())'"
 ssh -q -o BatchMode=yes -o ConnectTimeout=10 -o StrictHostKeyChecking=no user@host $cmd

Comments