从VBA代码调用SFTP进程

时间:2012-01-02 14:15:37

标签: windows vba vb6 windows-xp sftp

问题 - 将密钥存储在缓存中问题停止VBA应用

我使用pscp.exe将文件从Windows传输到Linux机器。

备注:我的VBA代码中存在pscp.exe(该客户端是PuTTY工具的一部分)

当我将文件从我的PC复制到任何新的Linux机器时,我得到的问题是“将密钥存储在缓存中?(y / n)”这会中断我的VBA应用程序(VBA应用程序在sftp进程上停止)。

我需要建议如何忽略“在缓存中存储密钥?(y / n)”的问题。或者也许自动从我的VBA代码发送“y”键?或者在运行pscp.exe之前在PC注册表中定义的其他解决方案?但是怎么做呢?

WIN XP命令行(cmd)

的示例

备注:192.9.200.120(Linux服务器IP地址)

  

“D:\ documents and settings \ udavid \ pscp.exe”-sftp -l root -pw pass123   “D:\ Documents and Settings \ udavid \ Desktop \ scan_ip.ksh”192.9.200.120:/var/tmp

CMD输出:

The server's host key is not cached in the registry. You
have no guarantee that the server is the computer you
think it is.
The server's rsa2 key fingerprint is:
ssh-rsa 1024 15:e1:ce:4f:8e:4e:7b:61:14:c3:df:3c:b1:50:67:b6
If you trust this host, enter "y" to add the key to
PuTTY's cache and carry on connecting.
If you want to carry on connecting just once, without
adding the key to the cache, enter "n".
If you do not trust this host, press Return to abandon the
connection.
Store key in cache? (y/n)

如果我使用-batch flag(pscp.exe -batch)

,CMD输出示例
The server's host key is not cached in the registry. You
have no guarantee that the server is the computer you
think it is.
The server's rsa2 key fingerprint is:
ssh-rsa 2048 d5:80:49:7f:69:f1:29:7c:1f:99:ec:c9:f4:b2:6f:a0
Connection abandoned.
Lost connection

                  ---  Example from my VBA code ---
Const cstrSftp As String = """D:\documents and settings\udavid\pscp.exe"""
Dim strCommand As String
Dim pUser As String
Dim pPass As String
Dim pHost As String
Dim pFile As String
Dim pRemotePath As String
pUser = "root"
pPass = "pass123"
pHost = "xxx.xxx.xxx.xxx" xxx.xxx.xxx.xxx - any server new IP
pFile = """D:\Documents and Settings\udavid\Desktop\scan_ip.ksh"""
pRemotePath = "/var/tmp"

strCommand = cstrSftp & " -sftp -l " & pUser & " -pw " & pPass & " " & pFile & " " & pHost & ":" & pRemotePath

Debug.Print strCommand
Shell strCommand, 1

3 个答案:

答案 0 :(得分:0)

试试这个:

 echo y|"C:\Program Files\PuTTY\pscp.exe"  -sftp -l root -pw pass123 C:\scan_ip.ksh $server:/var/tmp

它会“按”y到您的程序标准输入。

答案 1 :(得分:0)

帮助(pscp /?)为您提供答案。 您应该通过-batch来禁用所有提示。

您可能还需要正确引用参数,以便值中的空格和引号不会破坏。

答案 2 :(得分:0)

您可以使用Shell "cmd /c echo y | " & strCommand, 1作为建议的快速黑客。

我使用自制的cExec类来管道输出并有选择地响应像这样的putty警告

With New cExec
    .Run "d:\downloads\pscp.exe", sParams, True
    Do While Not .AtEndOfOutput
        If InStr(.ReadLineOutput, "(y/n)") Then
            .WriteInput "y"
        End If
    Loop
End With