gtk3-listdomains.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import gi
  2. gi.require_version('Gtk', '3.0')
  3. from gi.repository import GLib, Gtk, GObject
  4. import json
  5. import re
  6. import spur
  7. import threading
  8. import time
  9. fp = open('creds.json', 'r')
  10. creds = json.load(fp)
  11. def ssh_command(command, sudo=False):
  12. shell = spur.SshShell(
  13. hostname=creds['host'],
  14. username=creds['user'],
  15. private_key_file=creds['ssh_key_path'])
  16. with shell:
  17. command_bits = command.split(" ")
  18. if sudo:
  19. command_bits.insert(0, "sudo")
  20. process = shell.spawn(command_bits)
  21. if sudo:
  22. process.stdin_write(creds['password'])
  23. result = process.wait_for_result()
  24. return result.output.decode()
  25. #
  26. # def get_check_domain_command(domain):
  27. # return "sudo openssl x509 -text -in /etc/letsencrypt/live/" + domain + "/fullchain.pem"
  28. #
  29. # def join_commands(commands):
  30. # return " && ".join(commands)
  31. class EntryWindow(Gtk.Window):
  32. def __init__(self):
  33. Gtk.Window.__init__(self, title="Entry Demo")
  34. self.set_size_request(200, 100)
  35. self.timeout_id = None
  36. vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
  37. self.add(vbox)
  38. self.progressbar = Gtk.ProgressBar(show_text=True)
  39. # self.progressbar.set_fraction(0.0)
  40. vbox.pack_start(self.progressbar, True, True, 0)
  41. hbox = Gtk.Box(spacing=6)
  42. vbox.add(hbox)
  43. self.entry_passphrase = Gtk.Entry()
  44. # https://developer.gnome.org/gtk3/stable/GtkEntry.html#gtk-entry-set-invisible-char
  45. self.entry_passphrase.set_visibility(False)
  46. # self.entry_passphrase.set_text("Enter SSH key passphrase")
  47. hbox.pack_start(self.entry_passphrase, True, True, 0)
  48. self.entry_password = Gtk.Entry()
  49. self.entry_password.set_visibility(False)
  50. # self.entry_password.set_text("Enter sudo user password")
  51. hbox.pack_start(self.entry_password, True, True, 0)
  52. self.button = Gtk.Button(label="Click Here")
  53. self.button.connect("clicked", self.on_button_clicked)
  54. hbox.pack_start(self.button, True, True, 0)
  55. def update_progess(self, domain):
  56. self.progressbar.pulse()
  57. self.progressbar.set_text('Done: ' + domain)
  58. return False
  59. def example_target(self):
  60. for d in self.domains:
  61. GLib.idle_add(self.get_https_subdomains_for_domain, d)
  62. time.sleep(0.3)
  63. def on_button_clicked(self, widget):
  64. decoded = ssh_command("ls /etc/letsencrypt/live", True)
  65. domains = decoded.split("\n")
  66. domains.pop()
  67. self.domains = domains
  68. self.num_domains = len(domains)
  69. self.num_done = 0
  70. thread = threading.Thread(target=self.example_target)
  71. thread.daemon = True
  72. thread.start()
  73. # subdomains = [self.get_https_subdomains_for_domain(d) for d in domains]
  74. # subdomains_dict = dict(zip(domains, subdomains))
  75. # print(subdomains_dict)
  76. def get_https_subdomains_for_domain(self, domain):
  77. print(domain)
  78. p = re.compile('DNS:([0-9a-z-.]+)')
  79. cert_data = ssh_command("sudo openssl x509 -text -in /etc/letsencrypt/live/" + domain + "/fullchain.pem", True)
  80. self.num_done += 1
  81. percent_done = self.num_done * 1.0 / self.num_domains
  82. # print(percent_done)
  83. self.progressbar.set_fraction(percent_done)
  84. # self.progressbar.set_text('Done: ' + domain)
  85. # print(cert_data)
  86. # return p.findall (cert_data)
  87. return False
  88. def app_main():
  89. win = EntryWindow()
  90. win.connect("delete-event", Gtk.main_quit)
  91. win.show_all()
  92. if __name__ == '__main__':
  93. import signal
  94. signal.signal(signal.SIGINT, signal.SIG_DFL)
  95. # Calling GObject.threads_init() is not needed for PyGObject 3.10.2+
  96. GObject.threads_init()
  97. app_main()
  98. Gtk.main()