1

I make cloud panel projects with django but i have a problem. i am click to manage button after this page show:

https://s18.postimg.org/lj7qwc8p5/Ekran_G_r_nt_s_2018-02-21_19-22-33.png

I make connect to paramiko. Paramiko is right work but this is not connect yet. so we will make first with connect paramiko after then, later on the directory make verification on server.

This code piece is wrong:

from django.shortcuts import render
from explorer.models import Servers
from django.http import HttpResponse
import paramiko
import logging
import csv
paramiko.util.log_to_file("filename.log")
# Create your views here.

def listfiles(path,ssh,server,sftp):
        cmd = "cd "+path
        lf = "ls -p | grep -v /"
        f = open('dumps/' + server + 'traverse.sh','w+')
        f.write('#!/bin/bash\n')
        f.close()
        f = open('dumps/'+ server + 'traverse.sh','a+')
        f.write(cmd + "\n")
        f.write(lf)
        f.close()
        sftp.put('dumps/' + server +'traverse.sh ','traverse.sh')
        ssh.exec_command("chmod 777 traverse.sh")
        stdin,stdout,stderr = ssh.exec_command("./traverse.sh")
        data = stdout.read()
        ssh.exec_command("rm -rf traverse.sh")
        return data.split()

line 37 = lf = "ls -p | grep -v /"

manage.py inside: #!/usr/bin/env python import os import sys

   if __name__ == "__main__":
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cloudpanel.settings")

        from django.core.management import execute_from_command_line

        execute_from_command_line(sys.argv)

This function is connect to server with paramiko:

def filemanager(request,server,path):
s = Servers.objects.get(boxname = server)
orginalpath = s.path
a = orginalpath.split('/')
a = [k for k in a if k]
orginalpath = "/".join(a)
orginalpath = "/"+orginalpath+"/"
if path == 'new':
    path = orginalpath
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(s.ip,port=s.port,username=s.username,password=s.password)
    t=paramiko.Transport((s.ip,s.port))
    t.connect(username=s.username,password=s.password)
    sftp = paramiko.SFTPClient.from_transport(t)
    remotefiles = listfiles(path,ssh,server,sftp)
    remotedirs = listdirs(path,ssh,server,sftp)
    ssh.close()
    sftp.close()
    modpath = path.replace('/','*')
    return render(request,'manage.html',{'files':remotefiles,'dirs':remotedirs,'path':path,'orginalpath':orginalpath,'server':server,'modpath':modpath})

How to be solution my problem? Do not look for my bad english.

Safa
  • 31
  • 7
  • Also you better do not combine SFTP with shell commands. Use SFTP for everything that SFTP can do (like `chmod` => `sftp.chmod` and `rm` => `sftp.remove`). – Martin Prikryl Feb 22 '18 at 06:27
  • I did it, thank @MartinPrikryl. https://postimg.org/image/733tqyxdx/ it's work. Thanks. – Safa Feb 23 '18 at 10:18

1 Answers1

0

The answer is using with sftp instance as I used in question ssh.exec_command("chmod 777 traverse.sh"). 'paramiko.SFTPClient.from_transport(t)' contains sftp commands. I should have used sftp.remove and sftp.chmod lines rather running in command line. Thank you Martin Prikryl for answer.

The other question link: How to list all the folders and files in the directory after connecting through SFTP in Python

Safa
  • 31
  • 7