Using wodSSH with PowerShell? - WeOnlyDo Discussion board

Using wodSSH with PowerShell? (General questions)

by Sean, Tuesday, March 13, 2012, 19:45 (4435 days ago)

Hi there,

I heard about wodSSH being able to be used with PowerShell. Can anyone point me to an easy way to start with this?

1. How do I load the DLL to use with PowerShell? (I tried [Reflection.Assembly]::LoadFile( C:WindowsSystem32wodSSH64.dll )) but that doesn't seem to do the trick.

2. How do I create an object and establish my first connection to a remote SSH server via PowerShell?

3. How do I execute an SSH cmd on the target and return any output in the Shell to a variable in PowerShell?

Thanks!
Sean

Re: Using wodSSH with PowerShell?

by woddrazen, Tuesday, March 13, 2012, 21:45 (4435 days ago) @ Sean

Hi Sean,


Here is PowerShell example that will connect to server and execute ls -al command.
[code]$hostname = Read-Host Hostname
$login = Read-Host Login
$password = Read-Host Password

$ssh = New-Object -ComObject weOnlyDo.wodSSHCom.1

$ssh.Hostname = $hostname
$ssh.Login = $login
$ssh.Password = $password
$ssh.Blocking = 1
$ssh.Connect()

$ssh.DataReady = 0
$ssh.WaitFor( regex:[$ #>] $ )
$ssh.Execute( ls -al`r`n , regex:[$ #>] $ )
$ssh.Disconnect()[/code]
Let us know how it goes.


Regards,
Drazen

Re: Using wodSSH with PowerShell?

by Sean, Tuesday, March 13, 2012, 21:48 (4435 days ago) @ woddrazen

Hi Sean,


Here is PowerShell example that will connect to server and execute ls -al command.
[code]$hostname = Read-Host Hostname
$login = Read-Host Login
$password = Read-Host Password

$ssh = New-Object -ComObject weOnlyDo.wodSSHCom.1

$ssh.Hostname = $hostname
$ssh.Login = $login
$ssh.Password = $password
$ssh.Blocking = 1
$ssh.Connect()

$ssh.DataReady = 0
$ssh.WaitFor( regex:[$ #>] $ )
$ssh.Execute( ls -al`r`n , regex:[$ #>] $ )
$ssh.Disconnect()[/code]
Let us know how it goes.


Regards,
Drazen

Awesome - that is all I needed (this line here):

$ssh = New-Object -ComObject weOnlyDo.wodSSHCom.1

Thank you :) I think I can refer to your documentation for the rest. I didn't know how to get the ComObject loaded (or what it was called).

Thanks again.

Sean

Re: Using wodSSH with PowerShell?

by woddrazen, Tuesday, March 13, 2012, 21:59 (4435 days ago) @ Sean

Sean,


Excellent! We are glad to hear that it works now.

Yes you can use our documentation and also you can find some examples here:
http://www.weonlydo.com/code.asp?prod=wodSSH


Drazen

Re: Using wodSSH with PowerShell?

by Sean, Tuesday, March 13, 2012, 22:24 (4435 days ago) @ woddrazen

Sean,


Excellent! We are glad to hear that it works now.

Yes you can use our documentation and also you can find some examples here:
http://www.weonlydo.com/code.asp?prod=wodSSH


Drazen

Ok, I have tried this, and it doesn't seem to want to connect via SSH to any of my devices. I know they are accepting connections and my firewall is fine as I can use PuTTy to get onto the devices just fine.

I took a look at $ssh and saw that the Port property was still on 23, for telnet, so I changed that to 22, but still not working. Is there anything else I should be looking out for? Such as when you first connect in PuTTy to a device for example there is a thumbprint that you need to accept or add to the host's cache (Yes/No/Cancel) etc... is this a problem for wodSSH?

I have tried two different devices - a VMware ESXi host with SSH enabled, as well as a Cisco Catalyst switch. Both I can normally SSH directly onto using PuTTy.

Any ideas? :)

Re: Using wodSSH with PowerShell?

by woddrazen, Tuesday, March 13, 2012, 22:26 (4435 days ago) @ Sean

Sean,


Can you show us maybe your code snippet?


Drazen

Re: Using wodSSH with PowerShell?

by Sean, Tuesday, March 13, 2012, 22:34 (4435 days ago) @ woddrazen

Sean,


Can you show us maybe your code snippet?


Drazen

$hostname = esxi-01.noobs.local
$login = root
$password = mypassgoeshere

$ssh = New-Object -ComObject weOnlyDo.wodSSHCom.1

$ssh.Hostname = $hostname
$ssh.Login = $login
$ssh.Password = $password
$ssh.Blocking = 1
$ssh.Port = 22
$ssh.Timeout = 25
$ssh.Connect()

$ssh.DataReady = 0
$ssh.WaitFor( regex:[$ #>] $ )
$ssh.Execute( df -h`r`n , regex:[$ #>] $ )
$ssh.Disconnect()

Normally, when I SSH on in PuTTy my shell gives me the prompt: ~ #

So I guess that $ssh.WaitFor( regex:[$ #>] $ ) is waiting for the shell with a variety of options including #? Once it sees this it then continues on to the $ssh.execute part? Is the regex check on the end of the $ssh.Execute do? Does it also wait for the shell prompt ~ # before continuing?

Sean

Re: Using wodSSH with PowerShell?

by woddrazen, Tuesday, March 13, 2012, 22:49 (4435 days ago) @ Sean

Sean,


Your prompt should be accepted by our regex because # appear at end of your prompt.

Do you received some error maybe? Which line produce it?


Drazen

Re: Using wodSSH with PowerShell?

by Sean, Tuesday, March 13, 2012, 22:55 (4435 days ago) @ woddrazen

Sean,


Your prompt should be accepted by our regex because # appear at end of your prompt.

Do you received some error maybe? Which line produce it?


Drazen

Yep, its pretty standard timeout error I get on the connection. Odd, because PuTTy SSH connects instantly with the exact same details - port 22, my username and password. Does user perhaps need to be set username@host instead of just username?

Here is the error:

PowerCLI C:Program Files (x86)VMwareInfrastructurevSphere PowerCLI> $ssh.Connect()
Exception calling Connect with 0 argument(s): The current connection has timed out.
At line:1 char:13
+ $ssh.Connect <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation

PowerCLI C:Program Files (x86)VMwareInfrastructurevSphere PowerCLI>
PowerCLI C:Program Files (x86)VMwareInfrastructurevSphere PowerCLI> $ssh.DataReady = 0

PowerCLI C:Program Files (x86)VMwareInfrastructurevSphere PowerCLI> $ssh.WaitFor( rege
x:[$ #>] $ )
Exception calling WaitFor with 1 argument(s): Cannot receive at this time, not conne
cted.
At line:1 char:13
+ $ssh.WaitFor <<<< ( regex:[$ #>] $ )
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation

PowerCLI C:Program Files (x86)VMwareInfrastructurevSphere PowerCLI> $ssh.Execute( ls -
al`r`n , regex:[$ #>] $ )
Exception calling Execute with 2 argument(s): Cannot receive at this time, not conne
cted.
At line:1 char:13
+ $ssh.Execute <<<< ( ls -al`r`n , regex:[$ #>] $ )
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation

PowerCLI C:Program Files (x86)VMwareInfrastructurevSphere PowerCLI> $ssh.Disconnect()
PowerCLI C:Program Files (x86)VMwareInfrastructurevSphere PowerCLI>

Re: Using wodSSH with PowerShell?

by woddrazen, Tuesday, March 13, 2012, 23:01 (4435 days ago) @ Sean

Sean,


You need to use only username inside Login Property. What happens if you run our VBS sample inside samples folder.

Does it work maybe there?


Drazen

Re: Using wodSSH with PowerShell?

by Sean, Tuesday, March 13, 2012, 23:47 (4435 days ago) @ woddrazen

Sean,


You need to use only username inside Login Property. What happens if you run our VBS sample inside samples folder.

Does it work maybe there?


Drazen

Thanks Drazen,

I'll give the VB sample a try now as I have VS Express installed.

Re: Using wodSSH with PowerShell?

by Sean, Wednesday, March 14, 2012, 22:32 (4434 days ago) @ Sean

Sean,


You need to use only username inside Login Property. What happens if you run our VBS sample inside samples folder.

Does it work maybe there?


Drazen

Thanks Drazen,

I'll give the VB sample a try now as I have VS Express installed.

Thought that was the case. Ok tried out a VBS sample and it worked perfectly - connected straight away to the exact same host I was testing the PowerShell code on. PowerShell is now working too though. I noticed that the VBS sample had this:

ssh.Protocol = 4

so in my PoSH one, I added:

$ssh.Protocol = 4

and it now gets further and manages the Connect() method this time. It also returns what I was after (a SHA1 fingerprint from my host), but it still throws back an error on the WaitFor part. See this error:

PowerCLI C:Program Files (x86)VMwareInfrastructurevSphere PowerCLI> $ssh.WaitFor( regex:[$ #>] $ )
Exception calling WaitFor with 1 argument(s): The current connection has timeout.
At line:1 char:13
+ $ssh.WaitFor <<<< ( regex:[$ #>] $ )
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation

Any ideas on that? I am guessing it is not getting what it wants in the regex pattern whilst waiting? When I connect via SSH to this host normally I get this (see this screenshot for the error I was talking about above too): http://dl.dropbox.com/u/450727/Temp/wodSSHexample.jpg

Re: Using wodSSH with PowerShell?

by wodDamir, Wednesday, March 14, 2012, 22:46 (4434 days ago) @ Sean

Sean,

Can you try regex statement like this: regex:[~$ #>] $ ??

Also, does it work if you set exact prompt that you receive?

Regards,
Damba

Re: Using wodSSH with PowerShell?

by Sean, Wednesday, March 14, 2012, 23:29 (4434 days ago) @ wodDamir

Sean,

Can you try regex statement like this: regex:[~$ #>] $ ??

Also, does it work if you set exact prompt that you receive?

Regards,
Damba

Hi Damba,

Ok tried that and still no luck. I also tried specifying just this:

ssh.WaitFor( ~ # )

or

ssh.WaitFor( ~ # )

those didn't work either. I read up on the WaitFor method in your documentation, and see that it takes two parameters - one is mandatory - the string we expect to wait for to indicate we are done collecting data, and then optional, timeout value. If timeout is not specified then it defaults to $ssh.Timeout (which I have tried as 15 seconds to 120 seconds).

So my WaitFor method is essentially timing out and not finding what its looking for I would say... This is odd as it should just find the prompt of the SSH shell surely?

Any other suggestions?

Would giving you remote ssh access to this box help? I really don't mind configuring that because it is a test box at home.

Sean

Re: Using wodSSH with PowerShell?

by wodDamir, Wednesday, March 14, 2012, 23:58 (4434 days ago) @ Sean

Sean,

What happens if you remove the DataReady property just before Waitfor call?

As for remote access, it would surely help since we actually see what's happening. You can send login information to techsupport@weonlydo.com

However, we'll be able to check it out tomorrow during the day (it's 00 am here, we're based in Croatia, UTC+1).

Regards,
Damba

Re: Using wodSSH with PowerShell?

by Sean, Thursday, March 15, 2012, 00:03 (4434 days ago) @ wodDamir

Sean,

What happens if you remove the DataReady property just before Waitfor call?

As for remote access, it would surely help since we actually see what's happening. You can send login information to techsupport@weonlydo.com

However, we'll be able to check it out tomorrow during the day (it's 00 am here, we're based in Croatia, UTC+1).

Regards,
Damba

Hi Damba

Thanks I'll give that a go and let you know. if not I'll send connection info to you guys. Croatia
- beautiful country! I went on holiday there last year :-)

Sean