2009-08-12 2 views
0

처리 할 파일의 디렉토리를 읽는 코드가 있습니다. 나는 현재 dirent.h, opendir(), readdir() 메소드를 사용하고있다. 나는 똑같은 일을해야한다는 말을 들었지만, 이제는 SSH/SFTP 액세스가있는 원격 컴퓨터의 디렉토리입니다.SSH/SFTP를 통해 opendir(), readdir() 유형의 호출을 수행하는 방법이 있습니까?

내 소스 코드를 제외한 시스템에서 아무 것도 변경하지 않고 동일한 작업을 수행하는 유일한 방법은 "ssh user @ host ls"명령을 실행하고 구문 분석하고 기반으로 처리하는 것입니다 파서 결과.

여기 실제 질문은 opendir(), readdir() (또는 비슷한 것)을 SSH/SFTP를 통해 수행하는 방법입니까?

답변

2

Linux 또는 Mac OS X (또는 일부 BSD)를 사용하는 경우 가장 쉬운 방법은 sshfs을 사용하여 파일 시스템에 연결하는 것입니다. 대부분의 파일 시스템 호출은 수정없이 바로 작동합니다.

그렇지 않으면 sshfs의 source에서 어떻게 작동하는지 힌트를 볼 수 있습니다.

1

예, SFTP에는이를위한 프로토콜이 있습니다. 여기에서 가장 최신 문서를 읽으십시오. http://tools.ietf.org/wg/secsh/draft-ietf-secsh-filexfer/

+0

어디서나 구현 되었습니까? –

+1

OpenSSH는 코드 안의 주석을 제외하고는 안정적인 API 나 API 문서가없는 것 같습니다. –

+1

sshfs는 별도의 프로세스로 ssh를 실행하고 파이프를 통해 원시 SFTP를 말하며 안정적인 API가 필요하지 않습니다. – bdonlan

0

OpenSSH 5.1부터는이를 수행하는 데 제한된 구현이 있습니다. File XFER 표준을 완전히 구현하지는 않습니다. 그래서 파일 유형과 같은 것을 검사 할 수는 없지만 파일 이름을 직접 스캔 할 수는 있습니다 ... OpenSSH 소스에서 "sftp.c"코드를 사용하면 필요에 따라 변경할 수있는 adhoc main()을 함께 던졌습니다. 나는이 질문이 나중에 더 대답 할 수 있기를 바랍니다.

/* Username is a NULL terminated string 
    Host is a NULL terminated string indicating remote IP */ 
void setup_args(arglist *args, char *username, char *host) 
{ 
     // Arg init allocation 
     memset(args, '\0', sizeof(*args)); 
     args->list = NULL; 

     // Arg setup 
     addargs(args, "%s", "/usr/bin/ssh"); 
     addargs(args, "%s", "-oForwardX11 no"); 
     addargs(args, "%s", "-oForwardAgent no"); 
     addargs(args, "%s", "-oPermitLocalCommand no"); 
     addargs(args, "%s", "-oClearAllForwardings yes"); 
     addargs(args, "%s%s", "-l", username); 
     addargs(args, "%s", "-oProtocol 2"); 
     addargs(args, "%s", "-s"); 
     addargs(args, "%s", host); 
     addargs(args, "%s", "sftp"); 
} 
void run_program() 
{ 
     // Setup connection parameters. 
     int in, out; // The IO file descriptors. 
     arglist args; 
     setup_args(&args, "chenz", "172.16.22.128"); 
     connect_to_server("/usr/bin/ssh", args.list, &in, &out); 
     freeargs(&args); 

     // Create connection 
     struct sftp_conn *conn; 
     conn = do_init(in, out, copy_buffer_len, num_requests); 
     if (conn == NULL) 
       fatal("Couldn't initialise connection to server"); 

     //Get the current directory 
     char *pwd; 
     pwd = do_realpath(conn, "."); 
     printf("PWD: %s\n", pwd); 
     if (pwd == NULL) 
       fatal("Need cwd"); 

     SFTP_DIRENT** dirent; 
     do_readdir(conn, "/home/chenz", &dirent); 
     int i = 0; 
     while (dirent[i] != 0) { 
       printf("filename: %s\n", dirent[i]->filename); 
       printf("longname: %s\n", dirent[i]->longname); 
       printf("attrib:\n", dirent[i]->a); 
       i++; 
     } 

     // Clean up file descriptors 
     close(in); 
     close(out); 

     // Wait for kill signal and exit 
     while (waitpid(sshpid, NULL, 0) == -1) 
       if (errno != EINTR) 
         fatal("Couldn't wait for ssh process: %s", 
          strerror(errno)); 

     //exit(err == 0 ? 0 : 1); 
     exit(0); 
}