Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 使用批处理文件运行Unix shell脚本以从WinSCP下载文件_Batch File_Unix_Sftp_Winscp - Fatal编程技术网

Batch file 使用批处理文件运行Unix shell脚本以从WinSCP下载文件

Batch file 使用批处理文件运行Unix shell脚本以从WinSCP下载文件,batch-file,unix,sftp,winscp,Batch File,Unix,Sftp,Winscp,我正试图通过批处理文件从WinSCP下载一个文件。如果在批处理文件中输入文件名,则可以下载该文件 但我需要动态输入文件名(即,必须在运行时输入文件名) 这是我的密码 cd\Users\Desktop\WinSCP winscp.com/ini=null/script=C:\Users\Desktop\test.txt 打开sftp://username:password@主机名/ $ssh-o“StrictHostKeyChecking=yes”username@hostname 光盘/日志 获

我正试图通过批处理文件从WinSCP下载一个文件。如果在批处理文件中输入文件名,则可以下载该文件

但我需要动态输入文件名(即,必须在运行时输入文件名)

这是我的密码

cd\Users\Desktop\WinSCP
winscp.com/ini=null/script=C:\Users\Desktop\test.txt
打开sftp://username:password@主机名/
$ssh-o“StrictHostKeyChecking=yes”username@hostname
光盘/日志
获取test.log C:\Users\Desktop\Downloading\u日志\
这里
Test.log
是我在批处理文件中提供的文件名

请建议一种动态输入文件名的方法

提前感谢。

为批处理文件使用参数

代码(带参数bat的脚本):

@echo关闭
setLocalEnableExtensions
如果“%~1”相等(
echo“请提供文件名”
后藤:eof
)
cd\Users\Desktop\WinSCP
winscp.com/ini=null/script=C:\Users\Desktop\test.txt
打开sftp://username:password@主机名/
$ssh-o“StrictHostKeyChecking=yes”username@hostname
光盘/日志
获取“%~1”C:\Users\Desktop\Downloading\u日志\
回音“完成”
注释

  • 这是最简单的方法
  • 该参数被称为
    “%~1”
    。查看更多详细信息
  • 开头的
    if
    子句用于验证是否提供了参数,而不是简单地显示错误消息并退出
  • 在处理路径时,最好引用它们,因为路径中的空间可能会完全把事情搞砸
其他途径:

  • 使用从脚本外部设置的环境变量(而不是参数)
  • 让用户从脚本中输入文件名(通过
    set/p
    ),正如@ramu246的评论所指出的(我错过了这个:)
@EDIT0

  • 在看了@MartinPrikryl的评论并做了一些测试之后,发现你的.bat文件完全是垃圾(我的也是,因为它是基于你的)
  • 但是,即使修复不起作用,修复仍然可以(因为我盲目地应用了它-检查前面的项目)
我做了一些更改,下面是一个真正有效的版本(当然敏感数据已经更改)

script.bat:

@echo关闭
如果“%~1”相等(
echo“请提供文件名”
后藤:eof
)
winscp.com/ini=winscp_cfg.ini/script=winscp_script.txt/参数“%~1”
回音“完成”
winscp_script.txt:

echo收到的参数:“%1%”
打开sftp://cfati:password@127.0.0.1:22001/
cd/tmp
获取“%1%”\%1%
出口
输出

注释

  • 这次我用的是我自己的路
  • .ini文件(winscp_cfg.ini)是传递主机指纹所必需的。也可以为
    open
    命令()传递
    -hostkey
    参数,但我没有成功(我也没有尝试太多)
    • 正如您在输出中所看到的,第一次需要用户确认(
      (Y)es,(N)o,C(a)ncel…
      ),以便生成文件,下一次它只是使用它
    • 同样的事情也发生在你身上(我假设你想跳过.ini文件名),但由于一个错误:Win中Ux的/dev/null等价物是nul单l),所以你的案例中的winscp_cfg.ini是一个名为null的文件

非常感谢您的快速回复。我还尝试了这个选项set/p id=“Enter syslog name:”echo%id%cd\Users\Desktop\WinSCP.com/ini=null/script=C:\Users\Desktop\test.txt opensftp://username:password@hostname/$ssh-o“StrictHostKeyChecking=yes”username@hostnamecd/log获取%id%C:\Users\Desktop\Downloading\u logs\t另一种方式对Metat有效。我不知道我是怎么忘记这个的。非常感谢你的建议记住,尽管这可能是最简单的方法,
set/p
方法不可伸缩,如果你需要在一个文件(或文件列表)上多次运行脚本,每次输入文件名都很容易变得烦人(我建议每天20次),而如果从控制台运行,只需按键盘上的向上箭头,就可以运行上一个命令。此外,如果要自动化内容,用户输入是不可能的。因此,我建议使用该参数(您可以创建一个等待用户输入的包装器,但核心实现应该参数化).大家好…我有另一个与此相关的问题。上面的脚本正在我的计算机中运行,它使用我的ID和密码。我不想将此bat文件共享给我的团队。但是他们使用此批处理文件来获取文件。类似于从他们的计算机,他们必须提供服务器名和系统日志文件名,然后他们应该能够下载文件我有一个选项加密方法。有没有其他方法可以做到这一点批处理文件简直是胡说八道。
打开
cd
获取
应该在
测试.txt
中,而不是批处理文件中。什么是
$ssh
e:\Work\Dev\StackOverflow\q047989047>where winscp
c:\Install\x86\WinSCP\WinSCP\AllVers\WinSCP.com
c:\Install\x86\WinSCP\WinSCP\AllVers\WinSCP.exe

e:\Work\Dev\StackOverflow\q047989047>dir /b
script.bat
winscp_script.txt

e:\Work\Dev\StackOverflow\q047989047>script.bat "test with spaces.log"
Received argument: "test with spaces.log"
Searching for host...
Connecting to host...
Authenticating...
Continue connecting to an unknown server and add its host key to a cache?

The server's host key was not found in the cache. You have no guarantee that the server is the computer you think it is.

The server's Ed25519 key details are:

    Algorithm:  ssh-ed25519 256
    SHA-256:    M/iFTnSbi0k4VEcd8I75GiO7t6gza6RL99Pkh+bz4AQ=
    MD5:        8f:2f:f0:4a:ed:41:aa:e6:61:fa:5d:1f:f4:5b:c0:37

If you trust this host, press Yes. To connect without adding host key to the cache, press No. To abandon the connection press Cancel.
In scripting, you should use a -hostkey switch to configure the expected host key.
(Y)es, (N)o, C(a)ncel (8 s), (C)opy Key, (P)aste key: Yes
Using username "cfati".
Authenticating with pre-entered password.
Authenticated.
Starting the session...
Session started.
Active session: [1] cfati@127.0.0.1
/tmp
test with spaces.log      |            6 B |    0.0 KB/s | binary | 100%
"Done"

e:\Work\Dev\StackOverflow\q047989047>dir /b
script.bat
test with spaces.log
winscp_cfg.ini
winscp_script.txt

e:\Work\Dev\StackOverflow\q047989047>del /q "test with spaces.log"

e:\Work\Dev\StackOverflow\q047989047>dir /b
script.bat
winscp_cfg.ini
winscp_script.txt

e:\Work\Dev\StackOverflow\q047989047>script.bat "test with spaces.log"
Received argument: "test with spaces.log"
Searching for host...
Connecting to host...
Authenticating...
Using username "cfati".
Authenticating with pre-entered password.
Authenticated.
Starting the session...
Session started.
Active session: [1] cfati@127.0.0.1
/tmp
test with spaces.log      |            6 B |    0.0 KB/s | binary | 100%
"Done"

e:\Work\Dev\StackOverflow\q047989047>dir /b
script.bat
test with spaces.log
winscp_cfg.ini
winscp_script.txt