3

New to BASH and Linux and need some quick help. I have written a quick bash script that asks the user for a username and password from the command line and passes that info to a remote server later via SSH. Problem is, one user has a ' character in the password and the value is causing EOF errors when using SSH. The following is my code to capture the username and password

echo "Please type in username:"
read username
read -s -p "Enter Password: " password

and the following is where I send the information to the remote server. Note, there is an array of servers I must send this to.

echo "Adding username and password..."  
ssh root@${dssAssocArray[$key]} "echo username=$username > /etc/smbcredentials"
ssh root@${dssAssocArray[$key]} "echo password=$password >> /etc/smbcredentials"

Is there some simple way to dereference the ' (if that's the correct term) or any other special character that might cause an escape?

2 Answers2

3

Just make it:

echo "Adding username and password..."  
ssh "root@${dssAssocArray[$key]}" 'cat > /etc/smbcredentials' << EOF
username=$username
password=$password
EOF
  • Have seen here documents before but haven't really been comfortable using them. Will give it more of a try. Thank you. – antMachines Jan 04 '16 at 18:58
  • Just applied this. Had some slight issues with the EOF not being found for here document but found an explanation as to why with whitespaces being the issue. Included <<-EOF and tabs to fit the structure of my code. Thank you again. – antMachines Jan 04 '16 at 19:10
-1

You should be able to get around that by simply quoting your inner strings on the ssh command. For example

echo "Adding username and password..."  
ssh root@${dssAssocArray[$key]} "echo \"username=$username\" > /etc/smbcredentials"
ssh root@${dssAssocArray[$key]} "echo \"password=$password\" >> /etc/smbcredentials"
David King
  • 3,147
  • 9
  • 23
  • 1
    That just moves the problem to the ", $, backtick, backslash characters. That also assumes the login shell of root understands those " as quoting operators (likely). – Stéphane Chazelas Jan 04 '16 at 18:33