簡體   English   中英

將變量連接到 linux Z2591C98B70119FE62E91ZB 腳本 1E424FE62E91ZB98 的 cURL 中的 URL 末尾

[英]Concatenation of variable to end of URL in cURL for linux shell script

(我對此進行了多次編輯以回應評論)

我的目標是擁有一個 shell 腳本,該腳本進入外部服務器,從目錄中獲取一批文件,在熱敏收據打印機上打印它們,然后從遠程服務器中刪除相同的文件(這樣當 cron 作業運行時,它們不要再次打印)。 我的問題是將變量連接到 cURL 的 URL 末尾。 腳本的 rest 正在工作,但為了清楚起見,我將顯示整個腳本

我已經對解決方案進行了多次搜索,其中許多似乎涉及更復雜的情況,例如,由於變量附加到 URL 的中間(參見Bash ZF6E57C9DE7519E45FEB48ZF 和 55 中間的變量 Bash url )。 我嘗試了那個解決方案(和其他幾個),但它們沒有用。 我的情況更簡單(我認為)所以也許這些答案增加了不必要的復雜性,這就是我的問題? 無論如何...

這是包含敏感信息占位符的完整代碼:

!/bin/bash
# step 1 - change to the directory where we want to temporarily store tickets
cd Tickets
# step 2 - get all the tickets in the target directory on the external server and put them in the current temporary local directory
wget --secure-protocol TLSv1_2 --user=<placeholder> --password='<placeholder>d' ftps://<placeholder>/public_html/tickets/*.txt
# step 3 - print each of the tickets in the current temporary local directory
for i in *.txt
do lp $i
done
# step 4 - reach out to the target directory and delete each of the files that we previously downloaded (not the entire directory; might have new files)
for i in *.txt
do curl --verbose --ftp-ssl --user <placeholder>:<placeholder> 'ftp://<placeholder>/public_html/tickets' -Q "DELE /public_html/tickets/$i"
done
# empty the current local directory where we temporarily stored files during the execution of this script
for i in *.txt
do rm $i
done
# should be done now.

我在第 4 步中使用了以下所有變體:

for i in *.txt
do curl --ftp-ssl --user (myftpid):(mypasswd) ftp://(myhostname)/public_html/tickets/ -Q 'DELE /public_html/tickets/'$i
done
for i in *.txt
do curl --ftp-ssl --user (myftpid):(mypasswd) ftp://(myhostname)/public_html/tickets/ -Q 'DELE /public_html/tickets/'${i}
done
for i in *.txt
do curl --ftp-ssl --user (myftpid):(mypasswd) ftp://(myhostname)/public_html/tickets/ -Q 'DELE /public_html/tickets/'"$i"
done
for i in *.txt
do curl --ftp-ssl --user (myftpid):(mypasswd) ftp://(myhostname)/public_html/tickets/ -Q 'DELE /public_html/tickets/'"${i}"
done

Output 對於所有這四個是:

curl: (21) QUOT command filed with 550

通過測試,我能夠確認代碼在沒有變量的情況下工作:

curl --ftp-ssl --user <placeholder>:<placeholder> ftp://<placeholder>/public_html/tickets/ -Q 'DELE /public_html/tickets/14.txt'

*** 編輯 ** 我重新閱讀了評論,我想我最初誤解了其中的一些。 我能夠在 curl 命令前面使用 echo 來查看帶有變量的 output。 這非常有幫助,謝謝@Bodo。 @NationBoneless 對詳細標簽的建議也很有用,並產生了以下結果:

< 220-You are user number 1 of 50 allowed.
< 220-Local time is now 18:34. Server port: 21.
< 220-This is a private system - No anonymous login
< 220-IPv6 connections are also welcome on this server.
< 220 You will be disconnected after 30 minutes of inactivity.
AUTH SSL
< 500 This security scheme is not implemented
AUTH TLS
< 234 AUTH TLS OK.
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / <placeholder>
* Server certificate:
*  subject: CN=<placeholder>
*  start date: Nov 16 00:00:00 2020 GMT
*  expire date: Nov 16 23:59:59 2021 GMT
*  subjectAltName: host "<placeholder>" matched cert's "<placeholder>"
*  issuer: C=US; ST=TX; L=Houston; O=cPanel, Inc.; CN=cPanel, Inc. Certification Authority
*  SSL certificate verify ok.
USER <placeholder>
< 331 User <placeholder> OK. Password required
PASS <placeholder>
< 230 OK. Current restricted directory is /
PBSZ 0
< 200 PBSZ=0
PROT P
< 200 Data protection level set to "private"
PWD
< 257 "/" is your current location
* Entry path is '/'
DELE /public_html/tickets/14.txt
* ftp_perform ends with SECONDARY: 0
< 550 Could not delete /public_html/tickets/14.txt: No such file or directory
* QUOT command failed with 550
* Closing connection 0

我認為這樣的事情會奏效。 當我在本地測試它時, curl 命令似乎工作正常:

for i in *.txt
do curl --ftp-ssl --user (myftpid):(mypasswd) ftp://(myhostname)/public_html/tickets/ -Q "DELE /public_html/tickets/$i"
done

首先,我要感謝@Bodo 和@NationBoneless。 如果沒有他們的評論,我不會想到這一點。 非常感謝你們倆。

我的問題有兩個部分 - 一個是連接問題(我從一開始就知道),還有我如何為 curl 設置 URL 路徑(直到后來我才意識到)。

連接的正確形式是:

curl --ftp-ssl --user myID:mypassword 'ftp://host.mydomain.com' -Q "DELE /public_html/tickets/$i"

我在此過程中學到的一些要點可能對經驗同樣有限的其他人有用,包括:

  • 准確地嘗試評論者告訴你的嘗試,即使你認為你理解他們的推理並且你認為他們走錯了路——他們可能沒有錯。
  • 如果您計划在引號內包含變量,請對 shell 腳本使用雙引號。 單引號的行為非常不同,並且不能很好地處理變量。
  • echo是你的朋友; 它對調試非常有幫助,因為它向我表明,由於那些討厭的單引號,我的變量被解析為 $i 而不是 $i 的值。 謝謝@Bodo
  • --verbose標志非常有用。 它讓我看到 curl 正在連接到服務器,並且我的腳本失敗了,因為它找不到要刪除的文件。 我的 URL 路徑錯誤,我沒有意識到。 謝謝@NationBoneless
  • 我最初將完整路徑放在ftp:部分中,然后在-Q之后再次放置,但這不起作用。 經過多次試驗和錯誤,我得到它只使用ftp:hostname之前-Q和 rest 之后的路徑-Q
  • 下一部分可能不適用於其他任何人,但我正在使用的服務器正在使用 WHM/cPanel。 我嘗試連接的域實際上不是我在代碼中使用的域。 我必須連接到 host.myserverdomain.com (我在 WHM 中用於根管理員內容的域)而不是 ftp.thisprojectdomain.com (實際工作的域) 顯然 WHM/cPanel 可以通過多種方式設置,這就是我的數據中心設置我的方式。 不知道這有多普遍,但如果你正在撕毀你的頭發,請嘗試切換到 WHM 域。
  • 對於那些想知道為什么我的主腳本同時使用 wget 和 curl 的人; 我從 wget 開始,因為我認為我更熟悉它。 wget 無法刪除服務器上的文件。 我切換到 curl 來刪除文件,但從未將第一條語句切換到 curl 因為它有效。 可能有一種方法可以使用 curl 而不是 wget; 我把這個練習留給評論部分,因為我會堅持什么是有效的。

暫無
暫無

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

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