Find the answer to your Linux question:
Results 1 to 7 of 7
Hi, I am using Ubuntu 12.04 for development. I have the following problem: I have a variable that stores a few JVM arguments, like for example: Code: VAR="-Dprop1=value1 -Dprop2=value2 -Dprop3=value3 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2013
    Posts
    5

    Lightbulb Masking password in script echo


    Hi,

    I am using Ubuntu 12.04 for development.
    I have the following problem:

    I have a variable that stores a few JVM arguments, like for example:
    Code:
    VAR="-Dprop1=value1 -Dprop2=value2 -Dprop3=value3 -Dprop4=value4 -Dprop5=value5"
    Sometimes, one of the propertie/value pair will store a password that I need to hide when echo-ing $VAR content. Now, the problem is that the order of the properties and the number is variable within $VAR, so the only thing I know is that if a certain property (let's call it "user.password") is contained in $VAR, I have to replace its value with a mask (something like ***) and echo the content of $VAR like in the following example:

    Code:
    "Application arguments: -Dprop1=value1 -Duser.password=*** -Dprop3=value3 -Dprop4=value4 -Dprop5=value5"
    I've tried this using an if statement and "sed":

    Code:
    if [[ "$VAR" == *user.password* ]]
      then
        OFUSCATE_PWD=$(echo $VAR | sed 's/ user.password=.\+ / user.password=*** /' )
        echo "Application arguments: ${OFUSCATE_PWD}"
    fi
    But something does not work properly because it also strips some of the properties that are not named "user.password". I blame the spaces inside the expression, but using \s does not give any result...

    Thanks!
    Last edited by atreyu; 04-18-2013 at 09:19 PM. Reason: added CODE tags to aid in readability

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Hello and welcome!

    I've moved your thread to the Programming/Scripting forum, as it will probably get better attention there.

  3. #3
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by roccatgaming View Post
    Code:
    "Application arguments: -Dprop1=value1 -Duser.password=*** -Dprop3=value3 -Dprop4=value4 -Dprop5=value5"
    I've tried this using an if statement and "sed":
    I have trouble getting sed to work with non-greedy regexes. If perl is an option, how about this?

    Code:
    echo "$VAR"|perl -pe 's|(-Duser\.password=)(.*?) -D|\1***** -D|'

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Apr 2013
    Posts
    5
    The perl expression does not work either... it just prints the $VAR without masking the value of argument "-Duser.password=password"....

  6. #5
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,763
    Are you trying to safely pass password to a script with
    Code:
    VAR="-Dprop1=value1 -Duser.password=*** -Dprop3=value3 -Dprop4=value4 -Dprop5=value5"
    ?

    If that's the case, is it possible to modify the script to take password from a file ?

    Say something like

    Code:
    ##Inside script###
    
    mypasswd=`cat $PATH_TOMYPASSWD`
    
    
    where # echo $PATH_TOMYPASSWD
    /etc/secret/path/passwd.txt
    
    where#cat $PATH_TOMYPASSWD
    this is secret password!
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  7. #6
    Just Joined!
    Join Date
    Apr 2013
    Posts
    5
    It seems that:

    Code:
    $(echo $VAR | sed -e 's/-Duser.password=[^[:space:]]*/-Duser.password=********/')
    does the job.

  8. #7
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    I see you found a solution - that is good!

    Quote Originally Posted by roccatgaming View Post
    The perl expression does not work either... it just prints the $VAR without masking the value of argument "-Duser.password=password"....
    that is odd. this works for me:
    Code:
    #!/bin/bash
    VAR="Application arguments: -Dprop1=value1 -Duser.password=secret -Dprop3=value3 -Dprop4=value4 -Dprop5=value5"
    
    echo -e "$VAR\n"
    
    a=$(echo "$VAR"|perl -pe 's|(-Duser\.password=)(.*?) -D|\1******** -D|')
    echo a: $a
    
    b=$(echo $VAR | sed -e 's/-Duser.password=[^[:space:]]*/-Duser.password=********/')
    echo b: $b
    it outputs this:
    Code:
    Application arguments: -Dprop1=value1 -Duser.password=secret -Dprop3=value3 -Dprop4=value4 -Dprop5=value5
    
    a: Application arguments: -Dprop1=value1 -Duser.password=******** -Dprop3=value3 -Dprop4=value4 -Dprop5=value5
    b: Application arguments: -Dprop1=value1 -Duser.password=******** -Dprop3=value3 -Dprop4=value4 -Dprop5=value5
    Code:
    $ perl -v
    
    This is perl 5, version 14, subversion 3 (v5.14.3) built for i386-linux-thread-multi

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •