Last active
August 29, 2015 14:12
-
-
Save prochor666/42f5bb77488a792c0da7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import pexpect | |
import json | |
import time | |
import os | |
from sys import argv | |
class Mounter: | |
def __init__(self, type='reports', name=''): | |
self.HEADER = '\033[95m' | |
self.OKBLUE = '\033[94m' | |
self.OKGREEN = '\033[92m' | |
self.WARNING = '\033[93m' | |
self.FAIL = '\033[91m' | |
self.ENDC = '\033[0m' | |
self.BOLD = '\033[1m' | |
self.UNDERLINE = '\033[4m' | |
self.SCRIPTINFO = """ | |
********** Mount script ********** | |
** SUPPORTS sshfs | |
@author prochor666@gmail.com | |
@requires sshfs, fusermount, expect, python-pexpect | |
""" | |
self.invalid = """ | |
!!! Invalid parameters !!! | |
Type ' """+argv[0]+""" h' OR '"""+argv[0]+""" help' for help | |
""" | |
self.help = """ | |
Packages needed: | |
Use apt-get install expect expect-dev python-pexpect sshfs | |
Usage: | |
- For mount: | |
'"""+argv[0]+""" m config.file' | |
Use filename without extension | |
Config files are located in 'conf' directory | |
Example: 'conf/config.file.conf' | |
Remote location is mounted in 'location/config.file' directory | |
- For unmount: | |
'"""+argv[0]+""" u config.file' | |
- For help: | |
'"""+argv[0]+""" h' OR '"""+argv[0]+""" help' | |
""" | |
def info(self, msg): | |
print self.OKBLUE + str(msg) + self.ENDC | |
def success (self, msg): | |
print self.OKGREEN + str(msg) + self.ENDC | |
def fail(self, msg): | |
print self.FAIL + str(msg) + self.ENDC | |
def warning (self, msg): | |
print self.WARNING + str(msg) + self.ENDC | |
def load_config(self, cnf): | |
json_data=open(cnf).read() | |
return json_data | |
def start_sshfs(self, cmd, pw): | |
try: | |
ssh_newkey = 'Are you sure you want to continue connecting' | |
sshfs=pexpect.spawn(cmd) | |
i=sshfs.expect([ssh_newkey,'password',pexpect.EOF,pexpect.TIMEOUT],1) | |
if i==0: | |
print "I say yes" | |
sshfs.sendline('yes') | |
time.sleep(0.5) | |
i=sshfs.expect([ssh_newkey,'password: ',pexpect.EOF]) | |
if i==1: | |
print "I give password" | |
time.sleep(0.5) | |
sshfs.sendline(pw) | |
elif i==2: | |
print "I either got key or connection timeout" | |
pass | |
elif i==3: #timeout | |
pass | |
#sshfs = pexpect.spawn(cmd) | |
#sshfs.expect("password: ") | |
#time.sleep (0.5) | |
#sshfs.sendline (pw) | |
time.sleep (5) | |
sshfs.expect (pexpect.EOF) | |
self.success(""" | |
Location mounted successfully | |
Listing directory: | |
""" | |
) | |
except Exception, e: | |
self.fail(str(e)) | |
def stop_sshfs(self, cmd): | |
try: | |
self.info("Unmounting: "+cmd) | |
pexpect.run(cmd) | |
except Exception, e: | |
self.fail(str(e)) | |
def mount_ssh(self): | |
self.success(self.SCRIPTINFO) | |
if len(argv) == 3 and argv[1] == 'm': | |
config_file = 'conf/'+argv[2]+'.conf' | |
if os.path.exists(config_file): | |
cnf = json.loads(self.load_config(config_file)) | |
if os.path.exists('location/'+argv[2]) == False: | |
os.mkdir('location/'+argv[2], 0775) | |
user_str =str(cnf['user'])+'@'+str(cnf['host'])+':'+str(cnf['remoteDir'])+' location/'+argv[2] | |
password = str(cnf['pwd']) | |
self.info('Mounting location: '+str(cnf['host'])+' to: '+' location/'+argv[2]) | |
#self.start_sshfs("sshfs -o idmap=user " + user_str, password) | |
self.start_sshfs("sshfs " + user_str, password) | |
os.system('ls '+'location/'+argv[2]) | |
else: | |
self.fail("Config file 'conf/"+argv[2]+".conf' not found") | |
elif len(argv) == 3 and argv[1] == 'u': | |
self.stop_sshfs("fusermount -uz " + 'location/'+argv[2]) | |
if os.path.exists('location/'+argv[2]): | |
os.rmdir('location/'+argv[2]) | |
elif len(argv) == 2 and (argv[1] == 'h' or argv[1] == 'help') : | |
self.info(self.help) | |
else: | |
self.fail(self.invalid) | |
if __name__ == '__main__': | |
os.system('clear') | |
m = Mounter() | |
m.mount_ssh () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"host":"domain.tld","user":"username","pwd":"password","remoteDir":"/var/www"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment