PuTTY/PSFTP 文件传输自动化的批处理文件 [英] Batch file for PuTTY/PSFTP file transfer automation

查看:19
本文介绍了PuTTY/PSFTP 文件传输自动化的批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件,用于通过 SFTP 将文件从本地 PC 移动到服务器.我的系统中安装了 PuTTY,批处理文件代码如下.

I have a batch file for moving file from my local PC to server through SFTP. I have PuTTY installed in my system and the batch file code follows.

cd C:Program Files (x86)PuTTY
psftp
open <IP>
<user>
<PW>
cd /home/irisuser/iris/integration/dls_dlsblr_dlschnn_in_msg/in
lcd d:
put log.sh
bye

当我在命令提示符中输入上面的代码时,它完美地工作.但是当我双击 .bat 文件并运行它时,它没有运行并要求输入用户名和密码.我的目标是自动化整个过程,我只需单击 .bat 文件即可运行它.但我无法实现它.任何想法或片段都会对我有所帮助.

The above code perfectly works when I type it in command prompt. But when I double click the .bat file and run it, it's not running and asking for username and password to be entered. My aim was to automate the whole thing and I need to run it by simply clicking the .bat file. But am not able to achieve it. Any ideas or snippets will help me.

推荐答案

您需要将 psftp 脚本(从 openbye 的行)存储到一个单独的文件中并使用 -b 开关将其传递给 psftp:

You need to store the psftp script (lines from open to bye) into a separate file and pass that to psftp using -b switch:

cd "C:Program Files (x86)PuTTY"
psftp -b "C:path	oscriptscript.txt"

参考:
https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter6.html#psftp-option-b

对于用户名+密码:由于您不能在批处理文件中使用 psftp 命令,出于同样的原因,您不能将用户名和密码指定为 psftp命令.这些是 open 命令的输入.虽然您可以使用 open 命令(open @)指定用户名,但不能以这种方式指定密码.这只能在 psftp 命令行上完成.那么在命令行上完成所有操作可能会更干净:

For username+password: As you cannot use psftp commands in a batch file, for the same reason, you cannot specify the username and the password as psftp commands. These are inputs to the open command. While you can specify the username with the open command (open <user>@<IP>), you cannot specify the password this way. This can be done on a psftp command line only. Then it's probably cleaner to do all on the command-line:

cd "C:Program Files (x86)PuTTY"
psftp -b script.txt <user>@<IP> -pw <PW>

并从 script.txt 中删除 open 行>.

And remove the open, <user> and <PW> lines from your script.txt.

参考:
https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter6.html#psftp-starting
https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter3.html#using-cmdline-pw

您在 atm 中所做的是在没有任何参数或命令的情况下运行 psftp.一旦你退出它(比如通过输入 bye),你的批处理文件会继续尝试运行 open 命令(和其他命令),这是 Windows shell 显然不理解的.

What you are doing atm is that you run psftp without any parameter or commands. Once you exit it (like by typing bye), your batch file continues trying to run open command (and others), what Windows shell obviously does not understand.

如果您真的想将所有内容保存在一个文件(批处理文件)中,您可以将命令写入 psftp 标准输入,例如:

If you really want to keep everything in one file (the batch file), you can write commands to psftp standard input, like:

(
    echo cd ...
    echo lcd ...
    echo put log.sh
) | psftp <user>@<IP> -pw <PW>

虽然这有副作用.例如,如果主机不知道 plink(例如,如果您第一次在新机器上或在另一个本地帐户下运行它,例如在任务计划程序下),则输入的第一行将作为对主机的响应键提示.除了 y/i/Enter 之外的任何东西都被解释为 n(只连接一次,不添加密钥到缓存),所以即使是 cd 命令.脚本的其余部分将失败,因为 cd 不会发生.

Though this has side effects. For example, if the host is not known to plink (like if you run it first time on a new machine or under another local account, for example under Task Scheduler), the first line of input will be taken as a response to the host key prompt. Anything except for y/i/Enter is interpreted as as n (connect just once, without adding the key to the cache), so even the cd command. And the rest of the script will fail as the cd does not happen.

这篇关于PuTTY/PSFTP 文件传输自动化的批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆