1

I am trying to execute some powershell scripts from Linux using a C program automatically. 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. 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 program looks like this:

  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.

The whole setup of linux-windows-powershell-linux is in a local network. Hence, I am not too worried about using telnet at the moment. But I am open to any solutions (ssh or not) that helps achieve my goal.

I am open to not using libexpect as well if there are alternative options. 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).

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 i.e. execute powershell scripts/commands automatically and get stdout and stderr of them and their exit status $?. 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).

0

1 Answer 1

1

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." (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.

Sign up to request clarification or add additional context in comments.

2 Comments

It seems exp_loguser is not very useful because it simply writes the output to stdout (or not). But when I looked up at this, I saw exp_logfile which seems to do what I need. I am not sure if it differentiates stdout & stderr. By this "If exp_logfile is also nonzero, this same output is written to the stream defined by exp_logfile",I believe, it only cares about stdout. But that's an improvement to what I have and I'll give it a go. Thanks :)
Uhm... I am afraid that it mixes out&err :-(, since it internally uses a pseudoterminal. Anyway the "logfile" option may ease your job. e.g. you can create the pipe and use fdopen to connect the write part on the logging stream. Or simply create a temporary file, it depends on your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.