2017-10-17 62 views
0

我在我的excel文件中使用pscp(腻子客户端)上传文件到我的服务器的宏。VBA Excel和腻子(FTP) - 等待呼叫壳牌和旁路主机密钥没有缓存在注册表中

这是代码

Dim wsh As Object 
Set wsh = VBA.CreateObject("WScript.Shell") 
Dim waitOnReturn As Boolean: waitOnReturn = True 
Dim windowStyle As Integer: windowStyle = 1 

Dim cstrSftp As String 
Dim strCommand As String 
Dim pUser As String 
Dim pPass As String 
Dim pHost As String 
Dim pFile As String 
Dim pRemotePath As String 

'Specifying the values 
cstrSftp = """" & Application.ActiveWorkbook.Path & "\pscp.exe" & """" 
pUser = "username" 
pPass = "password " 
pHost = "ftp.xyz.com" 
pFile = """" & Application.ActiveWorkbook.Path & "\test.mkv" & """" 
pRemotePath = "/home/storage/public_html/" 

'Setting the command 
strCommand = cstrSftp & " -sftp -l " & pUser & " -pw " & pPass & _ 
    " " & pFile & " " & pHost & ":" & pRemotePath 

'Execution 
wsh.Run strCommand, windowStyle, waitOnReturn 

这工作得很好,但我有两个问题:

1)使用Windows 10系统弹出一个消息,询问到,如果他想运行PSCP用户。可执行程序。我想绕过这一点。

相反WScript.Shell的我就是用这个:

Call Shell(strCommand, vbNormalFocus) 

使用呼叫壳牌操作系统不问有关运行pscp.exe,但问题是我没有办法知道什么时候该过程结束

2)当用户运行首次命令,腻子提示此消息:

服务器的主机密钥未在注册表中缓存。您没有 保证服务器是您认为它的计算机。 服务器的rsa2密钥>指纹为:[ssh-rsa 1024 somekey]如果您信任此主机,请输入“y”将密钥添加到PuTTY的缓存,并在连接时携带 。如果您只想继续连接一次,而没有 将密钥添加到缓存中,请输入“n”。如果您不信任此主机,请按回车键放弃连接。将密钥存储在缓存中? (y/n)

我需要它是完全免提的,自动的。

我试图用 “回声N” 是这样的:

strCommand = “回声N |” & cstrSftp & “-sftp -l” & pUser & “-pw” & pPass & _ “” & PFILE & “” & pHost & “:” & pRemotePath

但我得到的消息 “IWshShell3” 失败。

+0

这是两个不同的问题。所以请单独询问。 –

+1

[使用echo y作为pcp hostkey提示符的自动响应](https://stackoverflow.com/questions/39924091/using-echo-y-as-an-automated-response-to-a-pcp -hostkey-prompt) –

回答

-1

问题2解决了!

回声是一个批处理文件命令,所以它需要有一个包装它。

此命令的作用:

strCommand = “CMD/C回波N |” & cstrSftp & “-sftp -l” & pUser & “-pw” & pPass & _ “” & PFILE & “” & pHost &“:”& pRemotePath

+0

千万不要这样做!提示是有原因的。主机密钥必须经过验证。 –