在 Unix 上遠端運行腳本,在本地獲取輸出?

在 Unix 上遠端運行腳本,在本地獲取輸出?

我需要從(本機)Windows 命令 shell 在遠端 Unix 電腦上執行(Tcl 或其他)腳本,並且我希望腳本的 stdout/stderr 顯示在 Windows CLI 中。退出代碼也很有用。

使用 ssh (putty) 可以嗎?或透過任何其他方式?

答案1

這是 SSH 的預設值。如果要遠端重定向輸出,請將重定向符號(通常為“>”)放在命令引號內。

例如:

ssh remote_host "ls > /tmp/file_on_remote_host.txt"

為了將輸出保存在 Windows 主機本機,

ssh remote_host "ls" > .\file_on_local_host.txt

要遠端合併 stderr 並將其和 stdout 保存在本地,

ssh remote_host "ls 2>&1" > .\combined_output_on_local_host.txt

答案2

我正在尋找的是膩子的助手,執行程式。與 putty 不同,putty 的作用類似於終端仿真器,它plink的作用類似於普通(非互動式)CLI 程式。

使用它,我可以執行此操作cmd.exe並獲取本地保存的遠端命令輸出。

\> plink remote_host "ls -l" > log.txt

最重要的是,這適用於SSH遠端登入

以下是可用選項,僅供參考:

PuTTY Link: command-line connection utility
Release 0.59
Usage: plink [options] [user@]host [command]
       ("host" can also be a PuTTY saved session name)
Options:
  -V        print version information and exit
  -pgpfp    print PGP key fingerprints and exit
  -v        show verbose messages
  -load sessname  Load settings from saved session
  -ssh -telnet -rlogin -raw
            force use of a particular protocol
  -P port   connect to specified port
  -l user   connect with specified username
  -batch    disable all interactive prompts
The following options only apply to SSH connections:
  -pw passw login with specified password
  -D [listen-IP:]listen-port
            Dynamic SOCKS-based port forwarding
  -L [listen-IP:]listen-port:host:port
            Forward local port to remote address
  -R [listen-IP:]listen-port:host:port
            Forward remote port to local address
  -X -x     enable / disable X11 forwarding
  -A -a     enable / disable agent forwarding
  -t -T     enable / disable pty allocation
  -1 -2     force use of particular protocol version
  -4 -6     force use of IPv4 or IPv6
  -C        enable compression
  -i key    private key file for authentication
  -noagent  disable use of Pageant
  -agent    enable use of Pageant
  -m file   read remote command(s) from file
  -s        remote command is an SSH subsystem (SSH-2 only)
  -N        don't start a shell/command (SSH-2 only)
  -nc host:port
            open tunnel in place of session (SSH-2 only)

答案3

是的,如果您使用 putty ssh 用戶端,您將看到腳本將產生的任何命令列輸出。若要在腳本執行後取得退出程式碼,請輸入:

echo $?

答案4

還有一個答案:

將所有 linux 指令保存在cmdlist.txt文件並使用以下命令。

plink.exe -ssh -pw 密碼[電子郵件受保護] 輸出.txt

密碼==裝置登入密碼

烏納梅==裝置登入的使用者名稱

1.2.3.4==設備IP位址

執行此命令後,所有 cmdlist.txt 命令都將在遠端裝置上執行,並將輸出保存在名為 output.txt 的檔案中

確保將 plink.exe 和 cmdlist.txt 儲存在同一目錄中,並且將 dos 提示字元指向同一目錄。

相關內容