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.

230 lines
6.1 KiB

  1. """
  2. Jeffery Russell
  3. 4-27-18
  4. """
  5. import subprocess
  6. import sys
  7. from utils import module
  8. import configuration
  9. """
  10. The mounts.txt file is a sequence of three strings
  11. user@remote.server.address
  12. /remote/mount/point
  13. /local/mount/point
  14. """
  15. MOUNT_FILE = configuration.get_config()["mounts"]
  16. def mount_drive(remote_connection, remote_mount_point, local_mount_point):
  17. """
  18. Calls sshfs to mount a remote connection on a local mount point over ssh
  19. """
  20. runCode = subprocess.call(["sshfs", "-o", "allow_other",
  21. remote_connection + ":" + remote_mount_point, local_mount_point])
  22. if runCode == 0:
  23. print("Mounted " + remote_connection + ":" + remote_mount_point +
  24. " to " + local_mount_point)
  25. else:
  26. print("Failed to mount " + remote_connection + ":" + remote_mount_point)
  27. def mount_drives():
  28. """
  29. Mounts all the ssh drives in the configuration file
  30. """
  31. file = module.input_file(MOUNT_FILE)
  32. if len(file) == 0:
  33. print(MOUNT_FILE + " is empty")
  34. else:
  35. for i in range(0, len(file), 3):
  36. mount_drive(file[i], file[i + 1], file[i + 2])
  37. def unmount_drive(local_mount_point):
  38. """
  39. UnMounts a drive from a computer
  40. """
  41. runCode = subprocess.call(["fusermount", "-u", local_mount_point])
  42. if runCode == 0:
  43. print("Un-Mounted " + local_mount_point)
  44. else:
  45. print("Failed to Un-Mount " + local_mount_point)
  46. def unmount_all_drives():
  47. """
  48. UnMounts the ssh drives from the computer
  49. """
  50. file = module.input_file(MOUNT_FILE)
  51. for i in range(0, len(file), 3):
  52. unmount_drive(file[i + 2])
  53. def forcefully_unmount_drive(local_mount_point):
  54. """
  55. Forcefully un-mounts a ssh drive
  56. :param local_mount_point:
  57. :return:
  58. """
  59. runCode = subprocess.call(["sudo","umount", "-l", local_mount_point])
  60. if runCode == 0:
  61. print("Un-Mounted " + local_mount_point)
  62. else:
  63. print("Failed to Un-Mount " + local_mount_point)
  64. def forcefully_unmount_drives():
  65. """
  66. Forcefully un-mounts all drives from the system
  67. :return:
  68. """
  69. file = module.input_file(MOUNT_FILE)
  70. for i in range(0, len(file), 3):
  71. forcefully_unmount_drive(file[i + 2])
  72. def remove_drive():
  73. """
  74. Prompts the user and removes a drive from MOUNT_FILE
  75. """
  76. options = []
  77. file = module.input_file(MOUNT_FILE)
  78. for i in range(0, len(file), 3):
  79. options.append(str(len(options) + 1) + ") " + file[i])
  80. options.append("A) Exit")
  81. module.print_menu("Remove SSH Drive", options)
  82. i = input("Enter Option:")
  83. if i.lower() != 'a' and int(i) <= len(file)/3 and int(i) > 0:
  84. index = (int(i) - 1) * 3
  85. f = open(MOUNT_FILE, "w")
  86. for x in range(0, len(file), 3):
  87. if index != x:
  88. f.write(file[x] + "\n")
  89. f.write(file[x + 1] + "\n")
  90. f.write(file[x + 2] + "\n")
  91. f.close()
  92. def add_drive_to_config(remote_connection, remote_mount_point,
  93. local_mount_point):
  94. """
  95. Adds a new network drive to the default mount config file
  96. """
  97. module.append_file(MOUNT_FILE, remote_connection)
  98. module.append_file(MOUNT_FILE, remote_mount_point)
  99. module.append_file(MOUNT_FILE, local_mount_point)
  100. def add_drive():
  101. """
  102. Prompts the user to enter information to add ssh mount drive to config
  103. """
  104. ssh_acct = input("Enter your ssh account:")
  105. remote_mount = input("Enter the remote mount point:")
  106. local_mount = input("Enter the local mount point:")
  107. add_drive_to_config(ssh_acct, remote_mount, local_mount)
  108. def view_drives():
  109. """
  110. Views the current drives to the user
  111. """
  112. drives = []
  113. file = module.input_file(MOUNT_FILE)
  114. for i in range(0, len(file), 3):
  115. drives.append(str(int(int(i)/3 + 1)) + ") " + file[i])
  116. drives.append(" " + file[i + 1])
  117. drives.append(" " + file[i + 2])
  118. module.print_menu("SSH Drives", drives)
  119. def print_mount_menu():
  120. """
  121. Displays box which has mount menu options
  122. """
  123. module.print_menu("SSH Drive Manager ", ["1) Mount SSH Drives",
  124. "2) Un-Mount SSH Drives",
  125. "3) Remove Remote Drive",
  126. "4) Add Drive to Mount",
  127. "5) View Drives",
  128. "6) Usage",
  129. "7) Manage Config",
  130. "8) Forcefully Un-Mount SSH Drives",
  131. "9) Exit"])
  132. def manage_mount_file():
  133. """
  134. Method which prompts user which action to take with mounts file
  135. """
  136. print_mount_menu()
  137. i = input("Enter Option:")
  138. while i != '9':
  139. if i == '4':
  140. add_drive()
  141. elif i == '3':
  142. remove_drive()
  143. elif i == '5':
  144. view_drives()
  145. elif i == '1':
  146. mount_drives()
  147. elif i == '2':
  148. unmount_all_drives()
  149. elif i == '8':
  150. forcefully_unmount_drives()
  151. elif i == '6':
  152. print_usage()
  153. elif i == '7':
  154. configuration.main()
  155. else:
  156. print("Invalid Option")
  157. if i != '1' and i != '2':
  158. print_mount_menu()
  159. i = input("Enter Option:")
  160. else:
  161. break
  162. def print_usage():
  163. """
  164. Prints the usage message to the terminal
  165. """
  166. print("Usage:")
  167. print("\t-m mounts drives to computer")
  168. print("\t-u unmounts drives from the computer")
  169. def main():
  170. """
  171. Parses cmd line input and calls appropriate functions
  172. """
  173. if len(sys.argv) > 1:
  174. if sys.argv[1].lower() == "-m":
  175. mount_drives()
  176. elif sys.argv[1].lower() == "-u":
  177. unmount_all_drives()
  178. else:
  179. print("Invalid Command")
  180. print_usage()
  181. else:
  182. manage_mount_file()
  183. """
  184. Makes sure that other programs don't execute the main
  185. """
  186. if __name__ == '__main__':
  187. try:
  188. main()
  189. except KeyboardInterrupt:
  190. exit()