Skip to content

Instantly share code, notes, and snippets.

@devdave
Created December 24, 2012 23:53
Show Gist options
  • Save devdave/4371070 to your computer and use it in GitHub Desktop.
Save devdave/4371070 to your computer and use it in GitHub Desktop.
Setup a PUB/SUB 0MQ client/server over ssh.
"""
Bridge test client:
So this one's going to be a doozy.
Connect to target via SSH and then connect to target:localhost:8283(DAVE) and listen
for the time messages.
"""
import zmq
from zmq import ssh
import paramiko #Ensure paramiko is installed ( and therefore pyCrypto as well )
class Client(object):
def __init__(self, host = None, port = None, ssh_server = None):
self.host = host or "127.0.0.1"
self.port = port or "3283"
self.conn = "tcp://%s:%s" % (self.host, self.port)
#Kick off 0MQ build up
self.ctx = zmq.Context()
self.s = self.ctx.socket(zmq.SUB)
self.s.setsockopt(zmq.SUBSCRIBE,'') #For now, subscribe to everything
#To lazy to setup a new ssh pub/priv key so setting a throwaway password
self.tunnel = ssh.tunnel_connection(self.s, self.conn, ssh_server, password = "password")
def receive(self):
return self.s.recv()
def run(self):
while True:
try:
msg = self.receive()
print msg
except KeyboardInterrupt:
print "Interupt"
break
def main():
Client(None, None, "[email protected]").run()
if __name__ == '__main__': main()
"""
Bridge Test Server
Goal:
Every N seconds, publish the time & interval to localhost:3283(DAVE)
terminate on ctrl+c
"""
from time import sleep
from time import ctime
import zmq
class Server(object):
def __init__(self, host = None, port = None):
self.host = host or "127.0.0.1"
self.port = port or "3283"
self.conn = "tcp://%s:%s" % (self.host, self.port)
#Kick off 0MQ build up
self.ctx = zmq.Context()
self.s = self.ctx.socket(zmq.PUB)
self.s.bind(self.conn)
def send(self, msg):
"""
Ecapsulate send in case I want/need to overload it
"""
return self.s.send(msg)
def run(self):
while True:
try:
msg = "%s" % ctime()
self.send(msg)
print msg
except KeyboardInterrupt:
print "Interupted!"
break
sleep(2)
def main():
#Don't need a reference, so just instantiate and let it block.
Server().run()
if __name__ == '__main__': main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment