vba调用linux shell,shell – 从Excel Vba调用Unix脚本

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

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发送命令之间添加一个短暂的等待.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值