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.

158 lines
3.8 KiB

  1. """
  2. Author: Jason Cheung
  3. Date: 2/8/2020
  4. Description: This is a manager for easily performing SHH port forwards
  5. """
  6. import subprocess
  7. import collections
  8. from utils import module
  9. import configuration
  10. # INPUT_FILE = configuration.get_config()["portforwards"]
  11. INPUT_FILE = "./portforwards.txt"
  12. Computer = collections.namedtuple("Computer", ('user', 'user_num'))
  13. WELCOME_MESSAGE = "**************************************"
  14. def main():
  15. """
  16. This function inputs all the available hosts from a text file and
  17. prompts the user to connect to them
  18. :return:
  19. """
  20. file = module.input_file(INPUT_FILE)
  21. cmp = []
  22. count = 1
  23. for line in file:
  24. cmp.append(Computer(line, count))
  25. count += 1
  26. menu = []
  27. for c in cmp:
  28. menu.append(str(c.user_num) + ") " + "".join(c.user))
  29. menu.append("A) Exit")
  30. menu.append("B) Manager tools")
  31. module.print_menu("SSH forwarding manager V 1.0", menu)
  32. i = input("Enter Option:")
  33. if i == '' or i == 'A' or i == 'a':
  34. exit_program()
  35. elif i == 'B' or i == 'b':
  36. sub_menu()
  37. else:
  38. for c in cmp:
  39. if int(i) == c.user_num:
  40. subprocess.call(
  41. ["ssh", "-L", c.user.split(":")[2] + ":localhost:"
  42. + c.user.split(":")[2], c.user.split(":")[1]])
  43. exit_program()
  44. def print_sub_menu():
  45. """
  46. prints out a sub help menu for other options
  47. :return: None
  48. """
  49. module.print_menu("Options", ["1) Add Host",
  50. "2) Remove host name",
  51. "3) Return to ssh forwarding",
  52. "4) Manage Configuration and Bash",
  53. "5) Exit"])
  54. def sub_menu():
  55. """
  56. calls printSubMenu and then gets input from user to
  57. make appropriate function calls
  58. :return: None
  59. """
  60. print_sub_menu()
  61. i = input("Enter selection:")
  62. if i != '' and int(i) in {1, 2, 3, 4, 5}:
  63. options = {1: add_host,
  64. 2: remove_host,
  65. 3: main,
  66. 4: configuration.main,
  67. 5: exit_program
  68. }
  69. options[int(i)]()
  70. else:
  71. print("Invalid selection!")
  72. sub_menu()
  73. def print_red(prt): return "\033[91m {}\033[00m".format(prt)
  74. def exit_program():
  75. """
  76. Exits the program and clears the screen
  77. :return: None
  78. """
  79. subprocess.call(["clear"])
  80. exit()
  81. def add_host():
  82. """
  83. appends an inputted host name to servers.txt
  84. :return: None
  85. """
  86. host = input("Enter 'user@host' or -1 to exit:")
  87. if host != '-1':
  88. name = input("Name: ")
  89. port = input("Port: ")
  90. module.append_file(INPUT_FILE, name + ":" + host + ":" + port)
  91. def remove_host():
  92. """
  93. Removes a host name from servers.txt
  94. :return: None
  95. """
  96. padding_star = "*" * 5
  97. max_len = 0
  98. file = module.input_file(INPUT_FILE)
  99. for line in file:
  100. temp = len(line)
  101. if max_len < temp:
  102. max_len = temp
  103. TOP_BAR = padding_star + "*" * max_len + padding_star
  104. cmp = []
  105. count = 1
  106. print(print_red("*" * len(TOP_BAR) + "*"))
  107. for line in file:
  108. cmp.append(Computer(line, count))
  109. count += 1
  110. for c in cmp:
  111. space = " " * (len(TOP_BAR) - 3 -
  112. len(str(c.user_num) + ") " + c.user))
  113. print(print_red("*") + " " + str(c.user_num) + ") " +
  114. c.user + space + print_red("*"))
  115. print(print_red("*" * len(TOP_BAR) + '*'))
  116. host = input("Enter number of host -1 to exit:")
  117. if host != '-1':
  118. for c in cmp:
  119. if c.user_num == int(host):
  120. module.remove_line_from_file(INPUT_FILE, c.user)
  121. """
  122. Makes sure that other programs don't execute the main
  123. """
  124. if __name__ == '__main__':
  125. try:
  126. main()
  127. except KeyboardInterrupt:
  128. exit_program()