Commit 68200af5 authored by Björn's avatar Björn

adding

parent 04dfc80b
......@@ -60,9 +60,8 @@ class Storagebox(Config):
def _command(self, command):
process = subprocess.run(command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
check=True,
shell=True)
return process
......@@ -108,13 +107,13 @@ class Storagebox(Config):
# authorize key
self._command("cat " + ssh_file + ".pub >> " + self._homeDir() + "/authorized_keys")
print('create ssh-key backup_' + self._namespace)
print('Create SSH-Key backup_' + self._namespace)
else:
print(colored('SSH-Key already exists!', 'yellow'))
self._clean()
print(colored('No Key added! Exit Script!', 'red'))
print(colored('No SSH-Key added! Exit Script!', 'red'))
exit()
#
......@@ -125,16 +124,44 @@ class Storagebox(Config):
def _remove_ssh_key(self):
ssh_file = self._homeDir() + '/.ssh/backup_' + self._namespace
ssh_pub_file = ssh_file + '.pub'
if os.path.exists(ssh_file):
if os.path.exists(ssh_file) and os.path.exists(ssh_pub_file):
with open('data.txt', 'r') as file:
ssh_file_pub = file.read()
print('SSH-Key backup_' + self._namespace + ' found!')
with open(ssh_pub_file, 'r') as file:
ssh_pub_file_string = file.read()
with open(self._homeDir() + '/authorized_keys', 'r') as file:
authorized_keys_string = file.read()
authorized_keys_string = authorized_keys_string.replace(ssh_pub_file_string , '')
with open(self._homeDir() + '/authorized_keys', 'w') as file:
file.write(authorized_keys_string)
os.remove(ssh_file)
os.remove(ssh_file + '.pub')
os.remove(ssh_pub_file)
self._command("sed -i '/' + + '/d' " + self.homeDir() + "/authorized_keys")
else:
print(colored('No SSH-Key found!', 'yellow'))
print(colored('No SSH-Key removed! Exit Script!', 'red'))
exit()
#
# testing connection
#
#
def _connection(self):
command = self._sftp_command('version')
process = self._command(command)
if process.stdout.find('SFTP protocol version') == -1:
print(colored('Connection Failed!', 'yellow'))
print(colored('Exit Script!', 'red'))
exit()
#
# get authorized_keys from storagebox
......@@ -145,18 +172,21 @@ class Storagebox(Config):
command = 'sftp -P ' + self._remote_port + ' ' + self._config.get('default', 'remote_host') + ':.ssh/authorized_keys ' + self._homeDir()
process = self._command(command)
if (not process.stderr):
print(process.stderr)
exit()
#
# connect to remote, create directory and upload authorized_keys
#
#
#
def _upload(self):
self._sftp_command("mkdir .ssh \n chmod 700 .ssh \n put " + self._homeDir() + "/authorized_keys .ssh/authorized_keys \n chmod 600 .ssh/authorized_keys")
print(colored(self._args.command + ' complete!', 'green'))
command = self._sftp_command("mkdir .ssh \n chmod 700 .ssh \n put " + self._homeDir() + "/authorized_keys .ssh/authorized_keys \n chmod 600 .ssh/authorized_keys")
process = self._command(command)
if (process.stderr):
print(colored(process.stderr, 'yellow'))
print(colored('No SSH-Key added! Exit Script!', 'red'))
exit()
else:
print(colored(self._args.command.capitalize() + ' Completed!', 'green'))
#
#
......@@ -164,6 +194,8 @@ class Storagebox(Config):
#
#
def run(self):
self._connection()
self._download()
if self._args.command == 'add':
......@@ -172,7 +204,7 @@ class Storagebox(Config):
self._remove_ssh_key()
self._upload()
#self._clean()
self._clean()
storagebox = Storagebox()
storagebox.run()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment