Posh-SSH icon indicating copy to clipboard operation
Posh-SSH copied to clipboard

Can't pass command in a menu

Open ThomasArtig opened this issue 3 years ago • 9 comments

Hi, I'm working on the Cisco ISE (a network access control equipement), and i'm trying to generate a report via SSH.

i'm using this powershell script

Import-Module Posh-SSH
$password = ConvertTo-SecureString "PSSWR" -AsPlainText -Force
$credential = New-Object PSCredential ("USER", $password)

New-SSHSession -ComputerName "SERVER" -Credential $credential -AcceptKey -Verbose


$sw_ssh = Get-SSHSession -Index 0
$stream = $sw_ssh.Session.CreateShellStream("dumb", 0, 0, 0, 0, 1000)
$stream.Write("application configure ise`n")
$stream.Write("0`n")

$Response = $stream.Read()
$stream.Write("exit`n")

Remove-SSHSession -Index 0 | Out-Null

Write-Output $Response

the thing is after the application configure command this menu is prompt :

SERVER/@User# application configure ise

Selection configuration option
[1]Reset M&T Session Database
[2]Rebuild M&T Unusable Indexes
[3]Purge M&T Operational Data
[4]Reset M&T Database
[5]Refresh Database Statistics
[6]Display Profiler Statistics
[7]Export Internal CA Store
[8]Import Internal CA Store
[9]Create Missing Config Indexes
[10]Create Missing M&T Indexes
[11]Enable/Disable ACS Migration
[12]Generate Daily KPM Stats
[13]Generate KPM Stats for last 8 Weeks
[14]Enable/Disable Counter Attribute Collection
[15]View Admin Users
[16]Get all Endpoints
[17]Enable/Disable Wifi Setup
[18]Reset Config Wifi Setup
[19]Establish Trust with controller
[20]Reset Context Visibility
[21]Synchronize Context Visibility With Database
[22]Generate Heap Dump
[23]Generate Thread Dump
[24]Force Backup Cancellation
[25]CleanUp ESR 5921 IOS Crash Info Files
[26]Configure TCP params
[27]Recreate undotablespace
[28]Fetch SGA/PGA Memory usage
[0]Exit

And the script can't pass this menu, no error and no crash it's just blocked after the menu and wait for the Remove-SSHSession

ThomasArtig avatar May 11 '22 12:05 ThomasArtig

Are you sending carrige returns after each command? I see the n but not the backtick before it

Sent from my iPhone

On May 11, 2022, at 8:16 AM, ThomasArtig @.***> wrote:

 Hi, I'm working on the Cisco ISE (a network access control equipement), and i'm trying to generate a report via SSH.

i'm using this powershell script

`Import-Module Posh-SSH $password = ConvertTo-SecureString "PSSWR" -AsPlainText -Force $credential = New-Object PSCredential ("USER", $password)

New-SSHSession -ComputerName "SERVER" -Credential $credential -AcceptKey -Verbose

$sw_ssh = Get-SSHSession -Index 0 $stream = $sw_ssh.Session.CreateShellStream("dumb", 0, 0, 0, 0, 1000) $stream.Write("application configure isen") $stream.Write("0n")

$Response = $stream.Read() $stream.Write("exit`n")

Remove-SSHSession -Index 0 | Out-Null

Write-Output $Response`

the thing is after the application configure command this menu is prompt :

@.***# application configure ise

Selection configuration option [1]Reset M&T Session Database [2]Rebuild M&T Unusable Indexes [3]Purge M&T Operational Data [4]Reset M&T Database [5]Refresh Database Statistics [6]Display Profiler Statistics [7]Export Internal CA Store [8]Import Internal CA Store [9]Create Missing Config Indexes [10]Create Missing M&T Indexes [11]Enable/Disable ACS Migration [12]Generate Daily KPM Stats [13]Generate KPM Stats for last 8 Weeks [14]Enable/Disable Counter Attribute Collection [15]View Admin Users [16]Get all Endpoints [17]Enable/Disable Wifi Setup [18]Reset Config Wifi Setup [19]Establish Trust with controller [20]Reset Context Visibility [21]Synchronize Context Visibility With Database [22]Generate Heap Dump [23]Generate Thread Dump [24]Force Backup Cancellation [25]CleanUp ESR 5921 IOS Crash Info Files [26]Configure TCP params [27]Recreate undotablespace [28]Fetch SGA/PGA Memory usage [0]Exit And the script can't pass this menu, no error and no crash it's just blocked after the menu and wait for the Remove-SSHSession

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.

darkoperator avatar May 11 '22 12:05 darkoperator

When i send the 3 commands i use the same syntax : $stream.Write("application configure ise`n")

with the `n at the end.

ThomasArtig avatar May 11 '22 12:05 ThomasArtig

I just saw if you look at my post via Email the "backtick" doesn't show up, but they are here !

ThomasArtig avatar May 11 '22 12:05 ThomasArtig

When you read back the stream is there a menu refresh or the command at the end of the menu? Dont have any networking kit to test

Sent from my iPhone

On May 11, 2022, at 8:24 AM, ThomasArtig @.***> wrote:

 When i send the 3 commands i use the same syntax : $stream.Write("application configure isen")`

the `n at the end.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.

darkoperator avatar May 11 '22 13:05 darkoperator

Here is the console logs when i run the script above :

PS C:\Users\ME> Import-Module Posh-SSH
$password = ConvertTo-SecureString "PSSWRD" -AsPlainText -Force
$credential = New-Object PSCredential ("USER", $password)

New-SSHSession -ComputerName "SERVER" -Credential $credential -AcceptKey -Verbose


$sw_ssh = Get-SSHSession -Index 0
$stream = $sw_ssh.Session.CreateShellStream("dumb", 0, 0, 0, 0, 1000)
sleep 3
$stream.Write("application configure ise`n")
sleep 3

$stream.Write("0`n")

$stream.Write("exit`n")
$Response = $stream.Read()

Remove-SSHSession -Index 0 | Out-Null

Write-Output $Response
VERBOSE: Using SSH Username and Password authentication for connection.
VERBOSE: ssh-rsa Fingerprint for SERVER: KEY
VERBOSE: Fingerprint matched trusted ssh-rsa fingerprint for host SERVER

SessionId  Host                                                                                                     Connected                                                                                         
---------  ----                                                                                                     ---------                                                                                         
    0      SERVER                                                                                                       True                                                                                           
Last login: Wed May 11 13:13:14 2022 from 10.0.X.X
[r[m[2J[H[?7h[?1;3;4;6l[4l[4l=[0m(B[1;24r[H[2J[H[2JFailed to log in 2 time(s)
Last failed login on Tue Apr 26 13:46:00 2022 from 10.112.16.135
SERVER/USER# application configure ise

Selection configuration option
[1]Reset M&T Session Database
[2]Rebuild M&T Unusable Indexes
[3]Purge M&T Operational Data
[4]Reset M&T Database
[5]Refresh Database Statistics
[6]Display Profiler Statistics
[7]Export Internal CA Store
[8]Import Internal CA Store
[9]Create Missing Config Indexes
[10]Create Missing M&T Indexes
[11]Enable/Disable ACS Migration
[12]Generate Daily KPM Stats
[13]Generate KPM Stats for last 8 Weeks
[14]Enable/Disable Counter Attribute Collection
[15]View Admin Users
[16]Get all Endpoints
[17]Enable/Disable Wifi Setup
[18]Reset Config Wifi Setup
[19]Establish Trust with controller
[20]Reset Context Visibility
[21]Synchronize Context Visibility With Database
[22]Generate Heap Dump
[23]Generate Thread Dump
[24]Force Backup Cancellation
[25]CleanUp ESR 5921 IOS Crash Info Files
[26]Configure TCP params
[27]Recreate undotablespace
[28]Fetch SGA/PGA Memory usage
[0]Exit

PS C:\Users\ME>

As you can see the "0" command is not even pushed. (i added some sleep and moved the $Response = $stream.Read())

ThomasArtig avatar May 11 '22 13:05 ThomasArtig

Hey,

I'm back on the subject, any update or idea on the issue ?

ThomasArtig avatar Jun 22 '22 09:06 ThomasArtig

Sadly no way to replicate on my end, only thing I can say would be to change the the sleep for a stream read and check that the string for the menu is present to be sure its not a timing issue. But outside of that no clue.

darkoperator avatar Jun 22 '22 09:06 darkoperator

I didn't think of reading the stream and add a condition, do you have any exemple of code that i can replicate and try this solution ?

ThomasArtig avatar Jun 22 '22 09:06 ThomasArtig

Check the code from the invoke-sshshellstreamcommand function

Sent from my iPhone

On Jun 22, 2022, at 5:12 AM, ThomasArtig @.***> wrote:

 Hey,

I'm back on the subject, any update or idea on the issue ?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.

darkoperator avatar Jun 22 '22 10:06 darkoperator