前幾天晚上做的惡夢至今仍揮之不去,我依然清楚地記得夢中那一行命令代碼以及手機上的神秘下載連結。
於是我今天又坐到了電腦前面,打算一探究竟,揭開夢中那個網站的神秘面紗。
前文參考:《僅一條命令實現文件傳輸,真實上演在公司 Ctrl+C 在家 Ctrl+V(上)》
文章連結:https://www.sysadm.cc/index.php/xitongyunwei/896-file-transfer-with-only-one-command-ctrl-c-in-company-and-ctrl-v-at-home-1
我試著回憶了一下,想起了那個名字,小心翼翼地在地址欄內輸入 transfer.sh ,然後輕輕地回車。
https://transfer.sh/沒想到很快打開了一個頁面簡單的網頁。
還真的有這樣一個網站啊!
雖然我已經做到了充分的心理準備,但多多少少還是有點小吃驚。
摸索了一會兒,我終於懂了,這個網站就是提供共享服務,專門用來傳輸文件的!
而令人稱奇的是,文件傳輸方式只是命令行,而且只用一條命令搞定一切。
嗯?用命令行方式來傳輸文件,有什麼用,不麻煩嗎?
當然有用了,在很多場景下,用命令行可比圖形操作可能來得更方便些!
比如,Linux 伺服器上想傳個文件到自己電腦上,那麼可以這樣做。
# 使用 cURL 上傳文件
$ curl --upload-file ./hello.txt https://transfer.sh/hello.txt
# ↓ 返回用於下載的連結信息
https://transfer.sh/RT9fzc/hello.txt然後我們再跑到自己的電腦上,輸入上面的下載連結即可獲取到這個文件了。
很顯然,在沒有圖形界面或是圖形操作不方便的情況下,使用命令方式傳文件也是完全可行的。
那這個 transfer.sh 能保證文件安全嗎,可以傳多大文件,又能保留多久呢?
經過我一番調查研究,現在我就將我了解到的內容和小夥伴們分享一下吧。
嗯,針對前面提到的這些問題,官網上也有說明,大概總結如下。
好吧,我們先來看看網頁上羅列的一些基於命令行的其他用法。
一些好玩的用法使用 Shell 函數上傳文件注意,以下命令行裡的 transfer 是個自定義函數,具體代碼後面就有。
# 使用 Shell 函數上傳文件
$ transfer hello.txt
##################################################### 100.0%
# ↓ 返回用於下載的連結信息
https://transfer.sh/i4Ejz8/hello.txt
1、在 Linux 上我們可以這樣寫 transfer 函數
在 .bashrc 或 .zshrc 中創建一個 Shell 函數。
# Add this to .bashrc or .zshrc or its equivalent
transfer(){ if [ $# -eq 0 ];then echo "No arguments specified.\nUsage:\n transfer <file|directory>\n ... | transfer <file_name>">&2;return 1;fi;if tty -s;then file="$1";file_name=$(basename "$file");if [ ! -e "$file" ];then echo "$file: No such file or directory">&2;return 1;fi;if [ -d "$file" ];then file_name="$file_name.zip" ,;(cd "$file"&&zip -r -q - .)|curl --progress-bar --upload-file "-" "https://transfer.sh/$file_name"|tee /dev/null,;else cat "$file"|curl --progress-bar --upload-file "-" "https://transfer.sh/$file_name"|tee /dev/null;fi;else file_name=$1;curl --progress-bar --upload-file "-" "https://transfer.sh/$file_name"|tee /dev/null;fi;}上面的代碼怎麼感覺亂亂的哈,其實它是將函數代碼寫成了一行。
嗯,空格和回車啥時候這麼值錢了?
反正將這些代碼複製粘貼後,保存並將文件名命名為 transfer 即可。
哦,對了,別忘記給它賦予執行權限哦!
$ sudo chmod +x transfer好了,現在就可以用這個 Shell 函數自由地上傳文件了。
# Now you can use transfer function
$ transfer hello.txt
2、Windows 上我們可以這樣寫 transfer 函數
Win10 已經自帶有 cUrl 命令了,所以可以直接用 cUrl 來傳輸文件。
不過也可以通過官方示例的代碼自定義 transfer 函數。
將下例代碼(第一行去掉)保存為一個批處理文件,比如 transfer.cmd 。
#Save this as transfer.cmd in Windows 10 (which has curl.exe)
@echo off
setlocal EnableDelayedExpansion EnableExtensions
goto main
:usage
echo No arguments specified. >&2
echo Usage: >&2
echo transfer ^<file^|directory^> >&2
echo ... ^| transfer ^<file_name^> >&2
exit /b 1
:main
if "%~1" == "" goto usage
timeout.exe /t 0 >nul 2>nul || goto not_tty
set "file=%~1"
for %%A in ("%file%") do set "file_name=%%~nxA"
if exist "%file_name%" goto file_exists
echo %file%: No such file or directory >&2
exit /b 1
:file_exists
if not exist "%file%\" goto not_a_directory
set "file_name=%file_name%.zip"
pushd "%file%" || exit /b 1
set "full_name=%temp%\%file_name%"
powershell.exe -Command "Get-ChildItem -Path . -Recurse | Compress-Archive -DestinationPath ""%full_name%"""
curl.exe --progress-bar --upload-file "%full_name%" "https://transfer.sh/%file_name%"
popd
goto :eof
:not_a_directory
curl.exe --progress-bar --upload-file "%file%" "https://transfer.sh/%file_name%"
goto :eof
:not_tty
set "file_name=%~1"
curl.exe --progress-bar --upload-file - "https://transfer.sh/%file_name%"
goto :eof
給定一些上傳條件# 最大下載數:1個,最大保留日期:5天
$ curl -H "Max-Downloads: 1" -H "Max-Days: 5" --upload-file ./hello.txt https://transfer.sh/hello.txt
# ↓ 返回用於下載的連結信息
https://transfer.sh/RT9fzc/hello.txt
# 下載這個文件
$ curl https://transfer.sh/RT9fzc/hello.txt -o hello.txt
同時上傳多個文件# 用 -F 指定多個文件
$ curl -i -F filedata=@/tmp/hello.txt -F filedata=@/tmp/hello2.txt https://transfer.sh/
# 下載的同時可打包成 zip 或 tar 文件
$ curl https://transfer.sh/(RT9fzc/hello.txt,i4Ejz8/world.txt).tar.gz
$ curl https://transfer.sh/(RT9fzc/hello.txt,i4Ejz8/world.txt).zip
在傳輸之前使用 gpg 加密文件# 使用 gpg 加密文件,密碼在 /tmp/hello.txt 中
$ cat /tmp/hello.txt|gpg -ac -o-|curl -X PUT --upload-file "-" https://transfer.sh/test.txt
# 下載並解密
$ curl https://transfer.sh/RT9fzc/test.txt|gpg -o- > /tmp/hello.txt
還可以上傳文件掃描病毒# 使用 Clamav 掃描病毒或惡意軟體
$ wget http://www.eicar.org/download/eicar.com
$ curl -X PUT --upload-file ./eicar.com https://transfer.sh/eicar.com/scan
# 將惡意軟體上載到 VirusTotal,返回永久連結
$ curl -X PUT --upload-file nhgbhhj https://transfer.sh/test.txt/virustotal
備份並加密 mysql 資料庫後再傳輸# 備份,加密並傳輸
$ mysqldump --all-databases|gzip|gpg -ac -o-|curl -X PUT --upload-file "-" https://transfer.sh/test.txt
帶上傳輸連結發送郵件(使用 Shell 函數)# Transfer and send email with link (uses shell function)
$ transfer /tmp/hello.txt | mail -s "Hello World" user@yourmaildomain.com
使用 Keybase.io# 從 keybase 導入密鑰
$ keybase track [them]
# 給接受方加密
$ cat somebackupfile.tar.gz | keybase encrypt [them] | curl --upload-file '-' https://transfer.sh/test.txt
# 解密
$ curl https://transfer.sh/RT9fzc/test.md |keybase decrypt
當然用 wget 上傳文件也是可以的,不一定要 curl# wget
$ wget --method PUT --body-file=/tmp/file.tar https://transfer.sh/file.tar -O - -nv
傳輸持續輸出的日誌文件# grep syslog for pound and transfer
$ cat /var/log/syslog|grep pound|curl --upload-file - https://transfer.sh/pound.log
用 Powershell 照樣可以玩上傳# Upload using Powershell
PS H:\> invoke-webrequest -method put -infile .\file.txt https://transfer.sh/file.txt
用 HTTPie 玩上傳# HTTPie
$ http https://transfer.sh/ -vv < /tmp/test.log
用第三方的 Python 客戶端上傳# transfersh-cli (https://github.com/tanrax/transfersh-cli)
$ transfersh photos.zip
# Uploading file
# Download from here: https://transfer.sh/RT9fzc/photos.zip
# It has also been copied to the clipboard!
傳輸前使用 openssl 加密文件# Encrypt files with password using openssl
$ cat /tmp/hello.txt|openssl aes-256-cbc -pbkdf2 -e|curl -X PUT --upload-file "-" https://transfer.sh/test.txt
# Download and decrypt
$ curl https://transfer.sh/RT9fzc/test.txt|openssl aes-256-cbc -pbkdf2 -d > /tmp/hello.txt
使用 Web 瀏覽器上傳文件最後,如果你感覺前面的好麻煩啊,那就打開瀏覽器,點擊 click to browse 後瀏覽文件即可,不過這就不是命令行方式了。
# 使用Web瀏覽器上傳文件
Drag your files here, or click to browse.寫在最後在現今的網絡時代,通常還有不少人用郵件甚至是U盤傳輸著文件。
當然在中國可能大家也用微信或其他的社交軟體充當文件傳輸工具。
可是如果你仔細觀察,就會發現這些東西的本質並不是用來傳文件的,而且體驗也不是很好。
比如,你要分享文件給多個人,但又想保密不讓更多的人知道,那麼 transfer.sh 就可以實現多人下載,並且只有知道密碼的人才能下載。
而郵件也好、微信也罷,好像沒有這麼靈活吧!
因此,如果你經常有文件傳輸的需求,而且對傳輸過程或都說效果還有點小要求,那麼可以來關注一下這個神秘的 transfer.sh 。
最後還要告訴小夥伴們一件事,這個 transfer.sh 還是個開源項目,下期有空的話,我也會嘗試搭建一個自己用的私人 transfer.sh ,到時會將這個過程分享給大家。
自己可以隨時 transfer.sh ,想想就很棒啊,小夥伴們趕快嘗試一下吧!
原文連結:https://www.sysadm.cc/index.php/xitongyunwei/897-file-transfer-with-only-one-command-ctrl-c-in-company-and-ctrl-v-at-home-2
掃碼關注@網管小賈,閱讀更多
網管小賈的博客 / www.sysadm.cc