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?

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 4 '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 4 '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"
  • 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 4 '16 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.