Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to fake windows console api?

I've written a ssh server in c# and I thought it'd be neat to hook up powershell as a shell. I've tried 2 methods to get this to work properly but both are far from perfect. Here's what I've tried:

  1. Launch powershell.exe and redirect it's std(in/out). This doesn't work well since powershell.exe detects it is redirected, changes it's behaviour. What's more, it expects input data on the stdid, not commands. So it uses the console api to read commands.
  2. Host powershell in a "wrapper" application. This has the advantage of being able to provide a "console" implementation to powershell (via the PSHostRawUserInterface). This works better, but you can still invoke commands (mostly real console applications) like "... | more", that expect to be able to use the console api, and subsequently try to read from the console of the wrapper process.

So what I'd like to do is have a set of functions replace the regular console input/output functions that console applications use, so I can handle them. But that seems rather drastic to the point of being a bad design idea (imo).

Right now I am on the idea of manipulating the console by sending the relevant keys with native/Pinvoke functions like WriteConsoleInput. I gather that it might be possible to fake the console that way. But I don't see how I would then "read" what happens on the console.

Also keep in mind, it's a service, so preferably it shouldn't spawn an actual console window, although perhaps in windows session 0 that wouldn't show up and not matter.

like image 974
Captain Coder Avatar asked Oct 26 '11 11:10

Captain Coder


1 Answers

You've got PSSession for this purpose and the Enter-PSSession CmdLet. What will your SSH with Powershell do that PSSession is not doing ?

But if you want to do that here is a solution whithout writting anything : Using PowerShell through SSH


Edited 02/11/2011

PowerShell inside provide another way to do it whithout writting anything (free for personal usage).

Host03 sample, can perhaps provide basic code to do what you wat to do.

like image 123
JPBlanc Avatar answered Nov 12 '22 16:11

JPBlanc