Python scripts I use to manage my ssh connections, drive mounts, and other bash related things.
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.

215 lines
5.0 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. """
  2. Jeffery Russell
  3. 9-26-17
  4. """
  5. import subprocess
  6. import collections
  7. import module
  8. import configuration
  9. import mount_ssh_drive
  10. INPUT_FILE = configuration.get_config()["servers"]
  11. Computer = collections.namedtuple("Computer", ('host', 'menu_id'))
  12. WELCOME_MESSAGE = "**************************************"
  13. def main():
  14. """
  15. This function inputs all the available hosts from a text file and
  16. prompts the user to connect to them
  17. :return:
  18. """
  19. file = module.input_file(INPUT_FILE)
  20. cmp = []
  21. count = 1
  22. for line in file:
  23. cmp.append(Computer(line, count))
  24. count += 1
  25. menu = []
  26. for c in cmp:
  27. menu.append(str(c.menu_id) + ") " + c.host)
  28. menu.append("A) Exit")
  29. menu.append("B) Manager tools")
  30. menu.append("C) Socks Tunnel")
  31. menu.append("D) SSH Drive Manager")
  32. module.print_menu("SSH manager V 1.0", menu)
  33. i = input("Enter Option:")
  34. if i == '' or i == 'A' or i == 'a':
  35. exit_program()
  36. elif i == 'b' or i == 'B':
  37. sub_menu()
  38. elif "c" == str.lower(i):
  39. socks_ssh_tunnel()
  40. elif "d" == str.lower(i):
  41. mount_ssh_drive.main()
  42. else:
  43. for c in cmp:
  44. if int(i) == c.menu_id:
  45. subprocess.call(["ssh", c.host])
  46. exit_program()
  47. def socks_ssh_tunnel():
  48. """
  49. prints user a menu to select a host to start a socks proxy with
  50. :return: None
  51. """
  52. file = module.input_file(INPUT_FILE)
  53. cmp = []
  54. count = 1
  55. for line in file:
  56. cmp.append(Computer(line, count))
  57. count += 1
  58. menu = []
  59. for c in cmp:
  60. menu.append(str(c.menu_id) + ") " + c.host)
  61. menu.append("A) Exit")
  62. menu.append("B) Main")
  63. module.print_menu("Socks Tunnel", menu)
  64. i = input("Enter option:")
  65. if i == '' or 'a' == str.lower(i):
  66. exit_program()
  67. elif 'b' == str.lower(i):
  68. main()
  69. else:
  70. for c in cmp:
  71. if int(i) == c.menu_id:
  72. print_red("Starting socks proxy on " + c.host + ":8123")
  73. subprocess.call(["ssh", "-D", "8123", "-C", "-q", "-N", c.host])
  74. exit_program()
  75. def print_sub_menu():
  76. """
  77. prints out a sub help menu for other options
  78. :return: None
  79. """
  80. module.print_menu("Options", ["1) Add Host",
  81. "2) Copy SSH key to server",
  82. "3) Remove host name",
  83. "4) Return to ssh manager",
  84. "5) Exit"])
  85. def print_red(prt): return "\033[91m {}\033[00m" .format(prt)
  86. def sub_menu():
  87. """
  88. calls printSubMenu and then gets input from user to
  89. make appropriate function calls
  90. :return: None
  91. """
  92. print_sub_menu()
  93. i = input("Enter selection:")
  94. if i != '' and int(i) in {1, 2, 3, 4, 5}:
  95. options = {1: add_host,
  96. 2: copy_ssh_key,
  97. 3: remove_host,
  98. 4: main,
  99. 5: exit_program,
  100. }
  101. options[int(i)]()
  102. else:
  103. print("Invalid selection!")
  104. sub_menu()
  105. def exit_program():
  106. """
  107. Exits the program and clears the screen
  108. :return: None
  109. """
  110. subprocess.call(["clear"])
  111. exit()
  112. def add_host():
  113. """
  114. appends an inputted host name to servers.txt
  115. :return: None
  116. """
  117. host = input("Enter 'user@host' or -1 to exit:")
  118. if host != '-1':
  119. module.append_file(INPUT_FILE, host)
  120. def copy_ssh_key():
  121. """
  122. calls systems ssh-copy-id with host name
  123. :return: None
  124. """
  125. file = module.input_file(INPUT_FILE)
  126. cmp = []
  127. count = 1
  128. for line in file:
  129. cmp.append(Computer(line, count))
  130. count += 1
  131. menu = []
  132. for c in cmp:
  133. menu.append(str(c.menu_id) + ") " + c.host)
  134. menu.append("A) Exit")
  135. module.print_menu("Copy SSH Key", menu)
  136. host_id = input("Enter number of host to copy ssh key to:")
  137. if not (host_id == '-1' or host_id.lower() == 'a'):
  138. for c in cmp:
  139. if c.menu_id == int(host_id):
  140. subprocess.call(["ssh-copy-id", c.host])
  141. def remove_host():
  142. """
  143. Removes a host name from servers.txt
  144. :return: None
  145. """
  146. file = module.input_file(INPUT_FILE)
  147. cmp = []
  148. count = 1
  149. print(print_red("*" * len(WELCOME_MESSAGE)))
  150. for line in file:
  151. cmp.append(Computer(line, count))
  152. count += 1
  153. for c in cmp:
  154. space = " " * (len(WELCOME_MESSAGE) - 3 -
  155. len(str(c.menu_id) + ") " + c.host))
  156. print(print_red("*") + str(c.menu_id) + ") " +
  157. c.host + space + print_red("*"))
  158. print(print_red("*" * len(WELCOME_MESSAGE)))
  159. host = input("Enter number of host -1 to exit:")
  160. if host != '-1':
  161. for c in cmp:
  162. if c.menu_id == int(host):
  163. module.remove_line_from_file(INPUT_FILE, c.host)
  164. """
  165. Makes sure that other programs don't execute the main
  166. """
  167. if __name__ == '__main__':
  168. try:
  169. main()
  170. except KeyboardInterrupt:
  171. exit_program()