lsd.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals, print_function
  3. def main():
  4. import frida
  5. from frida.application import ConsoleApplication
  6. class LSDApplication(ConsoleApplication):
  7. def _usage(self):
  8. return "usage: %prog [options]"
  9. def _start(self):
  10. try:
  11. devices = frida.enumerate_devices()
  12. except Exception as e:
  13. self._update_status("Failed to enumerate devices: %s" % e)
  14. self._exit(1)
  15. return
  16. id_column_width = max(map(lambda device: len(device.id), devices))
  17. type_column_width = max(map(lambda device: len(device.type), devices))
  18. name_column_width = max(map(lambda device: len(device.name), devices))
  19. header_format = "%-" + str(id_column_width) + "s " + \
  20. "%-" + str(type_column_width) + "s " + \
  21. "%-" + str(name_column_width) + "s"
  22. self._print(header_format % ("Id", "Type", "Name"))
  23. self._print("%s %s %s" % (id_column_width * "-", type_column_width * "-", name_column_width * "-"))
  24. line_format = "%-" + str(id_column_width) + "s " + \
  25. "%-" + str(type_column_width) + "s " + \
  26. "%-" + str(name_column_width) + "s"
  27. for device in sorted(devices, key=cmp_to_key(compare_devices)):
  28. self._print(line_format % (device.id, device.type, device.name))
  29. self._exit(0)
  30. def compare_devices(a, b):
  31. a_score = score(a)
  32. b_score = score(b)
  33. if a_score == b_score:
  34. if a.name > b.name:
  35. return 1
  36. elif a.name < b.name:
  37. return -1
  38. else:
  39. return 0
  40. else:
  41. if a_score > b_score:
  42. return -1
  43. elif a_score < b_score:
  44. return 1
  45. else:
  46. return 0
  47. def score(device):
  48. type = device.type
  49. if type == 'local':
  50. return 3
  51. elif type == 'tether':
  52. return 2
  53. else:
  54. return 1
  55. def cmp_to_key(mycmp):
  56. "Convert a cmp= function into a key= function"
  57. class K:
  58. def __init__(self, obj, *args):
  59. self.obj = obj
  60. def __lt__(self, other):
  61. return mycmp(self.obj, other.obj) < 0
  62. def __gt__(self, other):
  63. return mycmp(self.obj, other.obj) > 0
  64. def __eq__(self, other):
  65. return mycmp(self.obj, other.obj) == 0
  66. def __le__(self, other):
  67. return mycmp(self.obj, other.obj) <= 0
  68. def __ge__(self, other):
  69. return mycmp(self.obj, other.obj) >= 0
  70. def __ne__(self, other):
  71. return mycmp(self.obj, other.obj) != 0
  72. return K
  73. device = LSDApplication()
  74. device.run()
  75. if __name__ == '__main__':
  76. main()