Shell 从Excel Vba调用Unix脚本
我试图使用Plink(putty命令行)从VBA调用一组Unix命令,但这些命令没有得到执行。我会发布代码,任何更正或建议都会有所帮助 也欢迎其他想法,我所要做的就是访问unix文件更改访问权限并将文件移动到其他文件夹 请找到下面的代码Shell 从Excel Vba调用Unix脚本,shell,vba,unix,excel,vbscript,Shell,Vba,Unix,Excel,Vbscript,我试图使用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 o
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
如何打开和写入文件存在问题:
fNum = FreeFile()
Open vPath & "\Chg.txt" For Output As #1
Print #1, "c:\"
您正在检查下一个可用的文件编号,将该编号存储为变量“fNum”,然后将文件作为#1打开,而不管FreeFile()返回什么。据我所知,您可能有文件编号冲突。另外,在我这方面“-s:”as命令行参数失败。尝试改用.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, " "
Print #fNum, "cd /root/home/temp "
Print #fNum, " "
Print #fNum, "chmod 666 *.csv "
Print #fNum, " "
Print #fNum, "cd /root/home/temp1 "
Print #fNum, " "
Print #fNum, "chmod 666 *.csv "
Print #fNum, " "
Print #fNum, "exit "
Close #fNum
vscript = "" & vPath & "\Chg.cmd"
If fso.FolderExists("C:\Windows\System32") = False Then
Shell "C:\WINNT\system32\cmd.exe /k " & vscript & ""
Else
Shell "C:\WINDOWS\system32\cmd.exe /k " & vscript & ""
End If
SetAttr vPath & "\Chg.cmd", vbNormal
Kill vPath & "\Chg.cmd"
End Sub
Ref:此方法创建2个文件。一个(chg.bat)调用Plink并登录到服务器。此外,它还指示Plink执行第二个文件(commands.txt)中的命令
一个选项是在
WScript.Shell
中打开plink
会话,而不是使用VBA的Shell
使用脚本文件执行它。plink
程序将从命令行以交互模式运行,WshExec
对象允许您直接访问正在执行的进程的标准输入和标准输出流。这个简短的示例演示如何交互使用它(它登录到public telehack.com telnet服务器并执行fnord
命令),同时将所有控制台输出复制到即时窗口:
Private Sub-Fnord()
作为对象的暗壳
Set shell=CreateObject(“WScript.shell”)
作为对象的Dim控制台
'在交互模式下打开plink。
Set console=shell.Exec(“c:\putty\plink-telnet telehack.com-P 443”)
'等待命令提示。
WaitForResponseText控制台“”
'将fnord命令发送到标准输入。
console.StdIn.Write(“fnord”和vbCr)
'等待服务器将其回显。
WaitForResponseText控制台“.fnord”
'通过下一个命令提示符读取标准输出。
WaitForResponseText控制台“”
'退出远程会话。
console.StdIn.Write(“退出”和vbCr)
端接头
私有子WaitForResponseText(控制台作为对象,响应作为字符串)
像线一样变暗
'确保有要读取的输出。
如果console.StdOut.AtEndOfStream,则退出Sub
做
'从标准输出读取一行。
out=console.StdOut.ReadLine()
'这不是严格要求的,但允许在不退出的情况下终止进程。
多芬特
'将服务器输出发送到即时窗口。
调试。打印输出
“检查我们等待的答复。
如果安装(输出,响应),则
退出Do
如果结束
循环到console.StdOut.AtEndOfStream
端接头
在您的情况下,与所连接的服务器之间没有太多的“交互”,因此可能只需将所有命令直接发送到StdIn
。考虑到plink
具有广泛的协议支持,如果运行脚本文件与此有很大不同,我会感到惊讶:
Public Sub-Chgaccper()
作为对象的暗壳
Set shell=CreateObject(“WScript.shell”)
作为对象的Dim控制台
'在交互模式下打开plink。
Set console=shell.Exec(“c:\putty\plink服务器名称-l uname-pw密码”)
'将命令发送到标准输入。
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(“退出”和vbCr)
端接头
如果运行得太快,您可以始终进行测试,以确保获得适当的服务器响应,或者在向
StdIn
发送命令之间添加一段短等待时间,以确定代码的结果是什么?您是否有一个Chg.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, "c:\"
Print #1, "set PATH=" & vPath & ";%PATH% "
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, "chmod 666 *.csv"
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