Skip to content

Instantly share code, notes, and snippets.

@lukas-buergi
Last active December 21, 2015 13:18
Show Gist options
  • Save lukas-buergi/6311402 to your computer and use it in GitHub Desktop.
Save lukas-buergi/6311402 to your computer and use it in GitHub Desktop.
A script which mounts everything in /media of one or several remote machines into a local folder (by default /media). It adds a prefix to the stuff which is mounted to avoid conflicts.
#!/usr/bin/python3.3
import argparse, os, subprocess
#settings
setting_hostList=['agathe','a10','lukas-c', 'dummy']
addresses={ 'agathe':'192.168.1.2',
'a10':'192.168.1.21',
'lukas-c':'192.168.1.3',
'dummy':'192.168.1.15'}
usernames={ 'agathe':'t4b',
'a10':'t4b',
'lukas-c':'t4b',
'dummy':'hans'}
mountpoint='/media/'
def getOwnIP():
"""Returns the machines IP adresses in a list by querying ifconfig using the 'subprocess' module. Also uses grep."""
ifconfig=subprocess.check_output('ifconfig | grep -Eoe \'inet addr:([0-9]{1,3}\.){3}[0-9]{1,3}\' | grep -Eoe \'([0-9]{1,3}\.){3}[0-9]{1,3}\'', shell=True)
ownIPs=str(ifconfig, encoding='utf-8').splitlines()
return(ownIPs)
#shouldn't work on itself as the remote machine
hosts=[]
for host in setting_hostList:
if addresses[host] in getOwnIP():
continue
hosts.append(host)
#parse arguments
parser = argparse.ArgumentParser(description='Mount all folders from /media/ of remote machines over SSH.')
parser.add_argument('hosts', metavar='host', nargs='*', default=hosts, choices=hosts + [hosts], help='A list of remote host names as saved in the "hosts" list in the executable file. If no host is given ALL hosts are chosen.')
parser.add_argument('-n', '--dry-run', action='store_true', default=False, dest='dry', help='Don\'t do anything.')
args = parser.parse_args()
#dry run, abort
if args.dry:
print('Would have worked with the following remote machines:', args.hosts)
quit(0)
#mount stuff
for host in args.hosts:
try:
lsmedia=str(subprocess.check_output(['/usr/bin/ssh', usernames[host] + '@' + addresses[host], '/bin/ls --show-control-chars -1 -b /media/']),encoding='utf-8').splitlines()
except subprocess.CalledProcessError:
print('There was an error while ssh tried connecting to host ' + host + ', skipping it.')
continue
for remDir in lsmedia:
locDir=host + '.' + remDir
try:
os.mkdir(mountpoint + locDir, 0o777)
except FileExistsError:
pass
subprocess.call(['/usr/bin/sshfs', usernames[host] + '@' + addresses[host] + ':/media/' + remDir, mountpoint + locDir + '/'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment