remotehost.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Host class
  2. # Copyright (c) 2016, Qualcomm Atheros, Inc.
  3. #
  4. # This software may be distributed under the terms of the BSD license.
  5. # See README for more details.
  6. import logging
  7. import subprocess
  8. import threading
  9. logger = logging.getLogger()
  10. def execute_thread(command, reply):
  11. try:
  12. status = 0;
  13. buf = subprocess.check_output(command, stderr=subprocess.STDOUT)
  14. except subprocess.CalledProcessError as e:
  15. status = e.returncode
  16. buf = e.output
  17. cmd = ""
  18. for c in command:
  19. cmd = cmd + " " + c
  20. logger.debug("thread cmd: " + cmd)
  21. logger.debug("thread exit status: " + str(status))
  22. logger.debug("thread exit buf: " + str(buf))
  23. reply.append(status)
  24. reply.append(buf)
  25. class Host():
  26. def __init__(self, host=None, ifname=None, port=None, name="", user="root"):
  27. self.host = host
  28. self.name = name
  29. self.user = user
  30. self.ifname = ifname
  31. self.port = port
  32. if self.name == "" and host != None:
  33. self.name = host
  34. def local_execute(self, command):
  35. logger.debug("execute: " + command)
  36. words = command.split()
  37. cmd = []
  38. for word in words:
  39. cmd.append(word)
  40. try:
  41. status = 0;
  42. buf = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
  43. except subprocess.CalledProcessError as e:
  44. status = e.returncode
  45. buf = e.output
  46. logger.debug("status: " + str(status))
  47. logger.debug("buf: " + str(buf))
  48. return status, buf
  49. def execute(self, command):
  50. if self.host is None:
  51. return self.local_execute(command)
  52. cmd = ["ssh", self.user + "@" + self.host, command]
  53. _cmd = self.name + " execute: "
  54. for c in cmd:
  55. _cmd = _cmd + " " + c
  56. logger.debug(_cmd)
  57. try:
  58. status = 0
  59. buf = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
  60. except subprocess.CalledProcessError as e:
  61. status = e.returncode
  62. buf = e.output
  63. logger.debug(self.name + " status: " + str(status))
  64. logger.debug(self.name + " buf: " + str(buf))
  65. return status, buf
  66. # async execute
  67. def execute_run(self, command, res):
  68. if self.host is None:
  69. cmd = [command]
  70. else:
  71. cmd = ["ssh", self.user + "@" + self.host, command]
  72. _cmd = self.name + " execute_run: "
  73. for c in cmd:
  74. _cmd = _cmd + " " + c
  75. logger.debug(_cmd)
  76. t = threading.Thread(target = execute_thread, args=(cmd, res))
  77. t.start()
  78. return t
  79. def wait_execute_complete(self, t, wait=None):
  80. if wait == None:
  81. wait_str = "infinite"
  82. else:
  83. wait_str = str(wait) + "s"
  84. logger.debug(self.name + " wait_execute_complete(" + wait_str + "): ")
  85. if t.isAlive():
  86. t.join(wait)