Jump to content

SSH, StderrRead


Recommended Posts

Summary of problem:

Script runs plink (SSH command line tool) to connect to a network device, in some cases the network device will display a warning like:

"you do not have the certificate cached in the registry, add to registry? (y/n)" --- This output appears from the error output, therefore can be read from StderrRead. However StderrRead will pase the program UNTIL it has some input, which, in this instance is unhelpful as I want to take action if no input is given from the error...

Code:

$stream = Run(@comspec & " /k c:\plink.exe " & $hostname & " -pw xxxxx -l * -ssh ",'',$STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)

$output = $OUT = StderrRead($stream)

If $output = "you do not have the certificate cached in the registry, add to registry?" Then

Do XXXXXX

Else Do XXXXXX

....................................................................................................

.....................................

I have tried setting the "peak" option, but it doesn't appear to help me, I'm sure i'm being dumb here; any advice would be greatly appreciated.

Cheers

Link to comment
Share on other sites

really interesting your command line tool, but are you sure that it works as good as Putty ?

can you post you current script i would like to test your work because it's really interesting

Edited by arcker

-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]
Link to comment
Share on other sites

really interesting your command line tool, but are you sure that it works as good as Putty ?

can you post you current script i would like to test your work because it's really interesting

Thanks for your interest, I am a network administrator and have written a fair few scripts to do various things, backups, audits, firmware updates, security checks, I would be happy to send you a copy of the script once it's working :)

The most robust way I have scripted this type of thing is via telnet, you can make a TCP connection to devices which is very robust indeed (never fails). the scripts look through ini files and connect to all the devices in turn, perform a set of commands and log out. Really vey simple, I just hope I can get this StderrRead thing to work, some boffin out there will be able to help me i'm sure :D

Link to comment
Share on other sites

ok post your current work and i will try to help you

i've always search a command line utility as putty, but i've never heard plink...ARGH !

i wait for your script and i will help you

-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]
Link to comment
Share on other sites

ok post your current work and i will try to help you

i've always search a command line utility as putty, but i've never heard plink...ARGH !

i wait for your script and i will help you

#include <Constants.au3>
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Script to conect via SSH to remote devices, download a backup image via TFTP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

$numswitches = IniRead( "C:\myfile1.ini", "Switches", "Count", "0" )

for $x = 0 to $numswitches

$hostname = IniRead( "C:\myfile1.ini", "Switches", ( "Switch" & $x ) , "0" )




Dim $szIPADDRESS = TCPNameToIP($hostname)


$stream = Run(@comspec & " /k c:\plink.exe " & $hostname & " -pw password -l * -ssh ",'',$STDIN_CHILD + $STDERR_CHILD)
 

$OUT = StderrRead($stream)


If $OUT = "The server's host key is not cached in the registry*" Then
 
Sleep(200)
StdinWrite($stream, @CRLF)
Sleep(200)
StdinWrite($stream, "copy startup-config tftp " & @IPAddress1 & " " & $hostname & "_" & @MDAY & "." & @mon & "." & @YEAR & @LF)
Sleep(2000)
StdinWrite($stream, "logout" & @CRLF)
Sleep(1200)
StdinWrite($stream, "y")
Sleep(1200)
StdinWrite($stream, "y")
Sleep(1500)
ProcessClose($stream)
Sleep(1500)
Else


StdinWrite($stream,"y" & @CRLF)
Sleep(200) 
StdinWrite($stream, @CRLF)
Sleep(200)
StdinWrite($stream, "copy startup-config tftp " & @IPAddress1 & " " & $hostname & "_" & @MDAY & "." & @mon & "." & @YEAR & @LF)
Sleep(2000)
StdinWrite($stream, "logout" & @CRLF)
Sleep(1200)
StdinWrite($stream, "y")
Sleep(1200)
StdinWrite($stream, "y")
Sleep(1500)
ProcessClose($stream)
Sleep(500)

EndIf


next
Link to comment
Share on other sites

ouch really difficult because you try to send some commands with some sleep

i would start with cmd /c instead of /k, because /k don't return anything (it's is feature)

you can after get the output, and make somes cases, like tcprecv does !

i'm sure it's possible

btw, do you know if it's possible to disable the sound of plink ? i hate those pc speakers sounds !

-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]
Link to comment
Share on other sites

ouch really difficult because you try to send some commands with some sleep

i would start with cmd /c instead of /k, because /k don't return anything (it's is feature)

you can after get the output, and make somes cases, like tcprecv does !

i'm sure it's possible

btw, do you know if it's possible to disable the sound of plink ? i hate those pc speakers sounds !

Ahh that's something to try, thanks for that... I iwll keep you posted. As for the pc speaker sounds, no idea - on my system I don't hear anything.

Link to comment
Share on other sites

OK, an update:

After further testing it appears that none of the measures have helped

the problem is this:

sometimes you need to take action based on the STDERR, however, if there is no error then the function StderrRead will halt execution of the script. You can't just say:

IF (the error output = x) Then (do y) --- because if there is no error to read (which in some cases there won't be) then you just get paused until there is.

If anyone can help I would be most appreciative.

Link to comment
Share on other sites

It doesn't work because you have no loop to read the streams. The return from Run() is process ID. Microseconds after you kick it off you do ONE read of STDERR and act on it. There probably hasn't been time for anything to happen yet. You need a loop to read the streams you are interested in:

$PID = Run($YourCommandLine, '', @SW_SHOW, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)
DIM $sSTDOUT = "", $sSTDERR = ""
Do
    $sSTDOUT &= StdoutRead($PID)
    $sSTDERR &= StderrRead($PID)
    _FuncToActOnOutput()
    Sleep(20)
Until Not ProcessExists($PID)

Notice the use of the '&=' operator to assemble the output streams into complete strings...

:)

Edit: Forgot to include $STDOUT_CHILD in example Run() function...

Edit2: Sigh... Also didn't include show flag before IO... :">

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

It doesn't work because you have no loop to read the streams. The return from Run() is process ID. Microseconds after you kick it off you do ONE read of STDERR and act on it. There probably hasn't been time for anything to happen yet. You need a loop to read the streams you are interested in:

$PID = Run($YourCommandLine, '', $STDIN_CHILD + $STDERR_CHILD)
DIM $sSTDOUT = "", $sSTDERR = ""
Do
    $sSTDOUT &= StdoutRead($PID)
    $sSTDERR &= StderrRead($PID)
    _FuncToActOnOutput()
    Sleep(20)
Until Not ProcessExists($PID)

Notice the use of the '&=' operator to assemble the output streams into complete strings...

:)

YEAHHHHHH, Man thank you so much - I knew it was somehting like this, but i'm a noob with programming concepts

Link to comment
Share on other sites

strange i've anything from output

does it work for you ?

-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]
Link to comment
Share on other sites

strange i've anything from output

does it work for you ?

I didn't format the Run() function correctly in my original post. Make sure you have CommandLine, WorkingDir, ShowFlag, IOFlags.

:">

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

I will be back at work on Tuesday and will give this a shot, thanks again, I'm kinda knew I needed to have the functions in a loop - but never having been trained to program it's a new concept for me.

cheers

Link to comment
Share on other sites

thx to you for learn ms (maybe us) that we can develop some script to manage our unix/aix/linux servers from windows

BTW, i wait for your example and i'll try it as soon as possible

omg, i've forgotten the show flag too....tssss

Edited by arcker

-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]
Link to comment
Share on other sites

...we can develop some script to manage our unix/aix/linux servers from windows.

Eww! :D

Isn't that a bit like managing your French cuisine from a McDonald's grill?

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

OK update:

It still doesn't work, because; the StderrRead and StdoutRead functions will pause proggy execution until there is some data. Which is fine in itself, but given that for me, in some cases there will be data for StdError not StdOut and vice verse I can't see a way of making it work all the time that these functions pause the script until they have data.

Edited by megatron
Link to comment
Share on other sites

Tweaked to be non-blocking by checking with 'peek' that there are characters to read before doing so:

$PID = Run($YourCommandLine, '', @SW_SHOW, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)
DIM $sSTDOUT = "", $sSTDERR = ""
Do
    If StdoutRead($PID, 0, True) Then $sSTDOUT &= StdoutRead($PID)
    If StderrRead($PID, 0, True) Then $sSTDERR &= StderrRead($PID)
    _FuncToActOnOutput()
    Sleep(20)
Until Not ProcessExists($PID)

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Tweaked to be non-blocking by checking with 'peek' that there are characters to read before doing so:

$PID = Run($YourCommandLine, '', @SW_SHOW, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)
DIM $sSTDOUT = "", $sSTDERR = ""
Do
    If StdoutRead($PID, 0, True) Then $sSTDOUT &= StdoutRead($PID)
    If StderrRead($PID, 0, True) Then $sSTDERR &= StderrRead($PID)
    _FuncToActOnOutput()
    Sleep(20)
Until Not ProcessExists($PID)

:)

Ahh it makes sense!! - and it's starting to work in production with the correct sleep(xxx) inserted. I will post the fll code up when it's done.

PsaltyDS - Thanks so much for your help on this, it's just great to be able to ask questions on here and have them answered in such a way, you couldn't pay for a better service.

Link to comment
Share on other sites

Isn't that a bit like managing your French cuisine from a McDonald's grill?

ohh yes it was an image

but sometimes for recurrent information retrieval (like cpu) it's really boring to log on by putty, enter user/pass and execute a command

this open up some ways, that's just what i wanted to say

-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...