简体   繁体   English

从Linux执行Powershell脚本

[英]Executing powershell script from Linux

I am trying to execute some powershell scripts from Linux using a C program automatically. 我正在尝试使用C程序自动从Linux执行一些powershell脚本。 I am using libexpect to send the commands to Windows and get the file descriptor of the powershell terminal and writing to it to run powershell scripts/commands. 我正在使用libexpect将命令发送到Windows,并获取powershell终端的文件描述符,并写入该文件以运行powershell脚本/命令。 It works fine. 工作正常。 But my requirement is that I want to be able to read the output of the powershell script from my C program. 但是我的要求是我希望能够从我的C程序中读取powershell脚本的输出。

C program looks like this: 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);

As it's above, it's not possible to get the stdout/stderr of powershell script.ps1 that's being executed on windows. 如上所述,无法获得在Windows上执行的powershell script.ps1的stdout / stderr。

The whole setup of linux-windows-powershell-linux is in a local network. linux-windows-powershell-linux的整个设置都在本地网络中。 Hence, I am not too worried about using telnet at the moment. 因此,我现在不太担心使用telnet。 But I am open to any solutions (ssh or not) that helps achieve my goal. 但是我愿意接受任何有助于实现目标的解决方案(无论是否采用ssh)。

I am open to not using libexpect as well if there are alternative options. 如果有其他选择,我愿意不使用libexpect。 Basically I can change anything in this approach. 基本上,我可以使用这种方法进行任何更改。 I am using it because I am not aware of any other way to send in username/password and commands to the remote shell as done expect (libexpect). 我之所以使用它,是因为我不知道有什么其他方式可以像预期的那样将用户名/密码和命令发送到远程shell(libexpect)。

I am open to suggestions on writing a expect/bash script on linux OR some on-the-fly powershell script on windows as long as my primary objective is met ie execute powershell scripts/commands automatically and get stdout and stderr of them and their exit status $? 只要我的主要目标得到满足,即自动执行powershell脚本/命令并获取它们的stdout和stderr以及退出,我愿意接受有关在Linux上编写Expect / bash脚本或在Windows上即时运行Powershell脚本的建议。状态$? . Note that I don't need the file pointers themselves, I am quite happy if I can get the values in a char[] (like OUTPUT=$(ls) in bash). 请注意,我自己不需要文件指针,如果可以在char[]获取 (例如bash中的OUTPUT=$(ls) ,我会很高兴。

A (not very handy) solution: the expect's manual says: "If exp_loguser is nonzero, expect sends any output from the spawned process to stdout." 一个(不太方便的)解决方案:Expect的手册说:“如果exp_loguser不为零,则Expect将产生的进程的所有输出发送到stdout。” ( here ). 在这里 )。 So, if you temporarily redirect your stdout on a pipe's write side, you can use the logging feature to actually intercept the spawned process' output by reading the pipe's read side. 因此,如果您将标准输出临时重定向到管道的写入侧,则可以使用日志记录功能通过读取管道的读取侧来实际拦截生成的进程的输出。 The output will be probably mixed with the input (the interactive programs are used to echo their input), so it's on you to identify the relevant data in the redirected stream. 输出可能会与输入混合(交互式程序用于回显其输入),因此您需要在重定向流中标识相关数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM