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.1 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. """
  2. Jeffery Russell
  3. 9-26-17
  4. """
  5. import subprocess
  6. import collections
  7. from utils 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.1 ", 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) Manage Configuration and Bash",
  85. "6) Exit"])
  86. def print_red(prt): return "\033[91m {}\033[00m" .format(prt)
  87. def sub_menu():
  88. """
  89. calls printSubMenu and then gets input from user to
  90. make appropriate function calls
  91. :return: None
  92. """
  93. print_sub_menu()
  94. i = input("Enter selection:")
  95. if i != '' and int(i) in {1, 2, 3, 4, 5, 6}:
  96. options = {1: add_host,
  97. 2: copy_ssh_key,
  98. 3: remove_host,
  99. 4: main,
  100. 5: configuration.main,
  101. 6: exit_program
  102. }
  103. options[int(i)]()
  104. else:
  105. print("Invalid selection!")
  106. sub_menu()
  107. def exit_program():
  108. """
  109. Exits the program and clears the screen
  110. :return: None
  111. """
  112. #subprocess.call(["clear"])
  113. exit()
  114. def add_host():
  115. """
  116. appends an inputted host name to servers.txt
  117. :return: None
  118. """
  119. host = input("Enter 'user@host' or -1 to exit:")
  120. if host != '-1':
  121. module.append_file(INPUT_FILE, host)
  122. def copy_ssh_key():
  123. """
  124. calls systems ssh-copy-id with host name
  125. :return: None
  126. """
  127. file = module.input_file(INPUT_FILE)
  128. cmp = []
  129. count = 1
  130. for line in file:
  131. cmp.append(Computer(line, count))
  132. count += 1
  133. menu = []
  134. for c in cmp:
  135. menu.append(str(c.menu_id) + ") " + c.host)
  136. menu.append("A) Exit")
  137. module.print_menu("Copy SSH Key", menu)
  138. host_id = input("Enter number of host to copy ssh key to:")
  139. if not (host_id == '-1' or host_id.lower() == 'a'):
  140. for c in cmp:
  141. if c.menu_id == int(host_id):
  142. subprocess.call(["ssh-copy-id", c.host])
  143. def remove_host():
  144. """
  145. Removes a host name from servers.txt
  146. :return: None
  147. """
  148. file = module.input_file(INPUT_FILE)
  149. cmp = []
  150. count = 1
  151. print(print_red("*" * len(WELCOME_MESSAGE)))
  152. for line in file:
  153. cmp.append(Computer(line, count))
  154. count += 1
  155. for c in cmp:
  156. space = " " * (len(WELCOME_MESSAGE) - 3 -
  157. len(str(c.menu_id) + ") " + c.host))
  158. print(print_red("*") + str(c.menu_id) + ") " +
  159. c.host + space + print_red("*"))
  160. print(print_red("*" * len(WELCOME_MESSAGE)))
  161. host = input("Enter number of host -1 to exit:")
  162. if host != '-1':
  163. for c in cmp:
  164. if c.menu_id == int(host):
  165. module.remove_line_from_file(INPUT_FILE, c.host)
  166. """
  167. Makes sure that other programs don't execute the main
  168. """
  169. if __name__ == '__main__':
  170. try:
  171. main()
  172. except KeyboardInterrupt:
  173. exit_program()