?
Solved

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

Posted on 2012-09-14
15
Medium Priority
?
578 Views
Last Modified: 2014-08-08
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.
0
Comment
Question by:amughal7
  • 7
  • 6
13 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 38401638
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
 

Author Comment

by:amughal7
ID: 38406545
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
 
LVL 41

Expert Comment

by:HonorGod
ID: 38406931
> 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 41

Expert Comment

by:HonorGod
ID: 38406936
Since the result is likely to be HTML, you have to decide how you are going to deal with it.
0
 

Author Comment

by:amughal7
ID: 38407238
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
 

Author Comment

by:amughal7
ID: 38407247
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
 
LVL 41

Expert Comment

by:HonorGod
ID: 38407333
> 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
 

Author Comment

by:amughal7
ID: 38407460
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
 
LVL 41

Expert Comment

by:HonorGod
ID: 38409054
Again, I have to ask, what Operating System (OS) is being used?
0
 

Author Comment

by:amughal7
ID: 38409888
Hi Honor, I did mention it above - the OS is Linux Redhat.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 38410020
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
 

Author Comment

by:amughal7
ID: 38411644
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
 
LVL 41

Accepted Solution

by:
HonorGod earned 2000 total points
ID: 38413149
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

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Convert websphere application server default chained Certificates from 1024 to 2048 keysize or higher size and also you can change signatureAlgorithm . Please make sure Websphere Application Server fixpack 7.0.0.23 or Above. The following steps a…
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question