- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了以下代码来尝试上传到两个不同的服务器,一个通过 ftp,另一个通过 sftp。
我想知道是否有更好的方法通过 SFTP 上传,因为我当前的方法如果任何部分失败都不会触发 FTP 错误。
我想一个解决办法,我想要的就是让他们两个将输出记录到一个文本文件中,然后从中我可以手动查看错误是什么,如果我想设置一个简单的读取日志,检查错误,如果 x 执行 y...
On Error GoTo Err_FTPFile
' UPLOAD FIRST FILE VIA FTP
'Build up the necessary parameters
sHost = "ftp.server.com"
sUser = "user@server.com"
sPass = "password"
sSrc = """" + Environ("TEMP") + "\" + file + ".txt" + """"
sDest = "/remote/folder/"
'Write the FTP commands to a file
iFNum = FreeFile
sFTPCmds1 = Environ("TEMP") & "\" & "FTPCmd1.tmp"
Open sFTPCmds1 For Output As #iFNum
Print #iFNum, "ftp"
Print #iFNum, "open " & sHost
Print #iFNum, sUser
Print #iFNum, sPass
Print #iFNum, "cd " & sDest
Print #iFNum, "put " & sSrc
Print #iFNum, "disconnect"
Print #iFNum, "bye"
Close #iFNum
'Upload the file
Shell Environ("WINDIR") & "\System32\ftp.exe -s:" & sFTPCmds1
Application.Wait (Now + TimeValue("0:00:10"))
' UPLOAD SECOND FILE VIA SFTP
'Build up the necessary parameters
sFTPDetails = "C:\psftp.exe -b C:\commands.tmp user@ex.server.com -pw password"
sSrc = """" + Environ("TEMP") + "\" + file + ".txt" + """"
sDest = "/remote/folder/"
'Write the FTP commands to a file
iFNum = FreeFile
sFTPCmds2 = sFolder & "\" & "commands.tmp"
Open sFTPCmds2 For Output As #iFNum
Print #iFNum, "cd " & sDest
Print #iFNum, "put " & sSrc
Print #iFNum, "quit"
Print #iFNum, "bye"
Close #iFNum
'Upload the file
Call Shell(sFTPDetails, vbNormalFocus)
Application.Wait (Now + TimeValue("0:00:10"))
Exit_FTPFile:
On Error Resume Next
Close #iFNum
'Delete the temp FTP command file
Kill sFTPCmds1
Kill sFTPCmds2
Kill Environ("TEMP") + file + ".txt"
GoTo ContinuePoint
Err_FTPFile:
Shell "C:\FailPushBullet.exe"
MsgBox Err.Number & " - " & Err.Description & " Failed.", vbOKOnly, "Error"
GoTo ContinuePoint
ContinuePoint:
' Do stuff
我理想地希望底部的 SFTP 能够像上面的 FTP 一样工作和运行。
我尝试了以下操作并且运行:
sClient = "C:\psftp.exe"
sArgs = "user@website.com -pw passexample -b C:\commands.tmp"
sFull = sClient & " " & sArgs
sSrc = """" + Environ("TEMP") + "\" + "test" + ".txt" + """"
sDest = "folder"
'Write the FTP commands to a text file
iFNum = FreeFile
sFTPCmds = "C:\" & "commands.tmp"
Open sFTPCmds For Output As #iFNum
Print #iFNum, "cd " & sDest
Print #iFNum, "put " & sSrc
Print #iFNum, "quit"
Print #iFNum, "bye"
Close #iFNum
'Upload the file
Call Shell(sFull, vbNormalFocus)
但是如果我将 sArgs 更改为 sArgs = "user@website.com -pw passexample -b C:\commands.tmp 1> log.txt"
它不会运行,它只是关闭而不执行任何操作。我认为 1> log.txt
应该将输出放入文件
最佳答案
是否需要使用 Putty?我推荐WinSCP用于 VBA 中的 FTP 操作。实际上有一个 .NET 程序集/COM 库可用于使用 VBA 轻松实现自动化(甚至比我下面的示例更容易)。也就是说,我的公司环境禁止用户安装 .NET/COM(有充分的理由),因此我编写了自己的代码,简化如下。
要使用下面的内容,请从上面的链接下载可移植可执行文件,因为您需要 WinSCP.com 来编写脚本。
此示例具有以下功能:
Sub上传FTP和SFTP数据:
Public Sub FTPUpload()
'Execute the upload commands
'Create the commands file
Dim ObjFSO As Object
Dim ObjFile As Object
Dim ObjShell As Object
Dim ErrorCode As Integer
Dim sTempDir As String
Dim sType As String
Dim sUser As String
Dim sPass As String
Dim sServer As String
Dim sHostKey As String
Dim file As String 'Using your variable name here.
Dim sLocal As String
Dim sRemote As String
Dim sWinSCP As String
''''''''''''''''''''''''''''''''''''''''''''
'Set FTP Options
''''''''''''''''''''''''''''''''''''''''''''
sTempDir = Environ("TEMP") & "\" 'Log/batch files will be stored here.
sType = "ftp://" 'Or use "sftp://"
sUser = "user"
sPass = "password"
file = "example.txt" 'Assuming you will set this earlier in your code
sServer = "ftp.server.com"
sLocal = Chr(34) & Environ("TEMP") & "\" & file & Chr(34) 'Note that I included the full filename in the file variable; change this as necessary.
sRemote = "/remote/folder"
sWinSCP = "C:\Path\To\WinSCP\WinSCP.com"
'''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''
'Create batch file and command script
'''''''''''''''''''''''''''''''''''''''''''''
On Error Resume Next
'Delete existing files
Kill sTempDir & "winscp.txt"
Kill sTempDir & "winscp.bat"
Kill sTempDir & "winscplog.xml"
Kill sTempDir & "winscplog.txt"
On Error GoTo 0
Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Set ObjFile = ObjFSO.CreateTextFile(sTempDir & "winscp.txt", True)
ObjFile.writeline "open " & sType & sUser & ":" & sPass & "@" & sServer & "/" & IIf(sHostKey <> vbNullString, " -hostkey=" & Chr(34) & sHostKey & Chr(34), vbNullString)
ObjFile.writeline "put " & sLocal & " " & sRemote
ObjFile.writeline "close"
ObjFile.writeline "exit"
ObjFile.Close
Set ObjFile = Nothing
Set ObjFile = ObjFSO.CreateTextFile(sTempDir & "\winscp.bat", True)
ObjFile.writeline sWinSCP & " /script=" & sTempDir & "winscp.txt /xmllog=" & sTempDir & "winscplog.xml /log=" & sTempDir & "winscplog.txt"
ObjFile.Close
Set ObjFile = Nothing
Set ObjFSO = Nothing
'''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''
'Execute batch file and process output log
'''''''''''''''''''''''''''''''''''''''''''''
Set ObjShell = VBA.CreateObject("WScript.Shell")
ErrorCode = ObjShell.Run(sTempDir & "\winscp.bat", 0, True)
Set ObjShell = Nothing
If CheckOutput(sTempDir) <> "All FTP operations completed successfully." Then
MsgBox CheckOutput(sTempDir)
ElseIf ErrorCode > 0 Then
MsgBox "Excel encountered an error when attempting to run the FTP program. Error code: " & ErrorCode
Else
MsgBox "All FTP operations completed successfully."
End If
'''''''''''''''''''''''''''''''''''''''''''''
'Done with the FTP transfer. If you want to SFTP transfer immediately thereafter, use the below code
''''''''''''''''''''''''''''''''''''''''''''
'Re-set FTP Options
''''''''''''''''''''''''''''''''''''''''''''
sType = "sftp://"
'sHostKey = "ssh-rsa 1024 9d:d9:e9:69:db:cf:9c:71:8d:cb:da:a5:cf:a7:41:a7" 'Set this if you have a hostkey that should be auto-accepted
'I assume all other options are the same, but you can change user, password, server, etc. here as well.
'Note that all code from here down is exactly the same as above; only the options have changed.
''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''
'Create batch file and command script
'''''''''''''''''''''''''''''''''''''''''''''
On Error Resume Next
'Delete existing files
Kill sTempDir & "winscp.txt"
Kill sTempDir & "winscp.bat"
Kill sTempDir & "winscplog.xml"
Kill sTempDir & "winscplog.txt"
On Error GoTo 0
Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Set ObjFile = ObjFSO.CreateTextFile(sTempDir & "winscp.txt", True)
ObjFile.writeline "open " & sType & sUser & ":" & sPass & "@" & sServer & "/" & IIf(sHostKey <> vbNullString, " -hostkey=" & Chr(34) & sHostKey & Chr(34), vbNullString)
ObjFile.writeline "put " & sLocal & " " & sRemote
ObjFile.writeline "close"
ObjFile.writeline "exit"
ObjFile.Close
Set ObjFile = Nothing
Set ObjFile = ObjFSO.CreateTextFile(sTempDir & "\winscp.bat", True)
ObjFile.writeline sWinSCP & " /script=" & sTempDir & "winscp.txt /xmllog=" & sTempDir & "winscplog.xml /log=" & sTempDir & "winscplog.txt"
ObjFile.Close
Set ObjFile = Nothing
Set ObjFSO = Nothing
'''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''
'Execute batch file and process output log
'''''''''''''''''''''''''''''''''''''''''''''
Set ObjShell = VBA.CreateObject("WScript.Shell")
ErrorCode = ObjShell.Run(sTempDir & "\winscp.bat", 0, True)
Set ObjShell = Nothing
If CheckOutput(sTempDir) <> "All FTP operations completed successfully." Then
MsgBox CheckOutput(sTempDir)
ElseIf ErrorCode > 0 Then
MsgBox "Excel encountered an error when attempting to run the FTP program. Error code: " & ErrorCode
Else
MsgBox "All FTP operations completed successfully."
End If
'''''''''''''''''''''''''''''''''''''''''''''
Exit_Upload:
On Error Resume Next
'Clean up (leave log files)
Kill sTempDir & "winscp.txt" 'Remove scripting commands (note: this file will contain the password)
Kill sTempDir & "winscp.bat" 'Remove batch file
'Clear all objects
Set ObjFSO = Nothing
Set ObjFile = Nothing
Set ObjShell = Nothing
Exit Sub
End Sub
检查输出日志并为用户返回消息的函数:
Private Function CheckOutput(sLogDir As String) As String
Dim ObjFSO As Object
Dim ObjFile As Object
Dim StrLog As String
'Open log file
Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Set ObjFile = ObjFSO.OpenTextFile(sLogDir & "winscplog.xml")
StrLog = ObjFile.readall
ObjFile.Close
Set ObjFile = Nothing
Set ObjFSO = Nothing
'Check log file for issues
If InStr(1, StrLog, "<message>Authentication failed.</message>") > 0 Then
CheckOutput = "The supplied password was rejected by the server. Please try again."
ElseIf InStr(1, StrLog, "<failure>") Then
If InStr(1, StrLog, "<message>Can't get attributes of file") > 0 Then
CheckOutput = "The requested file does not exist on the FTP server or local folder."
Else
CheckOutput = "One or more attempted FTP operations has failed."
End If
ElseIf InStr(1, StrLog, " <result success=" & Chr(34) & "false" & Chr(34)) > 0 Then
CheckOutput = "One or more attempted FTP operations has failed."
ElseIf InStr(1, StrLog, " <result success=" & Chr(34) & "true" & Chr(34)) = 0 Then
CheckOutput = "No FTP operations were performed. This may indicate that no files matching the file mask were found."
End If
'Enter success message or append log file details.
If CheckOutput = vbNullString Then
CheckOutput = "All FTP operations completed successfully."
Else
CheckOutput = CheckOutput & vbLf & vbLf & "Please see the below files for additional information. Note that passwords are not logged for security reasons." & _
vbLf & "Condensed log: " & sLogDir & "winscplog.xml" & vbLf & "Complete log: " & sLogDir & "winscplog.txt"
End If
Exit_CheckOutput:
On Error Resume Next
Set ObjFile = Nothing
Set ObjFSO = Nothing
Exit Function
End Function
注意:我使用的实际代码要详细得多,因为它允许比上传更多的 (S)FTP 操作,使用 FTP 类来利用对象,等等。我认为这有点超出了答案的范围,但如果有帮助,我很乐意发布。
关于vba - 通过 sFTP 和 FTP 使用 VBA 上传,记录输出以检测错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35457719/
如果您登录到 sftp 服务器, 用户名和密码是否安全发送? 或者您是否必须进行基于证书的身份验证以确保整个传输都是加密的? 如果这是依赖于客户端的,那么您知道 Tumbleweed 和 WinSCP
我们如何在不安装任何软件/工具的情况下验证服务器上是否已授予 SFTP 访问权限? 最佳答案 大多数服务器有 curl和 scp安装,您可以使用它登录到 SFTP 服务器。要使用 curl 测试您的凭
我需要为 SFTP 执行校验和(为了数据完整性)。我希望这可以在 SFTP 文件传输期间完成 - 我意识到这可能取决于产品(仅供引用:使用 CLEO VLTrader),但想知道这是否是惯例? 我也在
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 8年前关闭。 Improve this que
我目前有一个 SFTP 脚本,它通过每天在 cron 上运行的 SSH 将最新版本的文件上传到服务器。但是我总是收到这样的电子邮件(我将服务器更改为 example.com fyi) /etc/cro
是否有使用“sftp”将远程服务器上的远程文件移动到另一个目录(在同一远程服务器上)的单行程序? 最佳答案 对于sftp,它是rename。如果你要求调用 sftp,使用 bash 就像是 sftp
嗨,我需要使用 NetSuite sFtp 功能从保存的搜索和 ftp'it 创建一个文件到另一台服务器。我想知道返回的数据后如何创建文件并将其传递给 FTP 对象?它将是一种csv形式,我可以将
我在 SFTP 位置创建了一个目录,我想将文件从一个 SFTP 目录移动到另一个目录,但是 cp那里不支持命令。 我怎样才能做到这一点? 最佳答案 核心 SFTP 协议(protocol)不支持复制远
我正在努力解决的问题有点复杂。我先向您解释设置环境。 我正在使用 PHPStorm 来处理 Symony2 项目。 我的 Apache 托管在通过“部署工具”连接到 PHPStorm 的 Debian
使用FTP,一切正常...但是.. 当我使用 SFTP 时,它成功连接,甚至当我使用“上传按钮”时,它成功上传文件...但是当我编辑+保存文件,它开始自动上传更改时,它无法上传(红色消息:文件....
我想将文件从远程服务器复制到不同目录中。例如,我想一次运行这4个命令。 scp remote:A/1.txt local:A/1.txt scp remote:A/2.txt local:A/2.tx
我可以使用 ssh/sftp(不带 -b 选项)登录我的 Remote sftp root@192.168.7.2 但是当我尝试 sftp -b commands.tmp root@192.168.7
我正在使用 Airflow v1.9rc5 中的 sftp_operator。我试图通过 SFTP 获取两个文件,但我得到: 错误 - 无法连接到主机:192.168.56.101,错误:没有可用的身
我想将 .png 文件从我计算机上的目录传输到远程服务器上的目录。 我必须使用 SFTP 来保护文件和传输模式。而且我已经有一个 UNIX 脚本 (.ksh) 文件以正常模式复制文件。如何在 SFTP
我正在使用 JSch 作为 SFTP 客户端,现在由于某些安全限制,我需要启用被动模式。 但我找不到启用被动模式的方法。 有人可以告诉我该怎么做吗? 最佳答案 “被动模式”是 FTP 协议(proto
我在 DigitalOcean 上有一个 Droplet,并创建了 SSH key 。我的 SSH 和 SFTP 访问与 Mac OSX 终端和传输完美配合。 但由于某种原因,SFTP 无法与 PHP
最近,我正在使用 SharpSSH 来处理远程文件。 SharpSSH 提供了用于删除远程目录的 rmdir API,但我发现当目录不为空时它会抛出异常并显示消息“Permission denied”
尝试通过 SFTP 连接到另一个集群中的文件,但没有任何效果。 Current spark version is : '2.2.0.2.6.4.0-91 . scala :2.11.8' . 下面是数
我已经使用 Package Installer (control+shift+p) 安装了 SFTP,但是现在当我点击“添加新服务器”时,无论我是哪个文件,它都只是添加了代码/json,我必须输入我的
我使用 DSL 配置和 spring。 我的路线如下: @Component public class UploadRoutesDefinition extends RouteBuilder {
我是一名优秀的程序员,十分优秀!