1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#!/usr/bin/env python
import alarmdecoder
import sys, select
import termios, tty
import time
def main():
if len(sys.argv) != 5:
print "Syntax: {0} [host:port] [ca cert] [client cert] [client key]\r".format(sys.argv[0])
return 1
host, port = sys.argv[1].split(':')
ca_cert = sys.argv[2]
client_cert = sys.argv[3]
client_key = sys.argv[4]
running = True
old_term_settings = termios.tcgetattr(sys.stdin.fileno())
tty.setraw(sys.stdin.fileno())
try:
print "Opening connection to {0}:{1}\r".format(host, port)
dev = alarmdecoder.devices.SocketDevice(interface=(host, int(port)))
dev.ssl = True
dev.ssl_certificate = client_cert
dev.ssl_key = client_key
dev.ssl_ca = ca_cert
dev.open(no_reader_thread=True)
dev.write("\r") # HACK: Prime the pump. This likely has to do with the SSL handshake
# not being completed when we get down to the select.
while running:
ifh, ofh, efh = select.select([sys.stdin, dev._device], [], [], 0)
for h in ifh:
if h == sys.stdin:
data = h.read(1)
# Break out if we get a CTRL-C
if data == "\x03":
print "Exiting..\r"
running = False
break
else:
dev.write(data)
else:
data = h.read(100)
sys.stdout.write(data)
sys.stdout.flush()
dev.close()
print "Connection closed.\r"
finally:
termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, old_term_settings)
if __name__ == '__main__':
main()
|