I am something of a beginner with python so I am writing bash script wrapper that passes some arguments to wlst - jython script that reads these values.
for example:
./serverArgUpdate.sh myAdminServer 40100 'server_MS1, server_MS2' -Dweblogic.logs2=debug
However, in my WLST script I read the values but having issue with arg value 4 == server_MS1, server_MS2
import sys
### Domain Properties ####
adminURL = sys.argv[1]
domainUserName = sys.argv[2]
domainPassword = sys.argv[3]
svrName = str(sys.argv[4])
print("The server names are "+svrName)
setarg = sys.argv[5]
############### Connecting to AdminServer #################################
def connectAdmin() :
try:
connect(userConfigFile=domainUserName,userKeyFile=domainPassword,url=adminURL)
except:
banner('Unable to find admin server..')
exit()
def banner(line):
print('='*80)
print(line)
print('='*80)
########################################################################
def updateSvrArg():
for i in svrName:
cd('/Servers/'+i+'/ServerStart/'+i)
...
############### Main Script #####################################
if __name__== "main":
try:
updateSvrArg()
disconnect()
except:
dumpStack()
banner("There was an error with the script: Check the properties file or check if Someone has the lock/edit on the console")
cancelEdit(defaultAnswer='y')
exit(exitcode=0)
sys.exit(1)
While executing
wlst.sh serverArgUpdate.py t3://myAdminServer:40100 systemKeys/system_dev.config systemKeys/system_dev.key server_MS1,server_MS2 -Dweblogic.logs=debug
Initializing WebLogic Scripting Tool (WLST) ...
Welcome to WebLogic Server Administration Scripting Shell
Type help() for help on available commands
The server names are server_MS1
Error: No domain or domain template has been read.
Error: No domain or domain template has been read.
========================================
The server name is s
========================================
basically should read
svrName=('server_MS1','server_MS2') to work
how do I pass 'server_MS1','server_MS2' to svrName variable from the bash script to wlst?
any help much appreciated.
Related
I am trying to create a Python script generation tool that creates Python files based on certain actions. For example:
action = "usb" # dynamic input to script
dev_type = "3.0" # dynamic input to script
from configuration import config
if action == "usb":
script = """
#code to do a certain usb functionality
dev_type = """ + dev_type + """
if dev_type == "2.0"
command = config.read("USB command 2.0 ")
proc = subprocess.Popen(command,stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=False)
elif dev_type == "3.0":
command = config.read("USB command 3.0 ")
proc = subprocess.Popen(command,stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=False)
"""
elif action == "lan":
script = """
#code to run a certain lan functionality for eg:
command = config.read("lan command")
proc = subprocess.Popen(command,stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=False)
"""
with open("outputfile.py", w) as fl:
fl.writelines(script)
The actual script generator is much complex.
I generate the "outputfile.py" script in my local machine inputting the action to the script and then deploy the script generated to remote machines to record the results. This is part of a larger framework and I need to keep this execution format.
Each "script" block uses a config.read function to read certain variables required for it to run from a config file. "command" in the above example.
My actual frame work has some 800 configurations in the config file - which I deploy on the remote machines along with the script to run. So there is a lot of extra configurations in that file that may not be required for a particular script to run and is not very user friendly.
What I am looking for is based on the "script" block that get written to the output file - create a custom config file that contains only the config that is required for that script to run.
For example if action is "lan", the below script block get written to the output file
script = """
#code to run a certain lan functionality for eg:
command = config.read("lan command")
proc = subprocess.Popen(command,stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=False)
"""
What I want is only the "lan command" in my custom config file.
My question is :
How do I evaluate the "script" block (inside triple quotes) to know which config are been used in that code and then to write that config to my custom config file when I write to output file ?
There could be multiple if conditions inside the "script" block also. I do not want config for "USB command 2.0" and "USB command 3.0" in the custom config file if the action = "usb" and dev_type = "3.0"
Of course I cannot execute the code inside the "script" block - and then intercept config.read() function to write the config that got called to my custom config file. Is there a better way to do it ?
I am stumped as to what this issue could be. The nagios log does not report any errors anymore but my file does not get written with anything.
def log_something(host_name host_address, attempt_number):
with open('file', 'a+') as log:
log.write('called function at %s with args %s' %s (str(datetime.datetime.now()), locals()))
def main():
parser = argparse.ArgumentParser()
parser.add_argument('host_name')
parser.add_argument('host_address')
parser.add_argument('attempt_number')
args = parser.parse_args()
log_something(args.host_name, args.host_address, args.attempt_number)
if __name__ == "__main__":
main()
And in my commands.cfg
define command {
command_name my_command
command_line $USER1$/my_command.py $HOSTNAME$ $HOSTADDRESS$ $HOSTATTEMPT$
}
And in my host config
define host {
...
event_handler my_command
}
And in the nagios log (journalctl -xe)
OST ALERT: test-router;UP;HARD;5;PING OK - Packet loss = 0%, RTA = 0.96 ms
Jan 31 15:38:47 nagios-server.nagios[9212]: HOST EVENT HANDLER: test-router;UP;HARD;5;my_command
Nothing is written to the file, no errors are reported. When there were errors in my syntax the nagios log would print the errors that were reported to stderr, one of which was a file permission issue. I fixed that by creating the file in the same folder and chmod 777 everything. Besides if that was an issue it should be logged.
Anyone have any idea what's going on here?
I figured something out. It seems as if you define where the output of nagios event handlers goes in the nagios.cfg file.
# TEMP PATH
# This is path where Nagios can create temp files for service and
# host check results, etc.
temp_path=/tmp
I think this means that the output of the plugin checks get sent to the temp folder. However, changing that path seems to have no effect and the result of my output in the python script still gets written as file in the /tmp folder. This is CentOS7.
Check you function, it seem you missing the "," in function parameters.
def log_something(host_name host_address, attempt_number):
with open('file', 'a+') as log:
I'm working with Django and I'd created two database. Everything seems to work fine, but then I had to edit one of the two and add a column.. From that moment the db wouldn't work anymore, so I exported in a text file the first database and thinking "now I recreate the two db and run a python script to refill the first one". The problem is that whene I try to run the script I get errors, because I can't run the command like bash using os.system, and I don't really know any other way... So, here's my code:
import os
def func ():
try:
FILE=open ("./languagedb.txt", "r")
except IOError:
print 'Can\'t open db file'
exit (1)
for line in FILE:
if (line.startswith('INSERT')):
values=line[43:-1]
language=values[1:3]
values=values[6:]
field=""
fieldBool=True
i=0
while fieldBool:
try:
c=values[i]
except:
print ''
if c != '\'':
field=field+str(c)
i=i+1
else:
fieldBool=False
values=values [(i+3):]
text=""
textBool=True
i=0
while textBool:
try:
c=values[i]
except:
print ''
if c != '\'':
text=text+str(c)
i=i+1
else:
textBool=False
comand="Language.objects.create(language=\""+language+"\", text=\""+text+"\", campo=\""+field+"\")"
os.system(comand)
This is the way I call the shell:
python manage.py shell
and the commands I give it:
import django
from languageMods.models import *
import mymigration #The name fo the file containing the above code
mymigration.func()
And I get the following error, for example
sh: -c: line 0: syntax error near unexpected token `language="en",'
Which is shell's error.
Does someone know how to execute a command from a python script in a python shell?
If you start your script the way you describe it you can just call the django DB API directly in your code:
Language.objects.create(language=language, text=text, campo=field)
I'm reading Mark Lutz's Programming Python 3rd editon , and I'm puzzled with a question: type('something') always result in an empty string.
Could someone kindly explain this?
Context info:
I add one line to the script $CODEROOT\pp3e\Internet\Web\cgi-bin\tutor0.py
#!/usr/bin/python
#######################################################
# runs on the server, prints html to create a new page;
# url=http://localhost/cgi-bin/tutor0.py
#######################################################
print "Content-type: text/html\n"
print "<TITLE>CGI 101</TITLE>"
print "<H1>A First CGI script</H1>"
print '<p>[%s]'%type('apple') # ★ I add this line
print "<P>Hello, CGI World!</P>"
For the added line, I hope to see in browser [<type 'str'>] , but I actually see [] .
The python to start a HTTP server is at $CODEROOT\pp3e\Internet\Web\webserver.py
#########################################################################
# implement HTTP web server in Python which knows how to serve HTML
# pages and run server side CGI scripts; serves files/scripts from
# the current working dir and port 80, unless command-line args;
# python scripts must be stored in webdir\cgi-bin or webdir\htbin;
# more than one of these may be running on the same machine to serve
# from different directories, as long as they listen on different ports;
#########################################################################
webdir = '.' # where your html files and cgi-bin script directory live
port = 80 # http://servername/ if 80, else use http://servername:xxxx/
import os, sys
from BaseHTTPServer import HTTPServer
from CGIHTTPServer import CGIHTTPRequestHandler
if len(sys.argv) > 1: webdir = sys.argv[1] # command-line args
if len(sys.argv) > 2: port = int(sys.argv[2]) # else dafault ., 80
print 'webdir "%s", port %s' % (webdir, port)
# hack for Windows: os.environ not propogated
# to subprocess by os.popen2, force in-process
if sys.platform[:3] == 'win':
CGIHTTPRequestHandler.have_popen2 = False
CGIHTTPRequestHandler.have_popen3 = False # emulate path after fork
sys.path.append('cgi-bin') # else only adds my dir
os.chdir(webdir) # run in html root dir
srvraddr = ("", port) # my hostname, portnumber
srvrobj = HTTPServer(srvraddr, CGIHTTPRequestHandler)
srvrobj.serve_forever() # serve clients till exit
My environment:
Python 2.7
Windows 7 x64
Probably because the angle brackets in <type 'str'> are causing the output to be treated as HTML.
Try changing:
print '<p>[%s]'%type('apple') # ★ I add this line
into:
hstr = "%s"%type('apple')
hstr = hstr.replace('<','<').replace('>','>')
print '<p>[%s]'%hstr
Is there any way to start up the Python interpreter from within a script , in a manner similar to just using python -i so that the objects/namespace, etc. from the current script are retained? The reason for not using python -i is that the script initializes a connection to an XML-RPC server, and I need to be able to stop the entire program if there's an error. I can't loop until there's valid input because apparently, I can't do something like this:
#!/usr/bin/python -i
# -*- coding: utf-8 -*-
import xmlrpclib
# Create an object to represent our server.
server_url = str(raw_input("Server: "))
while not server = xmlrpclib.Server(server_url):
print 'Unable to connect to server. Please try again'
else:
print 'Xmlrpclib.Server object `__main__.server\' of URL `', server_url, "' created"
break
# Python interpreter starts...
because:
% chmod u+x ./rpcclient.py
% ./rpclient.py
Traceback (most recent call last):
File "./rpcclient.py", line 8
while not server = xmlrpclib.Server(server_url):
^
SyntaxError: invalid syntax
>>>
Unfortunately, python -i starts the interpreter just after it prints out the traceback, so I somehow have to call the interactive interpreter - replacing the execution of the script so it retains the server connection - from within the script
Have you tried reading the error message? :)
= is assignment, you want the comparison operator == instead.
Well, I finally got it to work.
Basically, I put the entire try/except/else clause in a while True: loop, with the else suite being a break statement and the end of the except suite being a continue statement. The result is that it now continually loops if the user puts in an address that doesn't have a fully compliant XML-RPC2 server listening. Here's how it turned out:
#!/usr/bin/python -i
# -*- coding: utf-8 -*-
import xmlrpclib, socket
from sys import exit
# Create an object to represent our server.
#server = xmlrpclib.Server(server_url) and print 'Xmlrpclib.Server object `__main__.server\' of URL `', server_url, "' created"
server_url = str(raw_input("Server: "))
server = xmlrpclib.ServerProxy(server_url)
while True:
try:
server.system.listMethods()
except xmlrpclib.ProtocolError, socket.error:
print 'Unable to connect to server. Please try again'
server_url = str(raw_input("Server: "))
server = xmlrpclib.ServerProxy(server_url)
continue
except EOFError:
exit(1)
else:
break
print 'Xmlrpclib.Server object `__main__.server\' of URL `', server_url, "' created"
# Python interpreter starts...
Thank you very much!
...and I have to wait another day to accept this...