【发布时间】:2021-04-23 21:56:51
【问题描述】:
(我针对 cmets 对此进行了多次修改)
我的目标是有一个 shell 脚本去外部服务器,从目录中获取一批文件,在热敏收据打印机上打印它们,然后从远程服务器中删除相同的文件(这样当一个 cron作业运行,它们不会再次打印)。我的问题是将变量连接到 cURL 的 URL 末尾。脚本的其余部分正在运行,但为了清楚起见,我将显示整个脚本
我已经多次搜索解决方案,其中许多似乎涉及更复杂的情况,例如这个试图解决隐藏回车的问题,因为变量附加到 URL 的中间(c.f. Bash curl and variable in the middle of the 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
所有这四个的输出是:
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'
*** 编辑 ** 我重新阅读了 cmets,我想我最初误解了其中的一些。我能够在 curl 命令前面使用 echo 来查看带有变量的输出。这非常有帮助,谢谢@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
【问题讨论】:
-
请edit您的问题并添加更多详细信息:“没有一个有效”的确切含义是什么?显示确切的命令和相应的输出/结果/错误消息。要查看发生了什么,您可以在脚本中添加
set -x以打印命令或在curl之前插入echo,即类似for i in *.txt; do echo curl --ftp-ssl ... -
@Bodo - 我已经进行了请求的编辑。
-
在不同变体中显示代码的简短 sn-p 并没有多大帮助。显示每个单独的完整命令和相应的结果。您仍然没有明确“没有工作”的确切含义。以哪种方式不起作用?您收到错误消息吗?意想不到的结果?完全没有结果?尝试两种建议的方法来显示将/将要执行的命令。如果您没有发现问题,请将输出添加到问题中。
-
@Bodo - 我已经进行了请求的编辑。
-
详细输出显示
DELE /public_html/tickets/14.txt,后跟550 Could not delete /public_html/tickets/14.txt: No such file or directory。DELE协议命令看起来与我对带有硬编码文件名的curl命令的预期相同。此时文件是否存在?如果是,则可能某处存在隐形字符。您是否在终端和 Web 浏览器之间复制和粘贴代码和/或输出?根据所有四种情况的输出中的拼写错误,您可能重新输入了它,这可能会引入或隐藏错误。
标签: linux curl concatenation