开发者

Escape characters in batch scripts

开发者 https://www.devze.com 2023-01-18 11:06 出处:网络
I have the following script to automate uploading a file to a remote server. The problem is that the password I have been given is full of special characters which are killing the login process.

I have the following script to automate uploading a file to a remote server. The problem is that the password I have been given is full of special characters which are killing the login process.

Here is a changed version of the characters:

j7~]%&X

When using a normal FTP application there isn't a problem. I have wrapped the password in quotes and have also tried using a ^ to escape the percentage and ampersand. However, it doesn't work. Also, I can't tell what the actual password is that is being sent.

What could be going wrong or how do I reveal the password befo开发者_运维知识库re it is sent?

:Source = http://www.howtogeek.com/howto/windows/how-to-automate-ftp-uploads-from-the-windows-command-line/

@echo off
echo user myloginname> ftpcmd.dat
echo mypassword>> ftpcmd.dat
echo bin>> ftpcmd.dat
echo put %1>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat myserver
del ftpcmd.dat

SOLUTION

As mentioned below, I used the "type" command to see what was actually in the ftpcmd.dat. It turns out that, through a lot of trial an error and reading up on escape characters that you use different escape characters for different special characters!!!

For an ampersand use a caret, and for the percentage use a percentage. So, given my password:

j7~]%&X

the resulting password would be:

j7~]%%^&X


In your case you could simply type your file to reveal the password, just before you delete the file.

But it's better to use the delayed expansion, because then the special characters lose their "special" behaviour, even carets and percent signs.

@echo off
setlocal EnableDelayedExpansion
set /p passwd=Enter pwd
echo user myloginname> ftpcmd.dat
echo mypassword>> ftpcmd.dat
echo bin>> ftpcmd.dat
echo put !passwd! >> ftpcmd.dat
echo quit>> ftpcmd.dat
type ftpcmd.dat
ftp -n -s:ftpcmd.dat myserver

The second problem is the ftp command, where the characters also need escaping. Perhaps (like Vicky wrote) the backslash could work. You can place it before each single character, by using a for-loop


FYI, character prefixing is done with ^. So if you were doing an echo with > in the string but writing a file, it will fail without escaping. The syntax would be:

echo ^<html^> >> foo.txt


FTP passwords are sent in plain text so installing something like Wireshark will allow you to see what is being sent.

I would expect you would need to prefix the special characters with a backslash.

0

精彩评论

暂无评论...
验证码 换一张
取 消