-
Notifications
You must be signed in to change notification settings - Fork 7
/
mount_tuni.py
executable file
·77 lines (58 loc) · 1.76 KB
/
mount_tuni.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/python3
"""Script for mounting Tampere University network drives
SFTP access to Tampere University network drives was shut down on 2020-02-20,
so this script no longer works.
"""
import os
# Usage of the subprocess module is necessary
import subprocess # nosec
MOUNTPOINT = "/media/lab/tuni"
def yesno() -> bool:
res = input() # nosec
while res not in ["y", "n"]:
print("Please input y or n")
res = input() # nosec
if res == "y":
return True
elif res == "n":
return False
else:
raise RuntimeError()
def mount():
print("Please input credentials for mounting the network drive")
print("Username: ", end="")
username = input() # nosec
proc = subprocess.Popen( # nosec
# Passing of user-given parameters is ok, as their usage is explicit
["sshfs", f"{username}@ssh.intra.tut.fi:/", MOUNTPOINT],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
proc.communicate()
print("The network drive should now be mounted")
def unmount():
print("Unmounting")
subprocess.run(["fusermount", "-u", MOUNTPOINT]) # nosec
print("Unmounted")
def main():
try:
contents = os.listdir(MOUNTPOINT)
except OSError:
print("The network drive appears to be stuck. Closing connection.")
unmount()
return
if contents:
print("Network drive appears to be already mounted. Unmount? (y/n)")
res = yesno()
if res:
unmount()
return
mount()
res = ""
while res != "u":
print("Enter u to unmount the drive")
res = input() # nosec
unmount()
print("Ready. Press enter to exit")
input() # nosec
if __name__ == "__main__":
main()