You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.5 KiB

  1. #!/usr/bin/env python
  2. import sys, time, socket
  3. from websocket import create_connection, WebSocketTimeoutException
  4. #websocket._exceptions.WebSocketTimeoutException
  5. def checkws(ws,hostname,timeout,debug):
  6. # create websocket
  7. ws = create_connection(ws, subprotocols=["binary", "base64"])
  8. # set timeout
  9. ws.settimeout(timeout);
  10. # terminal initialization
  11. ws.send("\x0d")
  12. ws.send("\xff\xfd\x01")
  13. ws.send("\xff\xfb\x03")
  14. ws.send("\xff\xfb\x18")
  15. ws.send("\xff\xfb\x1f")
  16. ws.send("\xff\xfd\x03")
  17. ws.send("\xff\xfd\x18")
  18. ws.send("\xff\xfd\x1f")
  19. ws.send("\xff\xfe\x00")
  20. ws.send("\xff\xfc\x00")
  21. # initial receive
  22. try:
  23. result = ""
  24. result += ws.recv()
  25. except WebSocketTimeoutException:
  26. print "timeout"
  27. except socket.error:
  28. print "socket closed"
  29. sys.exit(3)
  30. if debug > 1: print result
  31. # send three carriage returns
  32. ws.send("\r\r\r")
  33. start = time.clock()
  34. duration = 0
  35. # receive result
  36. try:
  37. result = ""
  38. while True:
  39. result += ws.recv()
  40. if duration == 0 and result.count(hostname) >= 3:
  41. duration = time.clock() - start
  42. break
  43. except WebSocketTimeoutException:
  44. print "timeout"
  45. except socket.error:
  46. print "socket closed"
  47. sys.exit(3)
  48. if debug > 1: print result
  49. ws.close()
  50. # count occurences of hostname
  51. if result.count(hostname) >= 3:
  52. print "%f *sec elapsed*" % duration
  53. return 0
  54. else:
  55. print "UNUSABLE"
  56. return 2
  57. def main():
  58. if len(sys.argv) <= 4:
  59. sys.stdout.write(str(sys.argv[0]))
  60. print ": ws url, hostname, timeout and debug (e.g., \"ws://192.168.76.210:19406/websockify?token=870668d2-8855-4208-9d48-9b1a141271b2\" iosv-1 0.5 0) needed as argument! bailing out"
  61. return 1
  62. else:
  63. ws = str(sys.argv[1]).strip()
  64. hostname = str(sys.argv[2]).strip()
  65. timeout = float(sys.argv[3])
  66. debug = int(sys.argv[4])
  67. return checkws(ws,hostname,timeout,debug)
  68. if __name__ == '__main__':
  69. sys.exit(main())