gpt4 book ai didi

shell - 从 Excel Vba 调用 Unix 脚本

转载 作者:行者123 更新时间:2023-12-03 00:20:43 29 4
gpt4 key购买 nike

我尝试使用 Plink(putty 命令行)从 VBA 调用一组 Unix 命令,但这些命令没有被执行。我将发布代码,任何更正或建议都会有帮助。

也欢迎其他想法,我所要做的就是访问unix文件更改访问权限并将文件移动到其他文件夹。

请找到下面的代码

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, " "
Print #1, "cd /root/home/temp "
Print #1, " "
Print #1, "chmod 666 *.csv "
Print #1, " "
Print #1, "cd /root/home/temp1 "
Print #1, " "
Print #1, "chmod 666 *.csv "
Print #1, " "
Print #1, "exit "
Print #1, " "
Close #1

vscript = "" & vPath & "\Chg.txt"

If fso.FolderExists("C:\Windows\System32") = False Then
Shell "C:\WINNT\system32\cmd.exe -s:" & vscript & ""
Else
Shell "C:\WINDOWS\system32\cmd.exe -s:" & vscript & ""

End If

SetAttr vPath & "\Chg.txt", vbNormal
Kill vPath & "\Chg.txt"


End Sub

最佳答案

一种选择是在 WScript.Shell 中打开 plink session ,而不是使用 VBA 的 Shell 通过脚本文件执行它。 plink 程序将从命令行以交互模式运行,而 WshExec 对象使您可以直接访问您正在运行的进程的标准输入和标准输出流。执行。这个简短的示例演示了如何以交互方式使用它(它登录到公共(public) telehack.com telnet 服务器并执行 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:\putty\plink -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 具有广泛的协议(protocol)支持,如果运行脚本文件与此有很大不同,我会感到惊讶:

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:\putty\plink 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 发送命令之间添加短暂的等待。

关于shell - 从 Excel Vba 调用 Unix 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20796017/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com