2016-06-08 1 views
0

,paramiko로 gzip 파일의 내용을 어떻게 볼 수 있습니까? 다음과 같이 내가 원격 서버에 연결하면

transport.connect(username=username, password=password) 
sftp = paramiko.SFTPClient.from_transport(transport) 

나는 sftp.listdir()을하고 example.gz.2016처럼 원격 서버에 일부 GZIP 파일이 있다는 것을 알 수있다. 실제로 파일을 다운로드하지 않고 sftp 연결을 통해이 파일의 텍스트에 액세스하려면 어떻게해야합니까?

+0

처럼 SSH 연결에서 그것을 얻을 필요가 있지만, 보통의 콘솔 명령어로 나열 할 수 있습니다 SSH를 통해. –

+0

왜 이것이 가능하지 않습니까? –

+2

SFTP 프로토콜의 일부가 아닙니다. 가능한 모든 기능을 조회 할 수 있습니다. https://tools.ietf.org/html/draft-ietf-secsh-filexfer-13 –

답변

1

귀하의 질문은 두 부분이 있습니다

  1. 방법 명령 줄에서 zip 파일의 내용을 볼을
  2. 원격 명령을 실행하고
paramiko 파이썬 &를 사용하여 출력을 얻을하는 방법
  • 처음으로 사물 : 콘솔에 zip 파일의 내용을 나열하는 방법. less can look into zip files, 그래서 귀하의 경우, 실행 less example.gz.2016

    둘째 우편 아카이브 안에 당신에게 파일의 목록을 제공한다 : 원격으로 명령을 실행하는 방법에 대해 설명합니다.

    import paramiko 
    ssh = paramiko.SSHClient() 
    # next is needed if your keys are not yet known on the client. 
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
    
    ssh.connect(HOST_NAME, username=USER, password=PASSWORD) 
    
    stdin, stdout, stderr = ssh.exec_command('less ' + zipfilename) 
    for line in stdout.readlines(): 
        # here you will have your listing 
        print (line) 
    
    for errline in stderr.readlines(): 
        # Don't forget to check the error output 
        print ('***', errline) 
    

    행운을 비네!

    편집 같은 서버에 SFTP를 연결을해야하는 경우에는 불가능이

    sftp = ssh.open_sftp() 
    
  • +0

    내가 이것을 시도 할 때'paramiko.ssh_exception.SSHException : Channel closed .' 예외가 발생합니다. 어떻게 해결할 수 있습니까? –

    +0

    @Someone_Else : 알기 힘듭니다 .. 서버에서 ssh를 실행하고 있습니까? – Ward

    +0

    나는 그것이라고 생각 하느냐? nmap -p22 -Pn remotehost.com을 실행하면 (nmap -p22 remotehost.com은 알지 못하지만). –

    관련 문제