如何在Unix服务器上通过Excel VBA运行Python函数?
我想用Excel的VBA或者VB脚本来自动化下面的步骤。以下是我手动执行的操作列表:
第一步:登录到winscp。
- 主机名:example.ash.pwj.com
- 端口:22
- 用户名:xxx 密码:yyyy
- 文件协议:SFTP
将一个名为'testFile'的文件从本地计算机传输到Unix服务器上的一个目录(这个目录里有一个名为'connector.py'的Python脚本)。
第二步:打开putty。
- 目标主机名:example.ash.pwj.com
- 端口:22
- 连接类型:SSH
- 使用凭据登录。
第三步:在目录中调用Python脚本。
python connector.py argument1 argument2 argument3
例如:python connector.py testFile example2.ash.pwj.com 123
我需要用Excel的VBA或VB脚本来处理WINSCP的文件传输和在putty中调用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
1 个回答
0
你可以使用pscp
和plink
,这些工具是PuTTY套件的一部分:
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
注意,如果路径中有空格,你需要加上适当的引号。
如果你的目标路径只是一个目录,没有指定文件名,那么你需要在路径后面加上文件名。这样的话,如果目标目录不存在,你会收到错误提示。否则,像pscp file user@example.com:/some/dir
这样的命令可能会在/some
目录下创建一个名为/some/dir
的文件,而不是/some/dir/file
,这在/some
中没有子目录dir
时会发生。
另外,你还需要确保将正确的路径传递给你的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脚本并不会自动让这个目录成为当前工作目录。