jython or bash script to call and execute/open a URL

Anyone have a script or can help with creating a jython script that can call and execute a particular URL.

The URL is of the following form, where hostname and port (WCInboundDefault) would be relative to the environment being run (thus unknown element) and the remaining part is known (i.e, /content/cache.jsp?action=clearAll)

The script would also need to run on all servers in cell/cluster, so establishing how many JVMs there are is also a requirement (I am thinking perhaps a count = len(...) but not sure
how)

Also, I think I know how to write jython to output the hostname (from bootstrap address or import sys, socket -> host = socket.gethostname()) and the port (from the endpoints/serverEntry) but how would I extract the relevant host/port values and construct the URL below https://<hostname>:port/abc/content/cache.jsp?action=clearAll
 
Finally, I am trying to open/execute the URL by using (import urllib) but having trouble.
amughal7Asked:
Who is Participating?
 
HonorGodConnect With a Mentor Software EngineerCommented:
The error message is correct.  The urllib modules doesn't have a method / attribute named "open", it's urlopen...

urllib.urlopen( 'http://www.google.com' )
0
 
HonorGodSoftware EngineerCommented:
What version of WebSphere is being used?
- What's displayed in the "Installed Product" section of the versionInfo command script?

What operating system is being used?

> I am trying to open/execute the URL by using (import urllib) but having trouble.

What kind of trouble?

I know that the urllib module exists on version 6.1 and beyond.
0
 
amughal7Author Commented:
Hi Honor,

IBM WAS ND 7.0.0.15 on RedHat Linux

Well I have imported urllib but can't see what the code would be to run/execute a URL in jython (I can see it in python, like return urllib.urlopen('http://<host>:port/...')

Also, trying to work out how the dynamic url would be created.
I've tried using string manipulation and regular expression but its not happening.

Thanks in advance.
0
The Lifecycle Approach to Managing Security Policy

Managing application connectivity and security policies can be achieved more effectively when following a framework that automates repeatable processes and ensures that the right activities are performed in the right order.

 
HonorGodSoftware EngineerCommented:
> urllib but can't see what the code would be to run/execute a URL in jython

Something like this perhaps?

wsadmin>import urllib
wsadmin>rsp = urllib.urlopen( 'http://www.google.com' )
wsadmin>data = rsp.read()
wsadmin>len( data )
11054
wsadmin>print data[ :256 ]
<!doctype html><html itemscope="itemscope" itemtype="http://schema.org/WebPage"><head><meta content="Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking
wsadmin>

Open in new window

0
 
HonorGodSoftware EngineerCommented:
Since the result is likely to be HTML, you have to decide how you are going to deal with it.
0
 
amughal7Author Commented:
Thanks Honor, that is interesting althou I get an error:

wsadmin>import urllib
wsadmin>rsp = urllib.urlopen( 'http://www.google.com' )
Failed to get environment, environ will be empty: (0, 'Failed to execute command ([\'sh\', \'-c\', \'env\']): java.io.IOException: Cannot ru
n program "sh": CreateProcess error=2, The system cannot find the file specified.')

Also, I don't need/want to deal with the html or manipulate the output per se - I just want to be able to execute the URL as that URL will clear cache and that is what I require using jython.

Many Thanks.
0
 
amughal7Author Commented:
And the script needs to establishing how many JVMs there are in the cell and then run the URL on all servers.

Furthermore, how would I extract the relevant host/port values from each server and construct the complete URL below https://<hostname>:port/abc/content/cache.jsp?action=clearAll
0
 
HonorGodSoftware EngineerCommented:
> Failed to get environment, environ will be empty: ...

What OS are you using?  Is it something like Windows 2003 server?

Unfortunately, the version of Jython that is provided with the WebSphere product is very, very old:

wsadmin>print sys.version
2.1
wsadmin>print sys.version_info
(2, 1, 0, 'final', 0)
wsadmin>

Open in new window


and doesn't include the "fixes" that allow Jython to accurately determine the OS type.
So, instead of using "cmd.exe" to execute an operating system command, it is trying to execute: "sh" instead (which works in an *ix environment).

Here are the options that I can think of:

- You can download, and install the latest stable version of Jython, and have wsadmin use it, instead.

For example:
wsadmin>print sys.version
2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:48:36)
[IBM J9 VM (IBM Corporation)]
wsadmin>print sys.version_info
(2, 5, 3, 'final', 0)
wsadmin>

Open in new window


Let me know if you are interested in doing this.
Please note, however, that doing this is not supported by IBM technical support, and if you need to contact them with any Jython or scripting issues, they will ask you to "recreate the error" using the version of Jython provided with the product (i.e., version 2.1).

You could have your Jython script execute using the latest stable version of Jython without wsadmin.  For example:

Jython 2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:48:36)
[IBM J9 VM (IBM Corporation)] on java1.6.0
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib2
>>> rsp = urllib2.urlopen( 'http://www.google.com' )
>>> data = rsp.read()
>>> len( data )
11010
>>> print data[ :256 ]
<!doctype html><html itemscope="itemscope" itemtype="http://schema.org/WebPage"><head><meta content="Search the world's informat
ion, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking

>>>

Open in new window


What would you like to do?
0
 
amughal7Author Commented:
Thanks for that, I saw urllib2 before but I am going to have to stick with 2.1 version provided as the client I am helping will also be using Jython provided with the product and will not change.

Are you able to create what I am after ?
0
 
HonorGodSoftware EngineerCommented:
Again, I have to ask, what Operating System (OS) is being used?
0
 
amughal7Author Commented:
Hi Honor, I did mention it above - the OS is Linux Redhat.
0
 
HonorGodSoftware EngineerCommented:
sorry, I missed that...  but that is very, very strange since the message shown above, i.e.,

Failed to get environment, environ will be empty: (0, 'Failed to execute command ([\'sh\', \'-c\', \'env\']): java.io.IOException: Cannot ru
n program "sh": CreateProcess error=2, The system cannot find the file specified.')

Open in new window


Is generally caused when the Operating System type is incorrectly determined.

What is the output of the following (from a Linux command prompt)?

# which sh

I get:

/bin/sh

Also, what is the result of this from a wsadmin interactive session?

import os
os.system( 'pwd' )

Do you get the name of the current directory (followed by 0 on the next line) like I do?
Or do you get that same "Failed to get environment..." error?
0
 
amughal7Author Commented:
Sorry my mistake, I ran the command in Windows 7, although I need the script for linux - jumping from one wsadmin to another.  

in Linux I get:
WASX7015E: Exception running command: "rsp = urllib.open('http://www.google.com')"; exception information: com.ibm.bsf.BSFException
File "<input>", line 1 in ?
AttributeError: module 'urllib' has no attribute 'open'

output from linux is:
/bin/sh

wsadmin>print sys.version
2.1
wsadmin>print sys.version_info
(2, 1, 0, 'final',0)

and import os
os.system( 'pwd' ) does give the profile dir correctly followed by 0
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.