Search code examples
powershelltelnetvxworks

How to redirect or retrieve the output of a telnet process ran by a Powershell script?


I am running a Powershell script that starts a telnet process which will connect on a client machine and run a command. The client machine's operating system is a personalized version of VxWorks, and the command I'm using on the telnet calls a tool developed internally on the machine:

mediaGet("gei",1)

I'd like to retrieve the output of the code and log it.

I've tried using the parameters -NoNewWindow and -RedirectStandardOutput on the Powershell script, but it only creates me an empty file which means it fails to retrieve the output.

Here is the Powershell script:

Start-Process -FilePath 'telnet.exe' -ArgumentList '162.100.10.10'
[system.reflection.assembly]::loadwithpartialname("System.Windows.Forms")
$SendKeys = [System.Windows.Forms.SendKeys]
$sendkeys::SendWait('mediaGet{(}"gei"{)},1')
Start-Sleep -Seconds .5
$sendkeys::SendWait("{ENTER}")
Start-Sleep -Seconds .5
$sendkeys::SendWait("exit{ENTER}")

(Credits of this code go to the selected answer of this question How to use a string variable as a telnet command in a vbscript?)

This is the output of the code

mediaGet("gei",1)
> media: 12345
> up: 1000 full duplex

I would like a text file at the end with the following lines:

media: 12345
up: 1000 full duplex

How can I get there?


Solution

  • Komputer.

    Yeppers, I missed getting back to you on the other Q&A. Good to see you are getting the response back now.

    But even if that was in text file, you can just replace it after.

    $TelnetData = @'
    mediaGet("gei",1)
    > media: 12345
    > up: 1000 full duplex
    '@ 
    Clear-Host
    $TelnetData -replace 'mediaGet|\("gei",1\)|> ', ''
    
    # Results
    
    media: 12345
    up: 1000 full duplex
    

    You can do this as the file is written as well, in normal cases.

    However, on the redirect stuff, the common thing is,

    MS telnet doesn't use stdin/out.

    So, approaches like this …

    For redirecting the output of a telnet session you can use the ...

    -f logfile

    ...argument and then importing it into a variable after you are done with it:

    $output = get-contents logfile

    ... are often brought up.

    So, maybe this will get you where you want to be...

    Start-Process -FilePath 'telnet.exe' -ArgumentList '162.100.10.10', '-f E:\Temp\log.txt'
    [system.reflection.assembly]::loadwithpartialname("System.Windows.Forms")
    $SendKeys = [System.Windows.Forms.SendKeys]
    $sendkeys::SendWait('mediaGet{(}"gei"{)},1')
    Start-Sleep -Seconds .5
    $sendkeys::SendWait("{ENTER}")
    Start-Sleep -Seconds .5
    $sendkeys::SendWait("exit{ENTER}")
    

    Once you do the above, then you can run the clean-up on it, using what I have above, via the Get-Content and / or Set-Content cmdlets.

    Of course I cannot access the IPA you list, nor do I have that MediaGet thing, but a quick test to a telnet capable site, will do this.

    Start-Process -FilePath 'telnet.exe' -ArgumentList 'www.cyberciti.biz 80', '-f E:\Temp\log.txt'
    [system.reflection.assembly]::loadwithpartialname("System.Windows.Forms")
    $SendKeys = [System.Windows.Forms.SendKeys]
    $sendkeys::SendWait("mediaGet'(''gei''',1)'")
    Start-Sleep -Seconds .5
    $sendkeys::SendWait("{ENTER}")
    Start-Sleep -Seconds .5
    $sendkeys::SendWait("exit{ENTER}")
    
    # PowerShell Terminal output.
    
    GAC    Version        Location                                                                                                                                 
    ---    -------        --------                                                                                                                                 
    True   v4.0.30319     C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll 
    
    
    # Telnet Window and text file content
    Get-Content -Path 'E:\Temp\log.txt'
    
               HTTP/1.1 400 Bad Request
     Server: cloudflaree
     Date: Sat, 18 May 2019 08:39:59 GMTi
       Content-Type: text/html'Content-Type: text/html'
    
     Connection: close
     Connection: close
    
    <html>
    <head><title>400 Bad Request</title></head>e
      <body bgcolor="white">i
      <center><h1>400 Bad Request</h1></center>
      <center><h1>400 Bad Request</h1></center>
    <hr><center>cloudflare</center>
    </body>
    </html>