Skip to content
Permalink
Browse files
minor fix for custom backup user
  • Loading branch information
usmannasir committed Apr 27, 2020
1 parent 43eca57 commit ed0efbf9e04d0fc8d275068b685c7ab05d25a9f4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 26 deletions.
@@ -407,46 +407,65 @@ def submitDestinationCreation(self, userID = None, data = None):

destinations = backupUtil.backupUtilities.destinationsPath

ipAddress = data['IPAddress']
password = data['password']
finalDic = {}

finalDic['ipAddress'] = data['IPAddress']
finalDic['password'] = data['password']

try:
finalDic['port'] = data['backupSSHPort']
except:
finalDic['port'] = "22"

try:
finalDic['user'] = data['user']
except:
finalDic['user'] = "root"

if dest.objects.all().count() == 2:

final_dic = {'destStatus': 0,
'error_message': "Currently only one remote destination is allowed."}
final_json = json.dumps(final_dic)
return HttpResponse(final_json)

try:
d = dest.objects.get(destLoc=ipAddress)
d = dest.objects.get(destLoc=finalDic['password'])
final_dic = {'destStatus': 0, 'error_message': "This destination already exists."}
final_json = json.dumps(final_dic)
return HttpResponse(final_json)
except:

try:
port = data['backupSSHPort']
except:
port = "22"

execPath = "/usr/local/CyberCP/bin/python " + virtualHostUtilities.cyberPanel + "/plogical/backupUtilities.py"
execPath = execPath + " submitDestinationCreation --ipAddress " + ipAddress + " --password " \
+ password + " --port " + port
execPath = execPath + " submitDestinationCreation --ipAddress " + finalDic['ipAddress'] + " --password " \
+ finalDic['password'] + " --port " + finalDic['port'] + ' --user %s' % (finalDic['user'])

if os.path.exists(ProcessUtilities.debugPath):
logging.CyberCPLogFileWriter.writeToFile(execPath)

output = ProcessUtilities.outputExecutioner(execPath)

if os.path.exists(ProcessUtilities.debugPath):
logging.CyberCPLogFileWriter.writeToFile(output)


if output.find('1,') > -1:
try:
writeToFile = open(destinations, "w")
writeToFile.writelines(ipAddress + "\n")
writeToFile.writelines(data['backupSSHPort'] + "\n")
writeToFile.write(json.dumps(finalDic))
writeToFile.close()
newDest = dest(destLoc=ipAddress)
newDest = dest(destLoc=finalDic['ipAddress'])
newDest.save()

final_dic = {'destStatus': 1, 'error_message': "None"}
final_json = json.dumps(final_dic)
return HttpResponse(final_json)
except:
writeToFile = open(destinations, "w")
writeToFile.writelines(ipAddress + "\n")
writeToFile.writelines("22" + "\n")
writeToFile.write(json.dumps(finalDic))
writeToFile.close()
newDest = dest(destLoc=ipAddress)

newDest = dest(destLoc=finalDic['ipAddress'])
newDest.save()

final_dic = {'destStatus': 1, 'error_message': "None"}
@@ -33,25 +33,25 @@ <h3 class="title-hero">
</div>
</div>


<div class="form-group">
<label class="col-sm-3 control-label">{% trans "Password" %}</label>
<label class="col-sm-3 control-label">{% trans "User" %}</label>
<div class="col-sm-6">
<input placeholder="" type="password" class="form-control" ng-model="password" required>
<input placeholder="{% trans "Backup server SSH User, leave empty for root." %}" type="text" class="form-control" ng-model="user" required>
</div>
</div>


<div class="form-group">
<label class="col-sm-3 control-label">{% trans "Port" %}</label>
<label class="col-sm-3 control-label">{% trans "Password" %}</label>
<div class="col-sm-6">
<input placeholder="{% trans "Backup server SSH Port, leave empty for 22." %}" type="text" class="form-control" ng-model="backupSSHPort" required>
<input placeholder="" type="password" class="form-control" ng-model="password" required>
</div>
</div>

<div class="form-group">
<label class="col-sm-3 control-label">{% trans "User" %}</label>
<label class="col-sm-3 control-label">{% trans "Port" %}</label>
<div class="col-sm-6">
<input placeholder="{% trans "Backup server SSH User, leave empty for root." %}" type="text" class="form-control" ng-model="user" required>
<input placeholder="{% trans "Backup server SSH Port, leave empty for 22." %}" type="text" class="form-control" ng-model="backupSSHPort" required>
</div>
</div>

@@ -928,6 +928,9 @@ def sendKey(IPAddress, password, port='22', user='root'):
command = "scp -o StrictHostKeyChecking=no -P " + port + " /root/.ssh/cyberpanel.pub " + user + "@" + IPAddress + ":~/.ssh/authorized_keys"
setupKeys = pexpect.spawn(command, timeout=3)

if os.path.exists(ProcessUtilities.debugPath):
logging.CyberCPLogFileWriter.writeToFile(command)

index = setupKeys.expect(expectation)

## on first login attempt send password
@@ -981,6 +984,9 @@ def setupSSHKeys(IPAddress, password, port='22', user='root'):
command = "ssh -o StrictHostKeyChecking=no -p " + port + ' ' + user + "@" + IPAddress + ' "mkdir ~/.ssh || rm -f ~/.ssh/temp && rm -f ~/.ssh/authorized_temp && cp ~/.ssh/authorized_keys ~/.ssh/temp"'
setupKeys = pexpect.spawn(command, timeout=3)

if os.path.exists(ProcessUtilities.debugPath):
logging.CyberCPLogFileWriter.writeToFile(command)

index = setupKeys.expect(expectation)

## on first login attempt send password
@@ -1040,10 +1046,11 @@ def checkConnection(IPAddress, password, port='22', user='root'):
try:

try:
import json
destinations = backupUtilities.destinationsPath
data = open(destinations, 'r').readlines()
port = data[1].strip("\n")
user = data[2].strip("\n")
data = json.loads(open(destinations, 'r').read())
port = data['port']
user = data['user']
except:
port = "22"

@@ -1160,12 +1167,24 @@ def createBackupDir(IPAddress, port='22', user='root'):

try:
command = "sudo ssh -o StrictHostKeyChecking=no -p " + port + " -i /root/.ssh/cyberpanel " + user + "@" + IPAddress + " mkdir ~/backup"

if os.path.exists(ProcessUtilities.debugPath):
logging.CyberCPLogFileWriter.writeToFile(command)

subprocess.call(shlex.split(command))

command = "sudo ssh -o StrictHostKeyChecking=no -p " + port + " -i /root/.ssh/cyberpanel " + user + "@" + IPAddress + ' "cat ~/.ssh/authorized_keys ~/.ssh/temp > ~/.ssh/authorized_temp"'

if os.path.exists(ProcessUtilities.debugPath):
logging.CyberCPLogFileWriter.writeToFile(command)

subprocess.call(shlex.split(command))

command = "sudo ssh -o StrictHostKeyChecking=no -p " + port + " -i /root/.ssh/cyberpanel " + user + "@" + IPAddress + ' "cat ~/.ssh/authorized_temp > ~/.ssh/authorized_keys"'

if os.path.exists(ProcessUtilities.debugPath):
logging.CyberCPLogFileWriter.writeToFile(command)

subprocess.call(shlex.split(command))

except BaseException as msg:

0 comments on commit ed0efbf

Please sign in to comment.