Visual Basic for Applications(VBA)Shell

Visual Basic for Applications(VBA)Shell,shell,vba,command-line-arguments,putty,Shell,Vba,Command Line Arguments,Putty,我想在我的linux服务器上使用Excel中的VBA调用脚本。为了做到这一点,我使用了路径中的Putty的本地副本 我已经成功地编写了一个命令来创建putty会话并登录到服务器,我只是不确定在shell中打开后如何传递另一个命令 有什么想法吗 下面的子例程可以工作,但在尝试第二个命令时会导致问题 干杯 Sub test() Dim putty As String Dim strCommand As String Dim strCommand1 As String Dim User As Stri

我想在我的linux服务器上使用Excel中的VBA调用脚本。为了做到这一点,我使用了路径中的Putty的本地副本

我已经成功地编写了一个命令来创建putty会话并登录到服务器,我只是不确定在shell中打开后如何传递另一个命令

有什么想法吗

下面的子例程可以工作,但在尝试第二个命令时会导致问题

干杯

Sub test()
Dim putty As String
Dim strCommand As String
Dim strCommand1 As String
Dim User As String
Dim Pass As String
Dim Host As String
Dim File As String
Dim RemotePath As String

putty = """" & Range("E3").Text & "\Putty.exe"""
User = Range("E8").Text
Pass = Range("E9").Text
Host = Range("E7").Text
'File = """" & Range("E11").Text & """"
RemotePath = Range("E10").Text

strCommand = putty & " -l " & User & " -pw " & Pass & _
    " " & File & " " & Host & ":" & RemotePath
'MsgBox strCommand
Shell strCommand, 1 ' vbNormalFocus '
Module2.WaitFor (5)
Shell "ls -l > shell.log", 1
End Sub

使用其他工具执行shell命令 通过以下代码执行此命令:

Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Sub ShellAndWait(ByVal program_name As String, _
    ByVal window_style As VbAppWinStyle)
Dim process_id As Long
Dim process_handle As Long

    ' Start the program.
    On Error GoTo ShellError
    process_id = Shell(program_name, window_style)
    On Error GoTo 0

    DoEvents

    ' Wait for the program to finish.
    ' Get the process handle.
    process_handle = OpenProcess(SYNCHRONIZE, 0, process_id)
    If process_handle <> 0 Then
        WaitForSingleObject process_handle, INFINITE
        CloseHandle process_handle
    End If

    Exit Sub

ShellError:
    MsgBox Err.Description, vbOKOnly Or vbExclamation, "Error"
End Sub

ShellAndWait "plink -pw password -noagent user@server.com ""rm -f /tmp/file.txt ; gunzip /tmp/file.txt.gz""", vbMaximizedFocus
Private声明函数WaitForSingleObject库“kernel32”(ByVal hHandle长,ByVal dwms长)长
私有声明函数OpenProcess Lib“kernel32”(ByVal dwDesiredAccess为Long,ByVal bInheritHandle为Long,ByVal dwProcessId为Long)为Long
私有声明函数CloseHandle Lib“kernel32”(ByVal hObject As Long)为Long
子ShellAndWait(ByVal程序名称为字符串_
ByVal窗口(样式为VbAppWinStyle)
Dim进程\u id尽可能长
变暗处理(如长)
"启动程序。
关于错误转到ShellError
进程id=Shell(程序名称、窗口样式)
错误转到0
多芬特
'等待程序完成。
'获取进程句柄。
进程\u句柄=OpenProcess(同步,0,进程\u id)
如果进程处理0,则
WaitForSingleObject进程\ U句柄,无限
CloseHandle进程\u句柄
如果结束
出口接头
外壳错误:
MsgBox Err.Description,vbOKOnly或VBEQUOTE,“Error”
端接头
ShellAndWait“plink-pw密码-noagentuser@server.com“rm-f/tmp/file.txt;gunzip/tmp/file.txt.gz”“,vbMaximizedFocus”

我意识到最后一次Shell执行不会起作用,因为它不是一个windows Shell命令,而是一个Linux命令。我认为我的问题是如何获取最初被调用的shell来执行另一个命令。即使只是一个命令也可以,如此接近!将命令分隔在一行上,即命令a和命令b也不起作用。