2013-01-04 115 views
4

我通過命令行通過SFTP登錄到遠程服務器。我所在的文件夾包含數十萬個文件。我需要在文本文件中獲得這些文件的列表,以便我可以以編程方式訪問它們,因爲沒有一個PHP SFTP客戶端能夠返回如此龐大的文件列表。如何通過SFTP將遠程服務器上的LS輸出傳輸到本地文件系統?

當我在目錄上運行ls(在SFTP會話中)時,文件列表最終顯示大約需要20分鐘。

我對此服務器沒有寫入權限,所以無法將輸出傳送到遠程服務器上的文件。

如何將輸出傳輸到本地計算機上的文本文件中......或者以其他方式獲取文件列表到我的本地計算機?

回答

4

如果您願意等待20分鐘的數據在屏幕上滾動,則可以使用「腳本」捕獲所有輸出。

在開始你的ssh或sftp會話之前調用'腳本',它將捕獲所有終端輸出到你的本地磁盤。輸入'exit'結束捕獲。

NAME 
    script -- make typescript of terminal session 

SYNOPSIS 
    script [-akq] [-t time] [file [command ...]] 

DESCRIPTION 
    The script utility makes a typescript of everything printed on your ter- 
    minal. It is useful for students who need a hardcopy record of an inter- 
    active session as proof of an assignment, as the typescript file can be 
    printed out later with lpr(1). 

    If the argument file is given, script saves all dialogue in file. If no 
    file name is given, the typescript is saved in the file typescript. 

    If the argument command is given, script will run the specified command 
    with an optional argument vector instead of an interactive shell. 

    The following options are available: 

    -a  Append the output to file or typescript, retaining the prior con- 
      tents. 

    -k  Log keys sent to program as well as output. 

    -q  Run in quiet mode, omit the start and stop status messages. 

    -t time 
      Specify time interval between flushing script output file. A 
      value of 0 causes script to flush for every character I/O event. 
      The default interval is 30 seconds. 

    The script ends when the forked shell (or command) exits (a control-D to 
    exit the Bourne shell (sh(1)), and exit, logout or control-D (if 
    ignoreeof is not set) for the C-shell, csh(1)). 

    Certain interactive commands, such as vi(1), create garbage in the type- 
    script file. The script utility works best with commands that do not 
    manipulate the screen. The results are meant to emulate a hardcopy ter- 
    minal, not an addressable one. 

ENVIRONMENT 
    The following environment variable is utilized by script: 

    SHELL If the variable SHELL exists, the shell forked by script will be 
      that shell. If SHELL is not set, the Bourne shell is assumed. 
      (Most shells set this variable automatically). 

SEE ALSO 
    csh(1) (for the history mechanism). 

HISTORY 
    The script command appeared in 3.0BSD. 

BUGS 
    The script utility places everything in the log file, including linefeeds 
    and backspaces. This is not what the naive user expects. 

    It is not possible to specify a command without also naming the script 
    file because of argument parsing compatibility issues. 

    When running in -k mode, echo cancelling is far from ideal. The slave 
    terminal mode is checked for ECHO mode to check when to avoid manual echo 
    logging. This does not work when in a raw mode where the program being 
    run is doing manual echo. 
+1

這個工作太好了 - '腳本script.log'會啓動腳本會話並將輸出存儲到名爲'script.log'的文件中。 –

2

如果你有ssh訪問,這將是非常容易的:

ssh [email protected] ls > foo.txt 

否則,你可以重定向SFTP的STDOUT和STDERR到一個文件中。你必須盲目地輸入密碼和命令。

相關問題