在 Windows 批处理文件中控制 psftp

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/694203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:12:17  来源:igfitidea点击:

Controlling psftp in a Windows Batch File

windowsbatch-file

提问by Brad

How do I write a batch file to control psftp, but with a dynamic filename? Overall I'm trying to convert a video file and then upload it to my Apple TV. Here's what I have, it generally works, but the commands don't control psftp, psftp just waits for user input:

如何编写一个批处理文件来控制 psftp,但具有动态文件名?总的来说,我正在尝试转换视频文件,然后将其上传到我的 Apple TV。这是我所拥有的,它通常可以工作,但是命令不控制 psftp,psftp 只是等待用户输入:

echo Convert and Upload to Apple TV file Called %1.mkv

ffmpeg -i %1.mkv -vcodec mpeg4 -acodec ac3 -ab 384k -sameq -s hd720 -t 60 %1.avi

psftp [email protected] -pw aaa
cd downloads/boxee
put %1.avi
quit

I know with the -b flag psftp can call it's own batch file, but I don't know how to get the %1 argument to it. I've seen solutions where a text file is redirected to psftp, but that suffers from the same problem. Also, I'd prefer to have just one file, but having to call a second file would be alright too.

我知道使用 -b 标志 psftp 可以调用它自己的批处理文件,但我不知道如何获取 %1 参数。我见过将文本文件重定向到 psftp 的解决方案,但也遇到了同样的问题。另外,我更喜欢只有一个文件,但也可以调用第二个文件。

回答by Brad

I ended up creating a new batch file from the main one that I then told psftp to use:

我最终从主文件创建了一个新的批处理文件,然后我告诉 psftp 使用:

echo cd downloads/boxee > psftp.bat
echo put "%1.avi" >> psftp.bat
echo quit >> psftp.bat

psftp [email protected] -pw aaa -b psftp.bat

回答by Lukas

Specify a file containing batch commands

指定包含批处理命令的文件

In normal operation, PSFTP is an interactive program which displays a command line and accepts commands from the keyboard.

在正常操作中,PSFTP 是一个交互式程序,它显示命令行并接受来自键盘的命令。

If you need to do automated tasks with PSFTP, you would probably prefer to specify a set of commands in advance and have them executed automatically. The -b option allows you to do this. You use it with a file name containing batch commands. For example, you might create a file called myscript.scr containing lines like this:

如果您需要使用 PSFTP 执行自动化任务,您可能更愿意提前指定一组命令并让它们自动执行。-b 选项允许您执行此操作。您可以将它与包含批处理命令的文件名一起使用。例如,您可以创建一个名为 myscript.scr 的文件,其中包含如下几行:

cd /home/ftp/users/jeff
del jam-old.tar.gz
ren jam.tar.gz jam-old.tar.gz
put jam.tar.gz
chmod a+r jam.tar.gz
quit

and then you could run the script by typing

然后你可以通过键入来运行脚本

psftp user@hostname -b myscript.scr

Credit to http://the.earth.li/~sgtatham/putty/0.52/htmldoc/Chapter6.html#6.1.3

归功于http://the.earth.li/~sgtatham/putty/0.52/htmldoc/Chapter6.html#6.1.3

回答by alepi

All in one file:

全部在一个文件中:

@echo off
echo Convert and Upload to Apple TV file Called %1.mkv
ffmpeg -i %1.mkv -vcodec mpeg4 -acodec ac3 -ab 384k -sameq -s hd720 -t 60 %1.avi
(
echo cd downloads/boxee
echo put %1.avi
echo quit
) | psftp [email protected] -pw aaa -bc

If we need the precise port number:

如果我们需要精确的端口号:

psftp [email protected] -P 222 -pw aaa -bc

回答by Shay Erlichmen

Why not use the built in FTP command that comes with windows?

为什么不使用windows自带的内置FTP命令呢?

you will need to write a script that will upload the file:

您需要编写一个脚本来上传文件:

open 192.168.1.50
user
frontrow aaa
put file.avi
quit

open 192.168.1.50
user
frontrow aaa
put file.avi
quit

then call ftp-s:MyScript

然后调用ftp-s:MyScript

You will need to generate the script per each file using echo and the >> redirector.

您需要使用 echo 和 >> 重定向器为每个文件生成脚本。

回答by Clay

If all you're doing with ftp is uploading a single file, you can use pscp.

如果您使用 ftp 所做的只是上传单个文件,则可以使用pscp

echo Convert and Upload to Apple TV file Called %1.mkv

ffmpeg -i %1.mkv -vcodec mpeg4 -acodec ac3 -ab 384k -sameq -s hd720 -t 60 %1.avi
pscp -pw aaa -sftp %1.avi [email protected]:/downloads/boxee

回答by descomputer

I was struggling to run a simple batch file/script to have an end user upload a file she had just edited to a secure FTP site.

我正在努力运行一个简单的批处理文件/脚本,让最终用户将她刚刚编辑的文件上传到安全的 FTP 站点。

Using This Script:

使用这个脚本:

cd /outgoing
put Examplefile.txt
# chmod a+r Examplefile.txt
# ren Examplefile.txt Exam.ted
# rm Examplefile.txt
quit

and running PuTTY's sftp command from a standard Windows command prompt:

并从标准 Windows 命令提示符运行 PuTTY 的 sftp 命令:

psftp [email protected] -pw password -b testscript.scr

I was able to quickly and (more importantly!!) easily upload a file from the company I am doing work for to a company we needed to transact business with. You can also see I am ready to rename the file if needed (ren) or delete a file if needed (rm). The hash symbol (#) sets the lines as remark lines.

我能够快速且(更重要的是!!)轻松地将我工作的公司的文件上传到我们需要与之开展业务的公司。您还可以看到我已准备好在需要时重命名文件 ( ren) 或在需要时删除文件 ( rm)。哈希符号 ( #) 将这些行设置为备注行。

This allowed me to create a complete batch file in Windows that the end user could simply click on to upload the files as she generated them. It was MUCH simpler than using some of the other "flavors" of sftp I found on the 'Net and I have used and trusted PuTTY for decades.

这使我能够在 Windows 中创建一个完整的批处理文件,最终用户可以在生成文件时简单地单击以上传文件。它比使用我在 'Net 上发现的其他一些“口味”的 sftp 简单得多,几十年来我一直使用并信任 PuTTY。