如何使用腻子将多个命令传递给 VBA 中的 Shell 函数?

How to pass multiple commands to Shell function in VBA using putty?

我正在尝试使用 VBA shell 命令连接到 Linux 服务器。我无法在其中执行多个命令。

我的 VBA 脚本中有以下代码,以及包含命令列表的命令文件

Private Sub main_function()

    cmd = "C:\putty.exe -t -ssh username@servername.com -pw password -P 22 -m c:\cmd.txt"
    retval = Shell(cmd, vbMaximizedFocus)

End Sub

sudo to user
cat > filename.txt
sh shellfile.sh

我想执行 cmd.txt 文件中列出的所有命令,但无法执行 cmd.txt 文件中的任何命令,代码只是登录到 linux并退出。

创建一个无需使用 VBA 即可运行的批处理文件,然后使用上面的代码简单地调用该批处理文件。您的问题目前与 VBA.

无关

请参阅此post,了解如何制作批处理文件以使用 Putty 登录服务器。

https://superuser.com/questions/1278434/create-a-batch-file-or-shortcut-to-putty-ssh-that-opens-a-session-and-runs-a-c

例如:我刚测试过,效果很好

Private Sub main_function()

    cmd = "C:\test.cmd"
    retval = Shell(cmd, vbMaximizedFocus)

End Sub

确保您的批处理在您双击时有效,然后通过 VBA 调用。