|
# USE PYTHON3 |
|
|
|
from ast import Pass |
|
import os, json, getpass |
|
|
|
if os.geteuid() != 0: |
|
print("Must be run with sudo to manipulate mounts.") |
|
exit() |
|
|
|
def get_cifs_mounts(): |
|
output = os.popen('findmnt --json').read().strip() |
|
output = json.loads(output)['filesystems'][0]['children'] |
|
output = list(filter(lambda mount: mount['fstype'] == 'cifs', output)) |
|
return output |
|
|
|
def print_active_cifs_mounts(): |
|
print(f'\nCIFS mounts:') |
|
if len(get_cifs_mounts()): |
|
for x in get_cifs_mounts(): |
|
print(f' > {x["source"] : <40} -> {x["target"] : <30}') |
|
else: |
|
print (" -- None active --") |
|
print() |
|
|
|
def unmount_cifs_mounts(): |
|
for mount in get_cifs_mounts(): |
|
result = os.popen(f'umount {mount["target"]}').read().strip() |
|
print(f' > Unmounted {mount["target"]}') |
|
|
|
def mount_cifs_shares_for_username_on_host(): |
|
|
|
default_lp = "nas" |
|
default_hs = "192.168.1.2" |
|
|
|
lp = input(f"Enter local root (after /mnt/) [{default_lp}] : ") |
|
if lp == "": |
|
lp = default_lp |
|
hs = input(f"Enter hostname or IP [{default_hs}] : ") |
|
if hs == "": |
|
hs = default_hs |
|
un = input("Enter username : ") |
|
pw = getpass.getpass("Enter user password : ") |
|
|
|
os.popen(f'mkdir -p /mnt/{lp}').read().strip() |
|
for line in os.popen(f"smbclient -L \\\\{hs} -U '{un}%{pw}'").read().splitlines(): |
|
if "Disk" in line: |
|
share = line.split("Disk")[0].strip() |
|
os.popen(f'mkdir -p /mnt/{lp}/{share.replace(" ","_")}').read().strip() |
|
print(f' > Mounting /mnt/{lp}/{share.replace(" ","_")}') |
|
os.popen(f'sudo mount -t cifs \'//{hs}/{share}\' /mnt/{lp}/{share.replace(" ","_")} -o username={un},password={pw},iocharset=utf8,file_mode=0777,dir_mode=0777,vers=1.0').read() |
|
|
|
print_active_cifs_mounts() |
|
if input("Mount shared folders (yes/no) ? ").lower() in ["y", "yes"]: |
|
mount_cifs_shares_for_username_on_host() |
|
print_active_cifs_mounts() |
|
if input("Unmount shared folders (yes/no) ? ").lower() in ["y", "yes"]: |
|
unmount_cifs_mounts() |
|
print_active_cifs_mounts() |