簡體   English   中英

如何通過SFTP將遠程服務器上LS的輸出傳輸到本地文件系統?

[英]How do I pipe the output of an LS on remote server to the local filesystem via SFTP?

我在命令行通過SFTP登錄了遠程服務器。 我所在的文件夾包含數十萬個文件。 我需要在文本文件中獲取這些文件的列表,以便我可以以編程方式訪問它們,因為沒有任何PHP SFTP客戶端能夠返回如此大的文件列表。

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

我沒有在此服務器上的寫訪問權限,因此我無法將輸出通過管道傳輸到遠程服務器上的文件。

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

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

在啟動ssh或sftp會話之前調用'script',它將捕獲所有終端輸出到本地磁盤。 輸入“退出”以完成捕獲。

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.

如果你遠程做到這一點,吳的答案是好的。 如果您登錄到遠程服務器並希望將文件發回給自己,那么這是另一個選項:

正確的答案在這里: http//scratching.psybermonkey.net/2011/02/ssh-how-to-pipe-output-from-local-to.html

your_command | ssh username@server "cat > filename.txt"

如果您有ssh訪問權限,那將非常簡單:

ssh user@server ls > foo.txt

否則,您只需將sftp的STDOUT和STDERR重定向到文件即可。 你必須盲目地輸入密碼和命令。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM