Replace environment variables with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace environment variables with sed
# 1  
Old 07-23-2008
Replace environment variables with sed

Hi,

I need to write a sed that replaces the value of all environment variables that have PASS in the name with ***** . I have a log file who prints the value of all environment variables ,including those who hold passwords. In the name of these variables I found always the PASS string, ex: OP_ORA_PASS, OP_DSU_PASS,etc.

What I did until now is the next sed :
sed 's/'"$OP_ORA_PASS"'/******/g;s/'"$DSU_ORA_PASS"'/******/g'

I need an improvment so that the sed will check and replace all the variables with PASS in the name,kind of $*_PASS.

Thank you for your help.
# 2  
Old 07-23-2008
Please provide an example of your current logfile; otherwise, we're likely to miss a corner case.

Also, you say you're replacing the values of those variables, but it looks like you're just changing the name to "*****". So an example of ideal output from your logfile snippet would be appreciated, too.
# 3  
Old 07-23-2008
If I understand you right, you are going to add some filter into the print-env-var-script?!
If so, (assuming the environment printed out with variable names) you coud 'sed' for any line with substring 'PASS=' and replase following with '*****':
Code:
> env | sed  's/^\(.*PASS=\).*/\1*****/'

- so, it's remembering as \1 a beggining of a string with 'PASS=' and replace that string with the remembered \1 part and '*****'
The same pipe you can add in command of executing the script; so it will filtering the script's output.

If you do not have the variable's names (seems unbelievable), you can get all *PASS variables into a file or local variables and filtering printout in the way you already have, using retrieved values.

. (By the way, having a variables in sed-command part it is better to use:
... sed "commands" file
. ( - IMHO) )

For local variable: (I've used '*NAME' variables)
Code:
> for ln in $(env|grep -i name); do 
>>  sed_cmd=$sed_cmd"s/${ln#*=}/*****/g; "; 
>>done
>echo $sed_cmd
s/dstnsun0/*****/g;
s/geo_usa/*****/g;
s/dca0701/*****/g;
> env|sed "$sed_cmd" | grep -i name
MAPMARKER_RPC_HOSTNAME=*****
MAPMARKER_DBNAME=*****
LOGNAME=*****

For file:
Code:
>rm sed_cmd.t
> for ln in $(env|grep -i name); do ec "s/${ln#*=}/*****/g;">>sed_cmd.t; done
> env|sed -f sed_cmd.t | grep -i name

# 4  
Old 07-24-2008
Thank you Alex for the examples, its exactly what I needed.
I used sed 's/^\(.*PASS=\).*/\1*****/' and it did the job.
Can you explain me what this sed contains? Or maybe give a link to read? I need to make an adaptation of it and I need to know more.
I have the following line in the log :

ORA_CONNECT_STRING=username value/password

The username name is taken from the $USERNAME variable.

How do I write the sed to change the password to *****?
sed 's/^\(.*CONNECT_STRING='"$USERNAME"'/\).*/\1*****/'
Is that correct ?
# 5  
Old 07-24-2008
Worth noting that the scripts, as listed, will fail to (always, fully) protect a password containing the string "PASS=".

To fix that issue, I'd go with:
Code:
s/^\([^=]*PASS=\)/\1*****/

# 6  
Old 07-27-2008
Thank you Dan for your help.

I have another problem to resolve involving sed:
In the log I have the following line :

+ sqlplus -S env10/env10pass@DBL9

I put :

s/^\(.*+ sqlplus -S '"$ORA_USER"'\).*/\1\/\*******@'"$DB_NAME"'/


env10 is the value of $ORA_USER. Unfortunely this variable is an application-related one, and does not exist anymore when I execute the sed(after the log is writen). Because of this I get the following result :

+ sqlplus -s /****@DBL9

Thats because $ORA_USER is empty....



What I need is a sed that looks up the string :
+ sqlplus -S ,skips the rest until the / and then changes the string between the / and @ with *****

Thank you all for the help !
# 7  
Old 07-27-2008
Assuming your desired output is:

+ sqlplus -s env10/****@DBL9

you can try this:

Code:
sed 's!\(.*/\).*\(@.*\)!\1****\2!'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Environment Variables

Hi All, I need to understand following three environment variables and their usages in HP Unix. _M_ARENA_OPTS _M_CACHE_OPTS PTHREAD_SCOPE_SYSTEM How does these environment variables influence multi threaded applciation and how do we decide the value of these variables? Is there... (0 Replies)
Discussion started by: angshuman
0 Replies

2. Shell Programming and Scripting

Sed, replace variables in file

Hi Everyone, I need some help with sed and I'm totally new to it. I have a template file with variables in it. These variables start with a '$' sign and are exactly one character long (plus the '$' sign). For example: $a, $b, etc. Each variable should be replaced with the contents of a... (9 Replies)
Discussion started by: csarli2006
9 Replies

3. Homework & Coursework Questions

Environment Variables

1. The problem statement: What is the mesg value set for your environment? If it is on, how would you turn off your current session? How would you set it permanently? 3. The attempts at a solution : Read Unix The textbook. 3rd chapter has many things like environment variables and... (5 Replies)
Discussion started by: mahinkhan22
5 Replies

4. HP-UX

Environment Variables

Hi Experts, Need your help in understanding the commands to setup the environment variables in hp-ux. Beleive need to use either set,setenv or export. I am confused between above three options, when to use which option? On command line, I have tried both set and setenv but couldn't... (1 Reply)
Discussion started by: sai_2507
1 Replies

5. Shell Programming and Scripting

environment variables in a sed script file

Hello Everyone I need to create a script file which must append some lines to a target text file, I'm using sed for windows, the script file look like this: { a\ STRINGTABLE DISCARDABLE\ BEGIN\ 5, 150 {a\ #define RC_SHELL, "%ID_SHELL%"\ #define RC_NAME, "%ID_NAME%"\ END } ... (1 Reply)
Discussion started by: edgarvm
1 Replies

6. Shell Programming and Scripting

replace two character strings by two variables with sed command

Hello, I want to writte a script that replace two character strings by two variables with the command sed butmy solution doesn't work. I'm written this: sed "s/TTFactivevent/$TTFav/g && s/switchSLL/$SLL/g" templatefile. I want to replace TTFactivevent by the variable $TTFav, that is a... (4 Replies)
Discussion started by: POPO10
4 Replies

7. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

8. Programming

environment variables

hi, I want to create a new EV(Environment Variable) through a c program and I done this thing through setenv() method. But the newly created EV is not permanent, i.e. when I exit from the program the EV also no longer lives. But I want to make it a permanent EV for the current user. Actually I... (6 Replies)
Discussion started by: sumsin
6 Replies

9. UNIX for Dummies Questions & Answers

environment variables

Hi Folks, Is it possible somehow to unset all the environment variables which have been defined before in UNIX (Solaris). Thanks, Slava (3 Replies)
Discussion started by: spavlov
3 Replies

10. Programming

environment variables

Hi! How-to get the environment variables in GNU. getenv() only fetches the ones that you can find under export (not the ones under declare)... best regars .David (2 Replies)
Discussion started by: Esaia
2 Replies
Login or Register to Ask a Question