VBA Excel和Putty(FTP)-等待调用外壳程序和绕过主机项未缓存在注册表中



我的excel文件中有一个宏,它使用pscp(putty客户端)将文件上传到我的服务器。

这是代码

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.exe。我想绕过它。

我使用的不是WScript.Shell,而是

Call Shell(strCommand, vbNormalFocus)

使用Call Shell,操作系统不会询问是否运行pscp.exe,但问题是我无法知道进程何时通过

2) 当用户第一次运行命令时,putty会提示以下消息:

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

我需要它完全无提示、自动。

我试着这样使用"echo n":

strCommand="echo n|"&cstrSftp&"-sftp-l"&p用户&"-pw"&pPass&amp_"&p文件&"&pHost&":"&pRemotePath

但我得到的消息是"IWshShell3"失败了。

问题2解决了!

Echo是一个批处理文件命令,所以它需要一个包装器

此命令有效:

strCommand="cmd/c echo n|"&cstrSftp&"-sftp-l"&p用户&"-pw"&pPass&amp_"&p文件&"&pHost&":"&pRemotePath

最新更新