如何透過兩個ssh進行scp

如何透過兩個ssh進行scp

我有三台計算機,分別是家庭電腦、工作計算機和伺服器計算機,我透過以下方式從家庭電腦連接到伺服器:

ssh -t <WORKusername>@<WORK> ssh -p23456 <SERVERusername>@localhost

我需要將檔案從 HOME 傳輸到伺服器 但我無法將文件留在工作

我還需要將檔案從伺服器傳輸到主目錄處理完之後,我該怎麼做呢?

我看過了https://serverfault.com/questions/37629/how-do-i-do-multihop-scp-transfers我的(第三台)電腦上的兩個遠端主機之間的 scp但我無法更改 ssh 配置機器

我已經嘗試過這個但我無處可去:

$ scp -o ProxyCommand="ssh <WORKusername>@<WORK> nc localhost" ASPEC.zip <SERVERusername>@localhost:~/
<WORKusername>@<WORK>'s password: 
This is nc from the netcat-openbsd package. An alternative nc is available
in the netcat-traditional package.
usage: nc [-46DdhklnrStUuvzC] [-i interval] [-P proxy_username] [-p source_port]
      [-s source_ip_address] [-T ToS] [-w timeout] [-X proxy_protocol]
      [-x proxy_address[:port]] [hostname] [port[s]]
ssh_exchange_identification: Connection closed by remote host
lost connection

我也嘗試過以下操作,但這與從 SERVER -> HOME 傳輸文件相反:

ssh -t <WORKusername>@<WORK> "ssh -p 23456 <SERVERusername>@localhost \"cat file.zip\""

我也嘗試過 tar 但它不起作用:

tar c file.zip | ssh -t <WORKusername>@<WORK> "ssh -p 23456  <SERVERusername>@localhost | tar x"
Pseudo-terminal will not be allocated because stdin is not a terminal.
<WORKusername>@<WORK>'s password: 
Pseudo-terminal will not be allocated because stdin is not a terminal.
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password).
tar: This does not look like a tar archive
tar: Exiting with failure status due to previous errors

然後我嘗試:

# From home:
$ ssh -L 23456:<SERVERusername>@localhost:22 <WORKusername>@<WORK>

# After ssh, in WORK:
$ ssh -p 23456 <SERVERusername>@localhost

# From home:
$ scp -P file.zip <SERVERusername>@localhost:~/

當我發出 scp 命令時,我收到了這些錯誤,

# on WORK terminal:
channel 3: open failed: administratively prohibited: open failed

# on SERVER terminal:
ssh_exchange_identification: Connection closed by remote host
lost connection

我也嘗試過 rsync 但它不起作用:

rsync -e "ssh -A -t <WORKusername>@<WORK>e ssh -p23456 <SERVERusername>@localhost" file.zip :~/

如何從 HOME 進行 scp 將檔案傳輸到伺服器?

還有從伺服器到家庭?

答案1

嘗試使用這個:

scp -P SERVERPORT -o 'ProxyCommand ssh -p WORKPORT WORKUSER@WORK nc %h %p 2>/dev/null' localfile SERVERUSER@SERVER:remotefile

ProxyCommand在連接到 SERVER:SERVERPORT 的 WORK 主機上建立代理程式(請參閱數控)。 %h%p是 ssh 變數 - 分別是目標主機和連接埠。

答案2

你可以嘗試這樣的事情:

cat localfile | ssh <WORKusername>@<WORK> "ssh -p23456 <SERVERusername>@localhost 'cat > remotefile'"

相關內容