【发布时间】:2013-10-30 04:55:03
【问题描述】:
我正在尝试使用 C 程序自动从 Linux 执行一些 powershell 脚本。我正在使用 libexpect 将命令发送到 Windows 并获取 powershell 终端的文件描述符并写入它以运行 powershell 脚本/命令。它工作正常。但我的要求是我希望能够从我的 C 程序中读取 powershell 脚本的输出。
C 程序如下所示:
fd = exp_spawnl( "sh", "sh", "-c", "telent -l username machine", (char*)0);
exp_expectl( fd, exp_glob, "password: ", 1, exp_end);
write(fd, "password\r", NUM_BYTES);
exp_expectl( fd, exp_glob, ".*>", 1, exp_end);
write(fd, "powershell \path\script.ps1\r", NUM_BYTES);
如上所述,无法获取在 Windows 上执行的 powershell script.ps1 的 stdout/stderr。
linux-windows-powershell-linux 的整个设置都在本地网络中。因此,我现在不太担心使用 telnet。但我愿意接受任何有助于实现我的目标的解决方案(无论是否使用 ssh)。
如果有其他选择,我也愿意不使用 libexpect。基本上我可以用这种方法改变任何东西。我正在使用它,因为我不知道有任何其他方式可以按照预期(libexpect)将用户名/密码和命令发送到远程 shell。
只要满足我的主要目标,即自动执行 powershell 脚本/命令并获取它们的 stdout 和 stderr及其退出状态$?。请注意,我不需要文件指针本身,如果我能在 char[] 中获取 值(就像 bash 中的 OUTPUT=$(ls)),我会非常高兴。
【问题讨论】:
标签: c linux windows powershell expect