2

So, I am passing a environment variable from bash to python;

#!/usr/bin/env python2
import os

#connect("weblogic", "weblogic", url=xxx.xxx.xxx.xxx:xxxx)
os.environ['bash_variable']

via wlst.sh I can print exported bash_variable, but how do I execute stored variable? Basically, I am trying to remove the original connect statement and pass a variable that has said information. Thanks

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
q5p4k0
  • 51
  • 3
  • 5
  • You want to use `bash_variable` as the `url` parameter? – Andreas Fester Mar 12 '14 at 18:08
  • No, I want to replace connect("weblogic", etc with what is stored in the bash_variable - which IS a complete connect statement. That's the reason I need to execute that stored variable. – q5p4k0 Mar 12 '14 at 18:10
  • Means, `bash_variable` contains something like `connect("weblogic", "weblogic", url="192.168.1.1:7001")`? – Andreas Fester Mar 12 '14 at 18:14
  • Yes, that is correct. – q5p4k0 Mar 12 '14 at 18:16
  • `exec` should work: http://stackoverflow.com/a/701813/1611055. However, when I did something similar, I was passing username, password and url as environment variables and passed them to `connect`. With `exec` you can execute any python code stored in your variable, which could be a security issue – Andreas Fester Mar 12 '14 at 18:17

1 Answers1

1

Question though, why wouldn't you called the script with the variable as an argument and use sys.argv[] ?

By example something like this.

import os
import sys
import traceback
from java.io import *
from java.lang import *




wlDomain = sys.argv[1]
wlDomPath = sys.argv[2]
wlNMHost = sys.argv[3]
wlNMPort = sys.argv[4]
wlDPath="%s/%s" %(wlDomPath,wlDomain)
wlNMprop="/apps/bea/wls/scripts/.shadow/NM.prop"

try:
    print "Connection to Node Manager"
    print ""
    loadProperties(wlNMprop)
    nmConnect(username=NMuser,password=NMpass,host=wlNMHost,port=wlNMPort,domainName=wlDomain,domainDir=wlDPath,mType='ssl',verbose='true')
except:
    print "Fatal Error : No Connection to Node Manager"
    exit()

print "Connected to Node Manager"

The NM.prop file is a 600 file with the username/password for the NM.

EDIT :

So from what I understand you want to do something like this :

URLS = ['t3s://Host1:Port1','t3s://Host2:Port2','t3s://Host3:Port3']
for urls in URLS:
    connect('somebody','password',urls)
    {bunch of commands}
    disconnect()

And the values of the list URLS would be define by the environment.

The way I see it you have 3 choices :

  1. Have 1 script per environment, more or less identical save for the URLS list
  2. Have 1 script but with a conditionnal branching on sys.argv[1] (the environment as a parameter) and create the list there.
  3. Have 1 script which use a parameter file for each environment according to the environment. Each parameter file containing the list in question.

Something like that :

propENV = sys.argv[1]
propPath = "/path1/path2"
propFile = "%s/%s" %(propPath,propENV)

loadProperties(propFile)

I would probably use the properties file option myself as it is more flexible from an operational standpoint...at least IMHO.

Andre Gelinas
  • 912
  • 1
  • 8
  • 10
  • The reason I was utilizing bash was because the url field was assigned in a assoc array. User input would provide environment, and that key would provide more than one value (depending on environment)- meaning there were multiple urls that required connecting to via WLST. I need to remove bash from the equation and use python to create a for loop and run a set of commands for each URL provided. – q5p4k0 Mar 21 '14 at 20:45
  • Well you could load a properties file based on the environment provided by users and containing list of URL (and other informations). – Andre Gelinas Mar 24 '14 at 17:49
  • How exactly would that work with a properties file? The scenario is something along these lines; wlst.sh ./script.py [var] [var] [var] [env] wlst.sh sets up the environment and the script.py connects directly to a managed server and executes commands contained within script.py. The issue is that [env] contains multiple MS - up to 10 in some environments. The bash script is not really appropriate because it would run the command on one URL at a time, exit WLST and re-issue the entire command referenced above for each MS. – q5p4k0 Mar 25 '14 at 13:25