Commit 04dfc80b authored by Björn's avatar Björn

adding

parent e1af8c3c
......@@ -12,6 +12,7 @@ import subprocess
import os
from src.config import Config
from termcolor import colored
#
#
......@@ -67,6 +68,7 @@ class Storagebox(Config):
return process
#
# get home directory
#
#
#
......@@ -82,6 +84,7 @@ class Storagebox(Config):
# delete authorized_keys-file if it exists
#
#
#
def _clean(self):
authorized_keys_file = self._homeDir() + '/authorized_keys'
......@@ -89,32 +92,44 @@ class Storagebox(Config):
if os.path.exists(authorized_keys_file) and os.path.isfile(authorized_keys_file):
os.remove(authorized_keys_file)
#
# create ssh-key with current hostname
#
#
#
def _create_ssh_key(self):
ssh_file = self._homeDir() + '/.ssh/backup_' + self._namespace
if not os.path.exists(ssh_file):
self._command("ssh-keygen -f " + ssh_file + " -t rsa -b 4096 -N ''") # generate ssh-key without passphrase
self._command("cat " + ssh_file + ".pub >> " + self._homeDir() + "/authorized_keys") # authorize key
# generate ssh-key without passphrase
self._command("ssh-keygen -f " + ssh_file + " -t rsa -b 4096 -N ''")
# authorize key
self._command("cat " + ssh_file + ".pub >> " + self._homeDir() + "/authorized_keys")
print('create ssh-key backup_' + self._namespace)
else:
print('ssh-key already exists!')
print(colored('SSH-Key already exists!', 'yellow'))
self._clean()
print(colored('No Key added! Exit Script!', 'red'))
exit()
#
# delete ssh-keys and remove from authorized_keys
#
#
#
def _remove_ssh_key(self):
ssh_file = self._homeDir() + '/.ssh/backup_' + self._namespace
if os.path.exists(ssh_file):
with open('data.txt', 'r') as file:
ssh_file_pub = file.read()
with open('data.txt', 'r') as file:
ssh_file_pub = file.read()
os.remove(ssh_file)
os.remove(ssh_file + '.pub')
......@@ -125,6 +140,7 @@ class Storagebox(Config):
# get authorized_keys from storagebox
#
#
#
def _download(self):
command = 'sftp -P ' + self._remote_port + ' ' + self._config.get('default', 'remote_host') + ':.ssh/authorized_keys ' + self._homeDir()
process = self._command(command)
......@@ -132,21 +148,21 @@ class Storagebox(Config):
if (not process.stderr):
print(process.stderr)
exit()
else:
print('download authorized_keys')
#
# 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('upload authorized_keys')
print(colored(self._args.command + ' complete!', 'green'))
#
#
#
#
#
def run(self):
self._download()
......@@ -156,7 +172,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