How to connect to mq queue with python and bindings file? - python

I'm trying to connect with a remote MQ queue/serie and I only have a .bindings file to do it. I'm trying the python library "pymqi" but I can't connect using binding mode. Does someone knows what should I do or where should I place the file in order to use it with the library? Is there some other python solution to connect to the MQ queue?
this is a glimpse of my .bindings file:
JMSC/ClassName=com.ibm.mq.jms.MQQueueConnectionFactory
JMSC/FactoryName=com.ibm.mq.jms.MQQueueConnectionFactoryFactory
JMSC/RefAddr/0/Type=VER
JMSC/RefAddr/0/Encoding=String
JMSC/RefAddr/0/Content=7
JMSC/RefAddr/1/Type=TRAN
JMSC/RefAddr/1/Encoding=String
JMSC/RefAddr/1/Content=1
JMSC/RefAddr/2/Type=QMGR
JMSC/RefAddr/2/Encoding=String
JMSC/RefAddr/2/Content=MQFEND00
JMSC/RefAddr/3/Type=HOST
JMSC/RefAddr/3/Encoding=String
JMSC/RefAddr/3/Content=somehost
JMSC/RefAddr/4/Type=PORT
JMSC/RefAddr/4/Encoding=String
JMSC/RefAddr/4/Content=1414
JMSC/RefAddr/5/Type=CHAN
JMSC/RefAddr/5/Encoding=String
JMSC/RefAddr/5/Content=PORTALS.MQFEND00
It has like 100 params, that are the first 6,
Thanks
update 22/05/2019:
I will add more information about what I try.
I tried to connect with bindings mode as I saw on the pymqi documentation:
qmgr = pymqi.connect('MQFEND00')
And I get this error:
MQI Error. Comp: 2, Reason 2058: FAILED: MQRC_Q_MGR_NAME_ERROR
I'm not sure if it's the queue_manager, someone knows how can I get the queue_manager from bindings file?
I've also tried to connect with host, channel and port:
qmgr = pymqi.connect(queue_manager, channel, conn_info)
And I get an error of not authorized, I think it's because this second way is to connect with the client and I would need user and password which I haven't.

If you want to use binding mode, you should setup pymqi with server or binding parameters. You can not use binding and client mode simultaneously:
#From pymqi folder
cd ./code
./setup.py build server
I am not sure, that you can use .bindings file with pymqi without parsing it himself.

Probably I'm very late to this discussion, but:
import pymqi
queue_manager = 'MQFEND00'
channel = 'PORTALS.MQFEND00'
host = 'somehost'
port = '1414'
conn_info = '%s(%s)' % (host, port)
qmgr = pymqi.connect(queue_manager, channel, conn_info)
# other operations, see https://dsuch.github.io/pymqi/examples.html for more.
qmgr.disconnect()
You must have a MQ Client installed in the same machine that you run pymqi;
Are you using conn_info like the code snnipet?

Related

MQRC_SSL_INITIALIZATION_ERROR with PyMQI (however it connects successfully with c application amssslc)

I am using the same computer local windows 7 computer with MQ Client 9.0.4.0, when trying to connect to the server with amqssslc I can successfully connect to the QMGR (connected like so), however, when I try to connect with PyMQI, I get the following error,
MQI Error. Comp: 2, Reason 2393: FAILED: MQRC_SSL_INITIALIZATION_ERROR
The code that I am using is the following,
import pymqi
import logging
import sys, codecs, locale
logging.basicConfig(level=logging.INFO)
queue_manager = 'QMGR'
channel = 'channel'
host = 'server.com'
port = '1414'
conn_info = '%s(%s)' % (host, port)
ssl_cipher_spec = str.encode('TLS_RSA_WITH_AES_256_CBC_SHA256')
key_repo_location = str.encode('T:/Desktop/certificates/key')
message = 'TEST Message'
channel = str.encode(channel)
host = str.encode(host)
conn_info = str.encode(conn_info)
cd = pymqi.CD(Version=pymqi.CMQXC.MQCD_VERSION_11)
cd.ChannelName = channel
cd.ConnectionName = conn_info
cd.ChannelType = pymqi.CMQC.MQCHT_CLNTCONN
cd.TransportType = pymqi.CMQC.MQXPT_TCP
cd.SSLCipherSpec = ssl_cipher_spec
cd.CertificateLabel = 'edgar'.encode()
sco = pymqi.SCO()
sco.KeyRepository = key_repo_location
qmgr = pymqi.QueueManager(None)
qmgr.connect_with_options(queue_manager, cd, sco)
However when using amqssslc, that comes when installing the MQ Client on my machine, it does work without any errors and connects successfully.
The error from the AMQERR01 log file says the following,
AMQ9716E: Remote SSL certificate revocation status check failed for channel
'channel_name'.
EXPLANATION:
IBM MQ failed to determine the revocation status of the remote SSL certificate
for one of the following reasons:
(a) The channel was unable to contact any of the CRL servers or OCSP responders
for the certificate.
(b) None of the OCSP responders contacted knows the revocation status of the
certificate.
(c) An OCSP response was received, but the digital signature of the response
could not be verified.
I can't change my mqclient.ini config file, since it is locked by not having admin rights (company policies). What I find weird, is that amqssslc works, when they are both using the same mqclient file. I have also tried setting up the path of the MQCLTNFC to another folder including a different config file without success.
Any help would be truly appreciated!

Get command result from SSH/Telnet via another SSH jump server in Python

I am writing a Python code to SSH or Telnet remote hosts, execute some commands and get the result output. Here we have a jump server, so the code must be "connected" with this server and from it, Telnet or SSH the remote hosts.
All my approaches work fine within the jump server, for example I can get the output of commands inside it. The problem is when I try to remote connect to hosts from it.
import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('IP', 22, username="user", password="pass")
stdin, stdout, stderr = client.exec_command("command")
for line in stdout:
print('... ' + line.strip('\n'))
client.close()
Using the library jumpssh get same results, and I am not will able to Telnet hosts. I tried the follow approach, but get the error
Administratively prohibited.
from jumpssh import SSHSession
gateway_session = SSHSession('jumpserver','user', password='pass').open()
remote_session = gateway_session.get_remote_session('IP',password='pass')
print(gateway_session.get_cmd_output('command'))
In the last company i worked we had a license from an SSH client that supports Python scripts, and worked fine in a more "textual" treatment.
There is any way of accomplish same task natively in Python?
SSHSession is trying to open direct-tcpip port forwarding channel over the gateway_session.
"administratively prohibited" is OpenSSH sshd server message indicating that direct-tcpip port forwarding is disabled.
To enable port forwarding, set AllowTcpForwarding and DisableForwarding directives in sshd_config appropriately.
If you cannot enable the port forwarding on the server, you cannot use jumpssh library.
If you have a shell access to the server, you can use ProxyCommand-like approach instead.
See Paramiko: nest ssh session to another machine while preserving paramiko functionality (ProxyCommand).

Pairing bluetooth devices with Passkey/Password in python - RFCOMM (Linux)

I am working on a Python script to search for bluetooth devices and connect them using RFCOMM. This devices has Passkey/Password. I am using PyBlueZ and, as far as I know, this library cannot handle Passkey/Password connections (Python PyBluez connecting to passkey protected device).
I am able to discover the devices and retrieve their names and addresses:
nearby_devices = bluetooth.discover_devices(duration=4,lookup_names=True,
flush_cache=True, lookup_class=False)
But if tried to connect to a specific device using:
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.connect((addr,port))
I get an error 'Device or resource busy (16)'.
I tried some bash commands using the hcitool and bluetooth-agent, but I need to do the connection programmatically. I was able to connect to my device using the steps described here: How to pair a bluetooth device from command line on Linux.
I want to ask if someone has connected to a bluetooth device with Passkey/Password using Python. I am thinking about to use the bash commands in Python using subprocess.call(), but I am not sure if it is a good idea.
Thanks for any help.
Finally I am able to connect to a device using PyBlueZ. I hope this answer will help others in the future. I tried the following:
First, import the modules and discover the devices.
import bluetooth, subprocess
nearby_devices = bluetooth.discover_devices(duration=4,lookup_names=True,
flush_cache=True, lookup_class=False)
When you discover the device you want to connect, you need to know port, the address and passkey. With that information do the next:
name = name # Device name
addr = addr # Device Address
port = 1 # RFCOMM port
passkey = "1111" # passkey of the device you want to connect
# kill any "bluetooth-agent" process that is already running
subprocess.call("kill -9 `pidof bluetooth-agent`",shell=True)
# Start a new "bluetooth-agent" process where XXXX is the passkey
status = subprocess.call("bluetooth-agent " + passkey + " &",shell=True)
# Now, connect in the same way as always with PyBlueZ
try:
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.connect((addr,port))
except bluetooth.btcommon.BluetoothError as err:
# Error handler
pass
Now, you are connected!! You can use your socket for the task you need:
s.recv(1024) # Buffer size
s.send("Hello World!")
Official PyBlueZ documentation is available here
Is there a way to connect two phones via Bluetooth , the script should be running on a Linux host. Any suggestions of using pybluez or any other APIs?
I have seen some examples where a Linux host is used as Client and is connect a phone (which is a server), but here I'm want to use Linux host as just a device to run the script and make two phones connect via Bluetooth.

Problems in connecting to MusicBrainz database using psycopg2

I am trying to connect to the MusicBrainz database using the psycopg2 python's module. I have followed the instructions presented on http://musicbrainz.org/doc/MusicBrainz_Server/Setup, but I cannot succeed in connecting. In particular I am using the following little script:
import psycopg2
conn = psycopg2.connect( database = 'musicbrainz_db', user= 'musicbrainz', password = 'musicbrainz', port = 5000, host='10.16.65.250')
print "Connection Estabilished"
The problem is that when I launch it, it never reaches the print statement, and the console (I'm on linux) is block indefinitely. It does not even catches the ctrl-c kill, so I have to kill python itself in another console. What can cause this?
You seem to be mistaking MusicBrainz-Server to be only the database.
What's running on port 5000 is the Web Server.
You can access http://10.16.65.250:5000 in the browser.
Postgres is also running, but listens on localhost:5432.
This works:
import psycopg2
conn = psycopg2.connect(database="musicbrainz_db",
user="musicbrainz", password="musicbrainz",
port="5432", host="localhost")
print("Connection established")
In order to make postgres listen to more than localhost you need to change listen_addresses in /etc/postgresql/9.1/main/postgres.conf and make an entry for your (client) host or network in /etc/postgresql/9.1/main/pg_hba.conf.
My VM is running in a 192.168.1.0/24 network so I set listen_addresses='*' in postgres.conf and in pg_hab.conf:
host all all 192.168.1.0/24 trust
I can now connect from my local network to the DB in the VM.
Depending on what you actually need, you might not want to connect to the MusicBrainz Server via postgres. There is a MusicBrainz web service you can access in the VM.
Example:
http://10.16.65.250:5000/ws/2/artist/c5c2ea1c-4bde-4f4d-bd0b-47b200bf99d6.
In that case you might be interested in a library to process the data:
python-musicbrainzngs.
EDIT:
You need to set musicbrainzngs.set_hostname("10.16.65.250:5000") for musicbrainzngs to connect to your local VM.

pyxmpp2 connect to openfire cannot resolve NXDOMAIN

I installed pyxmpp2 https://github.com/Jajcus/pyxmpp2to my Ubuntu machine. I also installed Openfire 3.8.1 to it. I would like to use pyxmpp2 to connect to my Openfire server within the same machine.
In the Server -> Server Manager-> Server Information in my Openfire control panel, the Server Name showed in Server Properties in the panel was mymachine and the Host Name showed in the Environment section was MyMachine.
I tried the following code:
import logging
from pyxmpp2.jid import JID
from pyxmpp2.client import Client
logging.basicConfig()
client = Client(JID("admin#mymachine"),[])
client.connect()
and got the following message:
WARNING:pyxmpp2.resolver:Could not resolve '_xmpp-client._tcp.mymachine': NXDOMAIN
Did I miss configuring something?
It looks like there are no DNS SRV records for your domain and pyxmpp2 is therefore unable to resolve them. Have a look at http://wiki.xmpp.org/web/SRV_Records on how to create them.
Basically a DNS SRV record has the form
_service._proto.name TTL class SRV priority weight port target
which could look like this example
_xmpp-client._tcp.example.net. 86400 IN SRV 5 0 5222 example.net.
Maybe pyxmpp2 also provides a way to directly specify the host used for the XMPP service. This would avoid the DNS SRV lookup.
It may be using ipv6, you can force ipv4 using u"ipv4": True and specifing the server u"server": "chat.facebook.com"
handler = MyHandler(JID(target_jid), message)
settings = XMPPSettings({
u"ipv4": True,
u"server": "chat.facebook.com",
u"password": your_password,
u"starttls": True,
u"tls_verify_peer": False,
})
client = Client(JID(your_jid), [handler], settings)
client.connect()
client.run()
The full code is located on the pyxmpp2 examples folder send_message_client.py

Categories