我试图使用Plink(putty命令行)从VBA调用一组Unix命令,但是这些命令没有得到执行。 我将张贴代码任何更正或build议将有所帮助。
替代的想法也欢迎,我所要做的就是访问unix文件更改访问权限,并将文件移动到其他文件夹。
请find下面的代码
Public Sub Chgaccper() Dim vPath As String Dim vFile As String Dim vSubpath As String Dim vscript As String Dim fNum As Long Dim oShell Set fso = CreateObject("scripting.filesystemobject") vPath = ThisWorkbook.Path 'Mounting file command for ftp.exe fNum = FreeFile() Open vPath & "Chg.txt" For Output As #1 Print #1,"c:" Print #1,"set PATH=" & vPath & ";%PATH% " Print #1," " Print #1,"plink server Name -l uname -pw Password " Print #1,"cd /root/home/temp " Print #1,"chmod 666 *.csv " Print #1,"cd /root/home/temp1 " Print #1,"exit " Print #1," " Close #1 vscript = "" & vPath & "Chg.txt" If fso.FolderExists("C:WindowsSystem32") = False Then Shell "C:WINNTsystem32cmd.exe -s:" & vscript & "" Else Shell "C:WINDOWSsystem32cmd.exe -s:" & vscript & "" End If SetAttr vPath & "Chg.txt",vbnormal Kill vPath & "Chg.txt" End Sub
如何检查excel工作簿目前是否在MAC或Windows上工作
VBA userform工具箱附加控件对话窗口不显示
如何select列中的多个单元格
macros在Windows上正常工作,但在Mac上失败
如何将此代码从Visual Basic转换为Perl?
使用VBA将linux文本文件加载到excel中
传输特定于Windows的macros以在Mac Excel上运行
如何使WinHttpCrackUrl在64位工作
你可以用VBA返回磁盘的卷GUID吗?
一种选择是在WScript.Shell打开plink会话,而不是使用VBA的Shell来执行脚本文件。 plink程序将从命令行以交互模式运行, WshExec对象可以让您直接访问正在执行的进程的标准输入和标准输出流。 这个简短的例子演示了如何交互使用它(它登录到公共telehack.com远程登录服务器并执行fnord命令),所有控制台输出都被复制到直接窗口中:
Private Sub Fnord() Dim shell As Object Set shell = CreateObject("WScript.Shell") Dim console As Object 'Open plink in interactive mode. Set console = shell.Exec("c:puttyplink -telnet telehack.com -P 443") 'Wait for a command prompt. WaitForResponseText console,"." 'Send the fnord command to standard input. console.StdIn.Write ("fnord" & vbCr) 'Wait for the server to echo it back. WaitForResponseText console,".fnord" 'Read the standard output through the next command prompt. WaitForResponseText console,"." 'Exit the telent session. console.StdIn.Write ("exit" & vbCr) End Sub Private Sub WaitForResponseText(console As Object,response As String) Dim out As String 'Make sure there's output to read. If console.StdOut.AtEndOfStream Then Exit Sub Do 'Read a line from standard output. out = console.StdOut.ReadLine() 'Not strictly required,but allows killing the process if this doesn't exit. DoEvents 'Send the server output to the immediate window. Debug.Print out 'Check for the response we're waiting for. If InStr(out,response) Then Exit Do End If Loop Until console.StdOut.AtEndOfStream End Sub
在你的情况下,你所连接的服务器没有太多的“交互”,所以它可能就像直接把所有的命令直接发送到StdIn 。 鉴于plink的协议支持范围广泛,如果运行脚本文件与以下内容大相径庭,我会感到惊讶:
Public Sub Chgaccper() Dim shell As Object Set shell = CreateObject("WScript.Shell") Dim console As Object 'Open plink in interactive mode. Set console = shell.Exec("c:puttyplink server Name -l uname -pw Password") 'Send your commands to the standard input. console.StdIn.Write ("cd /root/home/temp" & vbCr) console.StdIn.Write ("chmod 666 *.csv" & vbCr) console.StdIn.Write ("cd /root/home/temp1" & vbCr) console.StdIn.Write ("chmod 666 *.csv" & vbCr) console.StdIn.Write ("exit" & vbCr) End Sub
如果运行速度过快,则可以随时进行测试,以确保获得适当的服务器响应,或者在将命令发送到StdIn之间添加一个短暂的等待时间。
您打开和写入文件的方式存在问题:
fNum = FreeFile() Open vPath & "Chg.txt" For Output As #1 Print #1,"c:"
您正在检查下一个可用的文件编号,将该编号存储为变量“fNum”,然后打开文件为#1,而不管FreeFile()返回的是什么。 据我所知,您可能会遇到文件编号冲突。 另外,在我的结尾“-s:”作为命令行参数失败。 尝试使用.cmd文件,并将其作为命令调用:
Public Sub Chgaccper() Dim vPath As String Dim vFile As String Dim vSubpath As String Dim vscript As String Dim fNum As Long Dim oShell Set fso = CreateObject("scripting.filesystemobject") vPath = ThisWorkbook.Path 'Mounting file command for ftp.exe fNum = FreeFile() Open vPath & "Chg.cmd" For Output As fNum Print #fNum,"c:" Print #fNum,"set PATH=" & vPath & ";%PATH% " Print #fNum," " Print #fNum,"plink server Name -l uname -pw Password " Print #fNum,"cd /root/home/temp " Print #fNum,"chmod 666 *.csv " Print #fNum,"cd /root/home/temp1 " Print #fNum,"exit " Close #fNum vscript = "" & vPath & "Chg.cmd" If fso.FolderExists("C:WindowsSystem32") = False Then Shell "C:WINNTsystem32cmd.exe /k " & vscript & "" Else Shell "C:WINDOWSsystem32cmd.exe /k " & vscript & "" End If SetAttr vPath & "Chg.cmd",vbnormal Kill vPath & "Chg.cmd" End Sub
参考: https : //msdn.microsoft.com/en-us/library/office/gg264526.aspx
此方法创建2个文件。 一个(chg.bat)调用Plink并登录到服务器。 此外,它指示Plink执行第二个文件(commands.txt)中的命令。
Public Sub Chgaccper() Dim vPath As String Dim vscript As String vPath = ThisWorkbook.Path Open vPath & "Chg.bat" For Output As #1 Print #1,"plink serverName -l uname -pw Password -m commands.txt" Close #1 Open vPath & "commands.txt" For Output As #2 Print #2,"cd /root/home/temp" Print #2,"chmod 666 *.csv" Print #2,"cd /root/home/temp1" Print #2,"exit" Close #2 vscript = "" & vPath & "Chg.bat" Shell vscript SetAttr vPath & "Chg.bat",vbnormal SetAttr vPath & "commands.txt",vbnormal Kill vPath & "Chg.bat" Kill vPath & "commands.txt" End Sub
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。