【问题标题】:How to run python function in Unix server using Excel VBA?如何使用 Excel VBA 在 Unix 服务器中运行 python 函数?
【发布时间】:2014-10-17 10:22:08
【问题描述】:

我想使用 Excel VBA/VB 脚本自动执行以下步骤。 这是我手动执行的操作列表

第一步:登录winscp。

  • 主机名:example.ash.pwj.com
  • 端口:22
  • 用户名:xxx 密码:yyyy
  • 文件协议:SFTP

使用 Winscp 将名为“testFile”的文件从本地机器传输到 Unix 服务器中的目录 (该目录有一个python脚本'connector.py')

第二步:打开腻子。

  • 目标主机名:example.ash.pwj.com
  • 端口:22
  • 连接类型:SSH
  • 使用凭据登录。

Step3:调用目录下的python脚本。 python connector.py argument1 argument2 argument3

例如:python connector.py testFile example2.ash.pwj.com 123

我必须使用 Excel VBA/VBScript 在 putty 中处理 WINSCP 文件传输和调用 python 脚本。

这是我的代码:

Set sh = CreateObject("WScript.Shell")

sh.Run "pscp -pw yyyy C:\Users\abc\testFile xxx@example.ash.pwj.com:/home/test/dir", 0, True
sh.Run "plink.exe -pw yyyy xxx@example.ash.pwj.com python /home/test/dir/connector.py testFile example2.ash.pwj.com 514", 0, True

【问题讨论】:

  • @will 并不是真正的重复,因为他想在具有不同操作系统的不同主机上运行 python 脚本。
  • 这一切都可以在被调用的 python 脚本中完成,不是吗?
  • @will 在远程 (Unix) 主机上调用 Python 脚本。我们不知道 Windows 机器上是否安装了 Python 解释器,或者 OP 是否可以安装一个。

标签: python vba unix vbscript


【解决方案1】:

您可以从PuTTY suitepscpplink 支付费用:

Set sh = CreateObject("WScript.Shell")

sh.Run "pscp -pw yyyy c:\testFile xxx@example.com:/some/dir/", 0, True
sh.Run "plink -pw yyyy xxx@example.com python /some/dir/connector.py arg1 arg2 arg3", 0, True

请注意,如果路径包含空格,则需要添加正确的引号。

如果您的目标路径只是没有文件名的目标目录:指定带有尾随文件名的路径。这样,当目标目录不存在时,您会收到错误消息。否则,当/some 中没有子目录dir 时,pscp file user@example.com:/some/dir 可能会创建一个文件/some/dir 而不是/some/dir/file

此外,您需要确保将正确的路径传递给 Python 脚本。当您将文件复制到/home/test/dir/ 时,您应该在调用 Python 脚本之前切换到该目录,或者指定文件名及其完整路径:

sh.Run "plink.exe -pw yyyy xxx@example.ash.pwj.com python /home/test/dir/connector.py /home/test/dir/testFile example2.ash.pwj.com 514", 0, True

/home/test/dir 运行 Python 脚本不会自动将该目录设为当前工作目录。

【讨论】:

  • 我用适当的路径尝试了上述方法。每次都会抛出“系统找不到指定的文件”错误!
  • @Prasanna 是否有任何路径/文件名包含空格?如果是这样:您是否正确引用了路径?
  • 嗨 Ansgar,路径/文件名没有空格。注意:arg2 是 /some/dir 中存在的文件名
  • sh.Run "pscp -pw yyyy C:\Users\abc\testFile xxx@example.ash.pwj.com:/home/test/dir", 0, True || sh.Run "plink.exe -pw yyyy xxx@example.ash.pwj.com python /home/test/dir/connector.py testFile example2.ash.pwj.com 514", 0, True
  • @Prasanna 请不要在 cmets 中发布类似的内容。将其添加到您的 question 中。该代码是脚本中的一行吗?如果是这样:那是行不通的。 || 是 CMD 运算符。它在 VBScript 中无效。
猜你喜欢
  • 2011-11-23
  • 1970-01-01
  • 2013-02-09
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多