Bash script and escaping special characters in password

113,217

Solution 1

Use double quotes twice, escaped and not escaped: -p"\"$MYPASSWORD\""

#!/bin/sh
source pass.cre
/usr/bin/ssh -p 91899 user@remoteHost 'mysqldump -u db_user -p"\"$MYPASSWORD\"" my_database |  gzip -c >  my_database.sql.gz'

Or an other version

/usr/bin/ssh -p 91899 user@remoteHost "mysqldump -u db_user -p\"'io#bc@14@9$#jf7AZlk99'\" my_database | gzip -c > my_database.sql.gz"

Examples

% source pass.cre
% ssh user@host mysqldump -u root -p$MYPASSWORD    
user@host's password: 
zsh:1: bad pattern: -p#8111*@uu(

% source pass.cre
% ssh user@host mysqldump -u root -p"$MYPASSWORD"   
user@host's password: 
zsh:1: bad pattern: -p#8111*@uu(

% source pass.cre
% ssh user@host mysqldump -u root -p"\"$MYPASSWORD\""
user@host's password: 
Warning: Using a password on the command line interface can be insecure.

Solution 2

The problem is that your string is being interpreted twice, once by the local shell, and again by the remote shell which ssh is running for you. So you need to quote twice, using either of these:

-p\''#8111*@uu('\'
-p"'#8111*@uu('"

Edit: If you are going to double-quote "" the entire command, you will have problems with passwords containing $. You need to single-quote the command to avoid this. But you still need to single quote the -p value as it is interpreted twice. So you need single quotes inside single quotes.

This is done by use a single quoted quote (\') as in this example:

'I don'\''t like java'

will give you the string I don't like java. So your double-quoted example becomes the single-quoted:

/usr/bin/ssh -p 91899 user@remoteHost 'mysqldump -u db_user -p'\''io#bc@14@9$#jf7AZlk99'\''my_database | gzip -c > my_database.sql.gz'

Don't you just love it?

Share:
113,217

Related videos on Youtube

Saahib
Author by

Saahib

Updated on September 18, 2022

Comments

  • Saahib
    Saahib over 1 year

    I have been reading a lot of questions already asked here, however, somehow nothing is working for me. I have a bash script where I have to send password which dump database on remote machine, so its like :

    !/bin/sh
    /usr/bin/ssh -p 91899 user@remoteHost mysqldump -u db_user -p#8111*@uu( my_database |  gzip -c >  my_database.sql.gz
    

    Now thing is that this password has all sorts of special character: #8111*@uu(

    If I run above command directly in shall using password inside single quotes then it works : ie.

    /usr/bin/ssh -p 91899 user@remoteHost mysqldump -u db_user -p'#8111*@uu(' my_database |  gzip -c >  my_database.sql.gz
    

    Without single quotes I get error with for the '(' at end.

    I also tried to escape characters in password like this :

    !/bin/sh
    /usr/bin/ssh -p 91899 user@remoteHost mysqldump -u db_user -p'\#8111\*\@uu(' my_database |  gzip -c >  my_database.sql.gz
    

    Then it gives access denied error.

    I also tried to use "source" ie. saving password in another file as :

    File pass.cre

    MYPASSWORD='#8111*@uu('
    

    Then including that file in bash script:

    !/bin/sh
    source pass.cre
    /usr/bin/ssh -p 91899 user@remoteHost mysqldump -u db_user -p$MYPASSWORD my_database |  gzip -c >  my_database.sql.gz
    

    It appears to be reading $MYPASSWORD from file then again error of invalid character.

    Any advice what I am missing ?

  • Saahib
    Saahib almost 9 years
    Thanks @meuh and A.B. , it certainly makes sense and I have tested it, its working. Thanks guys for pointing me to right direction.
  • Saahib
    Saahib almost 9 years
    There seems to be a little problem , I have password like this : io#bc@14@9$#jf7AZlk99 For remote , its not working ie. /usr/bin/ssh -p 91899 user@remoteHost "mysqldump -u db_user -p'io#bc@14@9$#jf7AZlk99' my_database | gzip -c > my_database.sql.gz" I tried to escape as suggested here ie. keeping in mind that its been executed twice but I think when sending command via SSH to remote server, things appears to be little changed.
  • A.B.
    A.B. almost 9 years
    Use /usr/bin/ssh -p 91899 user@remoteHost "mysqldump -u db_user -p\"'io#bc@14@9$#jf7AZlk99'\" my_database | gzip -c > my_database.sql.gz"
  • meuh
    meuh almost 9 years
    @Rick_IRS see my edit to my answer as you have a problem with $# and need single quotes throughout.