Commit e1af8c3c authored by Björn's avatar Björn

adding

parent bcf5c8e8
......@@ -23,12 +23,13 @@ import paramiko
import platform
from src.sendmail import Sendmail
from src.config import Config
#
#
#
#
class Backup:
class Backup(Config):
#
#
......@@ -57,15 +58,9 @@ class Backup:
# parse arguments
self._args = parser.parse_args()
if (self._args.config is None):
config = '.config'
else:
config = self._args.config
self._config = configparser.RawConfigParser(allow_no_value=True)
self._config.read_file(open(config))
# init parent
super().__init__()
self._remote_port = self._config.get('default', 'remote_port', fallback='22')
self._logs = ''
#
......
This diff is collapsed.
import configparser
#
#
#
#
#
class Config:
#
#
# @param message
# @param subject
# @param options { encrypt: True or False }
#
def __init__(self):
if (self._args.config is None):
config = '.config'
else:
config = self._args.config
self._config = configparser.RawConfigParser(allow_no_value=True)
self._config.read_file(open(config))
self._remote_port = self._config.get('default', 'remote_port', fallback='22')
#!/usr/bin/env python3
#
#
#
#
import getpass
import argparse
import configparser
import platform
import subprocess
import os
from src.config import Config
#
#
#
#
class Storagebox:
class Storagebox(Config):
#
#
......@@ -12,19 +25,31 @@ class Storagebox:
#
def __init__(self):
# adding parser for arguments
parser = argparse.ArgumentParser()
# define arguments
parser.add_argument('command', help='add|remove')
# define options
parser.add_argument('-c', '--config', help='full path to config-file')
# parse arguments
self._args = parser.parse_args()
if (self._args.config is None):
config = '.config'
else:
config = self._args.config
# init parent
super().__init__()
self._namespace = platform.uname()[1]
self._remote_port = self._config.get('default', 'remote_port', fallback='22')
self._namespace = platform.uname()[1])
#
# create command for sftp
#
# @param command
#
def _sftp_command(self, command):
return "echo \"" + command + "\" | sftp -P " + self._remote_port + " " + self._config.get('default', 'remote_host')
#
# call subprocess
......@@ -45,24 +70,24 @@ class Storagebox:
#
#
#
def _home(self):
def _homeDir(self):
if (getpass.getuser() == 'root'):
home = '/root/'
else:
home = '/home/' + getpass.getuser() + '/'
home = '/home/' + getpass.getuser()
return home
#
#
# delete authorized_keys-file if it exists
#
#
def _clean(self):
authorized_keys_file = elf._home() + '/authorized_keys'
authorized_keys_file = self._homeDir() + '/authorized_keys'
if os.path.exists(authorized_keys_file) and os.path.file(authorized_keys_file):
os.delete(authorized_keys_file)
if os.path.exists(authorized_keys_file) and os.path.isfile(authorized_keys_file):
os.remove(authorized_keys_file)
#
......@@ -71,32 +96,40 @@ class Storagebox:
#
def _create_ssh_key(self):
ssh_file = self._home() + '/.ssh/backup_' + self._namespace
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._home() + "/authorized_keys") # authorize key
self._command("cat " + ssh_file + ".pub >> " + self._homeDir() + "/authorized_keys") # authorize key
else:
print('ssh-key already exists!')
exit()
#
# create command for sftp
# delete ssh-keys and remove from authorized_keys
#
# @param command
#
def _sftp_command(self, command):
return "echo \"" + command + "\" | sftp -P " + self._remote_port + " " + self._config.get('default', 'remote_host')
def _remove_ssh_key(self):
if os.path.exists(ssh_file):
with open('data.txt', 'r') as file:
ssh_file_pub = file.read()
os.remove(ssh_file)
os.remove(ssh_file + '.pub')
self._command("sed -i '/' + + '/d' " + self.homeDir() + "/authorized_keys")
#
# get authorized_keys from storagebox
#
#
def _download(self):
command = 'sftp -P ' + self._config.get('default', 'remote_port', 22) + ' ' + self._config.get('default', 'remote_host') + ':/.ssh/authorized_keys ' + self._home()
command = 'sftp -P ' + self._remote_port + ' ' + self._config.get('default', 'remote_host') + ':.ssh/authorized_keys ' + self._homeDir()
process = self._command(command)
if (process.stderr != ''):
if (not process.stderr):
print(process.stderr)
exit()
else:
......@@ -107,7 +140,7 @@ class Storagebox:
#
#
def _upload(self):
self._sftp_command("mkdir .ssh \n chmod 700 .ssh \n put " + self._home() + "/authorized_keys .ssh/authorized_keys \n chmod 600 .ssh/authorized_keys")
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')
#
......@@ -116,8 +149,14 @@ class Storagebox:
#
def run(self):
self._download()
self._create_ssh_key()
if self._args.command == 'add':
self._create_ssh_key()
elif self._args.command == 'remove':
self._remove_ssh_key()
self._upload()
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