Quantcast
Channel: sshnet Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1729

New Post: "An established connection was aborted by the software in your host machine." after establishing a SSH connection to a juniper firewall

$
0
0
I ran into this exact issue, and threw together some solutions I found scattered around. As this issue seems to be limited to specific Juniper network devices, as this thread is the top result in searches for "Juniper SSH.Net" maybe posting my solution here will help some other hapless souls like myself.

Utilizing this Powershell SSH.Net module and the latest SSH.Net (for .net 3.5, but also tested and validated against 4.0).

This is my Powershell solution for automating running commands against my Juniper appliance:
function RunSSHCommand(){
    param(
        [string]$computername,
        [string]$sshcommand
    );
    if($global:sshStream -eq $null){
        $global:sshStream=($global:sshsessions).$computername.CreateShellStream(([string](new-object system.random).next()).Substring(0,5), 80, 24, 800, 600, 1024);
    }
    if($global:sshStream -ne $null){
        $output="";
        $reader= New-Object -typename System.IO.StreamReader -argumentlist $global:sshStream ;
        $writer= New-Object -typename System.IO.StreamWriter -argumentlist $global:sshStream ;
        $writer.autoflush=$true;
        
        $writer.WriteLine(" ");$writer.flush();
        for($i=0;$i -lt 10;$i++){if($global:sshStream.length -eq 0){start-sleep -Milliseconds 250;}else{break;}}
        $line=$reader.readline();
        while($line -ne $null){$line;$line=$reader.readline();}
        
        $writer.WriteLine($sshcommand);$writer.flush();
        while($global:sshStream.length -eq 0){start-sleep -Milliseconds 250;}
        $line=$reader.readline();
        while($line -ne $null){$output+=$line;$line;$line=$reader.readline();}
        <#
        $writer.close();
        $reader.close();
        $stream.close();$stream.dispose(); 
        #>
    }else{
        "RunSSHCommand:Failed. Unable to CreateShellStream against SSHSession($computername)";
    }
if($output -ne ""){return $output;}
}

import-module ssh-sessions;
$computername="192.168.1.254";
new-sshsession -computername $computername -username netscreen;
RunSSHCommand -computername $computername -sshcommand "get system version";
RunSSHCommand -computername $computername -sshcommand "get admin user login";
remove-sshsession -computername $computername;
[[Yes, i'm aware the coding practice of using Globals isn't epic, but its functional and doesn't violate the environment i'm working in, please feel free to modify it to suit your own needs]]

Cheer,
-TheJonnyG

Viewing all articles
Browse latest Browse all 1729

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>