Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added HTTPAdapter to force XMLRPC request to go inside the TLS channel #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 77 additions & 27 deletions bmc_bladelogic/getUsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,43 @@
import requests
import argparse
import xml.etree.ElementTree as ET
from requests.packages.urllib3 import PoolManager, HTTPConnectionPool
#If you encounter problems with import, try to comment the previous line and use the following one instead
#from urllib3 import PoolManager, HTTPConnectionPool, connectionpool
try:
from http.client import HTTPConnection
except ImportError:
from httplib import HTTPConnection

import httplib
from requests.packages.urllib3 import PoolManager
from requests.packages.urllib3.connection import HTTPConnection
from requests.packages.urllib3.connectionpool import HTTPConnectionPool
from requests.adapters import HTTPAdapter

class MyHTTPConnection(HTTPConnection):
def __init__(self, unix_socket_url, timeout=60):
HTTPConnection.__init__(self, HOST, timeout=timeout)
self.unix_socket_url = unix_socket_url
self.timeout = timeout

def connect(self):
self.sock = wrappedSocket
if self._tunnel_host:
self._tunnel()

requests.packages.urllib3.connectionpool.HTTPConnection = MyHTTPConnection
#If you used the alternative import, comment the previous line and uncomment the following instead
#connectionpool.HTTPConnection = MyHTTPConnection

class MyHTTPConnectionPool(HTTPConnectionPool):
def __init__(self, socket_path, timeout=60):
HTTPConnectionPool.__init__(self, HOST, timeout=timeout)
self.socket_path = socket_path
self.timeout = timeout

def _new_conn(self):
return MyHTTPConnection(self.socket_path, self.timeout)


class MyAdapter(HTTPAdapter):
def __init__(self, timeout=60):
super(MyAdapter, self).__init__()
self.timeout = timeout

def get_connection(self, socket_path, proxies=None):
return MyHTTPConnectionPool(socket_path, self.timeout)

def request_url(self, request, proxies):
return request.path_url


def optParser():
parser = argparse.ArgumentParser(description="Retrieving system users with BMC BladeLogic Server Automation RSCD agent")
Expand All @@ -43,6 +63,8 @@ def optParser():

closeAsset="""<?xml version="1.0" encoding="UTF-8"?><methodCall><methodName>DAAL.assetStreamClose</methodName><params><param><value><struct><member><name>streamID</name><value><struct><member><name>sessionId</name><value><i4>2</i4></value></member></struct></value></member></struct></value></param></params></methodCall>"""

getHostOverview="""<?xml version="1.0" encoding="UTF-8"?><methodCall><methodName>RemoteServer.getHostOverview</methodName></methodCall>"""

options=optParser()
PORT=options.port
HOST=options.host
Expand All @@ -52,40 +74,66 @@ def optParser():

# Initial packet which will trigger XMLRPC communication
sock.sendall("TLSRPC")

# Create TLS tunnel
wrappedSocket = ssl.wrap_socket(sock)

# Use our existing socket for all future HTTP requests
adapter = MyAdapter()
s = requests.session()
s.mount("http://", adapter)
#s.mount("https://", adapter)

print "Sending intro..."
r=requests.post('http://'+HOST+':'+str(PORT)+'/xmlrpc',data=init)
r=s.post('http://'+HOST+':'+str(PORT)+'/xmlrpc',data=init)
#print r.status_code
r.content
#print r.content
print "Getting version..."
r=requests.post('http://'+HOST+':'+str(PORT)+'/xmlrpc',data=getVersion)
r=s.post('http://'+HOST+':'+str(PORT)+'/xmlrpc',data=getVersion)
#print r.status_code
#print r.content
rootVersion = ET.fromstring(r.content)
print "========================="
print "Major version: " + rootVersion[0][0][0][0][0][1].text
print "Minor version: " + rootVersion[0][0][0][0][1][1].text
print "Patch version: " + rootVersion[0][0][0][0][2][1].text
print "Major version : " + rootVersion[0][0][0][0][0][1].text
print "Minor version : " + rootVersion[0][0][0][0][1][1].text
print "Patch version : " + rootVersion[0][0][0][0][2][1].text
print "Platform version: " + rootVersion[0][0][0][0][3][1].text
print "=========================\n"

print "Getting host overview..."
r=s.post('http://'+HOST+':'+str(PORT)+'/xmlrpc',data=getHostOverview)
#print r.status_code
#print r.content
rootOverview = ET.fromstring(r.content)
print "=================================================="
print "Agent instal dir: " + rootOverview[0][0][0][0][ 0][1].text
print "Licensed? : " + ("false" if (int(rootOverview[0][0][0][0][1][1][0].text) == 0) else "true")
print "Repeater? : " + ("false" if (int(rootOverview[0][0][0][0][11][1][0].text) == 0) else "true")
print "Hostname : " + rootOverview[0][0][0][0][ 5][1].text
print "Netmask : " + rootOverview[0][0][0][0][12][1].text
print "CPU architecture: " + rootOverview[0][0][0][0][ 9][1].text
print "Platform (OS) : " + rootOverview[0][0][0][0][13][1].text
print "OS version : " + rootOverview[0][0][0][0][14][1].text
print "OS architecture : " + rootOverview[0][0][0][0][ 2][1].text
print "OS release : " + rootOverview[0][0][0][0][10][1].text
print "Patch level : " + rootOverview[0][0][0][0][ 6][1].text
print "==================================================\n"

print "Sending request for users...\n"
r=requests.post('http://'+HOST+':'+str(PORT)+'/xmlrpc',data=getUsers)
r=s.post('http://'+HOST+':'+str(PORT)+'/xmlrpc',data=getUsers)
#print r.status_code
r.content
r=requests.post('http://'+HOST+':'+str(PORT)+'/xmlrpc',data=getNext)
#print r.content
r=s.post('http://'+HOST+':'+str(PORT)+'/xmlrpc',data=getNext)
#print r.status_code
with open("./users.xml", "w") as text_file:
text_file.write(r.content)

r=requests.post('http://'+HOST+':'+str(PORT)+'/xmlrpc',data=getNext)
r=s.post('http://'+HOST+':'+str(PORT)+'/xmlrpc',data=getNext)
#print r.status_code
r.content
r=requests.post('http://'+HOST+':'+str(PORT)+'/xmlrpc',data=closeAsset)
#print r.content
r=s.post('http://'+HOST+':'+str(PORT)+'/xmlrpc',data=closeAsset)
#print r.status_code
r.content

#print r.content

# Parsing the response
# If parsing does not work correctly, the users' information still can be found in the saved users.xml file
Expand All @@ -101,6 +149,8 @@ def optParser():
count+=1

print "Number of users found: " + str(count) + "\n"
#raw_input("Press [Enter] to print users...")


for i in range(0,count):
print "User " + str(i) + ": " + root[0][0][0][0][0][1][0][0][i][0][0][1][0][2][1].text + "\n........................"
Expand Down