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.

138 lines
3.1 KiB

  1. """
  2. Jeffery Russell
  3. 9/29/17
  4. """
  5. import subprocess
  6. import os.path
  7. def input_file(file_name):
  8. """
  9. This file inputs the file defined by INPUT_FILE into a string line and returns it
  10. :return: a string array containing the lines of INPUT_FILE
  11. """
  12. f = []
  13. with open(file_name.strip('\n')) as file:
  14. for line in file:
  15. f.append(line.strip(' \t\n\r'))
  16. return f
  17. def input_file_with_new_line(file_name):
  18. """
  19. Reads an entire file and returns the contents as a string
  20. """
  21. f = ""
  22. with open(file_name.strip('\n')) as file:
  23. for line in file:
  24. f = f + line
  25. return f
  26. def check_file_exists(fileloc):
  27. """
  28. Function which checks to see if a file exists
  29. :return: whether file exists
  30. """
  31. return os.path.exists(fileloc)
  32. def append_file(file_name, append):
  33. """
  34. Appends text to bottom of a text file
  35. :param file_name: name of file
  36. :param append: message to append on file
  37. :return: None
  38. """
  39. file_name = os.path.expanduser(file_name)
  40. f = open(file_name, "a+")
  41. f.write(append + "\n")
  42. f.close()
  43. def remove_line_from_file(file_name, remove):
  44. """
  45. removes a single line of text from a text file
  46. :param file_name:
  47. :param remove:
  48. :return:
  49. """
  50. lines = input_file(file_name)
  51. f = open(file_name, "w")
  52. for host in lines:
  53. if remove not in host:
  54. f.write(host + "\n")
  55. f.close()
  56. def create_empty_file(file_name):
  57. """
  58. simple function to create a new file on system
  59. """
  60. file_name = file_name.replace('\n', '')
  61. subprocess.call(['touch', file_name])
  62. # TOP_BAR = "********************************************"
  63. # TOP_BAR =
  64. def print_magenta(prt): return "\033[95m {}\033[00m".format(prt)
  65. def print_green(prt): return "\033[92m {}\033[00m".format(prt)
  66. def print_red(prt): return "\033[91m {}\033[00m".format(prt)
  67. def print_menu_option(s, top_bar_length):
  68. """
  69. Prints each host option
  70. :param s:
  71. :return:
  72. """
  73. space = " " * (top_bar_length - 4 - len(s))
  74. print(print_magenta("* ") + s + space + print_magenta("*"))
  75. def print_menu(name, lines):
  76. """
  77. Function which prints a nice menu for the user (box thing)
  78. ex:
  79. **************************************
  80. * SSH Drive Manager *
  81. * 1) Remove Remote Drive *
  82. * 2) Add Drive to Mount *
  83. * 3) View Drives *
  84. * 4) Exit *
  85. **************************************
  86. """
  87. padding_star = "*" * 5
  88. temp = 31
  89. max_len = 31
  90. for s in lines:
  91. temp = len(s)
  92. if max_len < temp:
  93. max_len = temp
  94. TOP_BAR = padding_star + "*" * max_len + padding_star
  95. if not len(name) % 2 == 0:
  96. name = name + " "
  97. name = name + " "
  98. spaces = len(TOP_BAR) - 4 - len(name)
  99. print(print_magenta(TOP_BAR))
  100. print(print_magenta("*") +
  101. (int(spaces / 2) * " ") +
  102. print_green(name) +
  103. (int(spaces / 2) * " ") +
  104. print_magenta("*"))
  105. for s in lines:
  106. print_menu_option(s, len(TOP_BAR))
  107. print(print_magenta(TOP_BAR))