Home Programming MS PowerShell PowerShell: How to use SSH.NET Library for Cisco on Windows 7

As a network engineer, Expect script is handy on linux environment. Since working on Windows environment with limited power to install 3rd party software. Microsoft PowerShell is one of best option for alternative. I have tried Activestate perl with Expect module and plink.exec with "-m" option to use commands. However, partially worked or constantly met limitation and comparibility issue. Ended up, decided to find something always works. Here is what I found.

 

PowerShell : How to use SSH.NET Library on Windows 7

 

1. Download

Download SSH.NET Library from below Link.

http://www.powershelladmin.com/wiki/SSH_from_PowerShell_using_the_SSH.NET_library

"Download Script Module and DLL file: SSH-SessionsPSv3.zip"

 

1-1. Unzip it

1-2. Create a folder name as "SSH-Sessions" under

C:\Windows\System32\WindowsPowerShell\v1.0\Modules

* In order to create a folder under the System32 directoy, it requires admin priviliege. If you don't have admin priviliege, create a folder under user dictory. Then you need to update "PS envi path" 

 

1-3. Open powershell console by start > windows powersheel.

1-4. From prompt >

PS C:\WINDOWS\system32\windowspowershell\v1.0\Modules> Import-Module SSH-Sessions

 

1-5. Make sure "New SshSession" cmlet from list.

 * Also, make sure your power shell is version 3.

 

 

 

2. Import Module

PS C:\MyScripts\Import-Module SSH-Sessions

Now, ready to use SSH.NET module on your script.

 

3. Scripting example

 

3-1. Send a command thru ssh to Cisco router & switch

 

Param($hostname,$username,$password)

# Host device IP & logon credentials
$hostname = "x.x.x.x"
$username = "TheAdmin"
$password = "P@ssw0rd"

 

# Confirm "Import-Module" might be missed.
Import-Module SSH-sessions

 

New-Sshsession -computername $hostname -username $username -password $password
$Result = Invoke-SshCommand -InvokeOnAll -Command "sh arp" > c:\tools\output.txt

Remove-SshSession -computername $hostname

 

* Do not change name of variable, it might not work as it should.

 

 

3-2. Send multiple commands thru ssh to Cisco router & switch

 

Param($hostname,$username,$password)

# Host device IP & logon credentials
$hostname = "x.x.x.x"
$username = "TheAdmin"
$password = "P@ssw0rd"

 

# Confirm "Import-Module" might be missed.
Import-Module SSH-sessions

New-Sshsession -computername $hostname -username $username -password $password
$Result0 = Invoke-SshCommand -InvokeOnAll -Command "terminal length 0"

New-Sshsession -computername $hostname -username $username -password $password
$Result1 = Invoke-SshCommand -InvokeOnAll -Command "show arp"

New-Sshsession -computername $hostname -username $username -password $password
$Result2 = Invoke-SshCommand -InvokeOnAll -Command "show ip interface brief"

New-Sshsession -computername $hostname -username $username -password $password
$Result3 = Invoke-SshCommand -InvokeOnAll -Command "show running-config"

New-Sshsession -computername $hostname -username $username -password $password
$Result4 = Invoke-SshCommand -InvokeOnAll -Command "show proc cpu"

$Result1, $Result2, $Result3, $Result4 | Out-File C:\MyScripts\output.txt

Remove-SshSession -RemoveAll (or computername $hostname)

 

 

3-3. Send multiple commands thru ssh to multiple Cisco routers & switches with CVS file.

 

Param($hostname,$username,$password)

# Host device IP & logon credentials
$hostname = "x.x.x.x"
$username = "TheAdmin"
$password = "P@ssw0rd"

 

# Confirm "Import-Module" might be missed.
Import-Module SSH-sessions

 

# Bring Host or Cisco Devices IP from *.csv file( named "Host-IP-list.csv").
$DeviceList = Import-Csv c:\MyScripts\Host-IP-list.csv
foreach ($IP_add in $Devicelist)
{
$hostname = $IP_add.IPAddress; #IP is IP address

New-Sshsession -computername $hostname -username $username -password $password
$Result0 = Invoke-SshCommand -InvokeOnAll -Command "terminal length 0"

New-Sshsession -computername $hostname -username $username -password $password
$Result1 = Invoke-SshCommand -InvokeOnAll -Command "show arp"

New-Sshsession -computername $hostname -username $username -password $password
$Result2 = Invoke-SshCommand -InvokeOnAll -Command "show ip interface brief"

New-Sshsession -computername $hostname -username $username -password $password
$Result3 = Invoke-SshCommand -InvokeOnAll -Command "show running-config"

New-Sshsession -computername $hostname -username $username -password $password
$Result4 = Invoke-SshCommand -InvokeOnAll -Command "show proc cpu"

$Result1, $Result2, $Result3, $Result4 | Out-File C:\MyScripts\$hostname'output.txt'

Remove-SshSession -RemoveAll (or computername $hostname)
}

 

3-4. Send multiple commands thru ssh to multiple Cisco routers & switches in sequential IPs.

 

Param($hostname,$username,$password,$IP_node)

# Host device IP & logon credentials
$IP_node = 1
$IP_net = "192.168.88."
$username = "chris"
$password = "Speeder99"

# Confirm "Import-Module" might be missed.
Import-Module SSH-sessions

while ( $IP_node -le 20 )
{
$hostname = $IP_net+$IP_node
 
New-Sshsession -computername $hostname -username $username -password $password
$Result0 = Invoke-SshCommand -InvokeOnAll -Command "terminal length 0"

New-Sshsession -computername $hostname -username $username -password $password
$Result1 = Invoke-SshCommand -InvokeOnAll -Command "show arp"

New-Sshsession -computername $hostname -username $username -password $password
$Result2 = Invoke-SshCommand -InvokeOnAll -Command "show ip interface brief"

New-Sshsession -computername $hostname -username $username -password $password
$Result3 = Invoke-SshCommand -InvokeOnAll -Command "show run"

New-Sshsession -computername $hostname -username $username -password $password
$Result4 = Invoke-SshCommand -InvokeOnAll -Command "show proc cpu"

$Result1, $Result2, $Result3, $Result4 | Out-File C:\MyScripts\$hostname'output.txt'

Remove-SshSession -RemoveAll (or computername $hostname)

$IP_node = $IP_node + 1
}

 

 

4. Troubleshooting  / Error messages

4-1. 'Unable to connect to x.x.x.x: Exception calling "Connect" with "0" argument(s): "User cannot be authenticated"'

; Check remote host that is reached max SSH sessions or max failure attempt count.

 

4-2.  'Unable to connect to x.x.x.x: Exception calling "Connect" with "0" argument(s): "Server string is null or empty"'

; Try different syntex from x.x.x.x to 'x.x.x.x'

 

 

4-3.  Some of SSH server(deamon) won't work / understand a string from SSH client in somehow. Ex) Nortel 1104e VoIP phone. 

Change from

$hostname = "x.x.x.x"
$username = "TheAdmin"
$password = "P@ssw0rd"

 To

$hostname = 'x.x.x.x'
$username = 'TheAdmin'
$password = 'P@ssw0rd'

 

 

 

 

Last Updated (Wednesday, 10 February 2016 08:51)

 
Statistics
Content View Hits : 1473473
Search
Related Items
Polls
Highly recommended firewall vendor?
 
Google Translation
English Arabic Chinese (Simplified) Czech Dutch French German Italian Korean Portuguese Russian Spanish Filipino Vietnamese Thai Turkish
BGP routing issue?
Banner
World Route Servers
Banner
Who's Online
We have 109 guests online
network monitoring tool