How do I pass commands to CLI within ssh session?

I am building a PS script to do things with Sonicwalls, like add address objects etc., script is PS7 compatible so I can use SSH.
I have a foreach loop to go through the Sonicwalls, manually enter password.
Then it goes to the Admin prompt and it’s at this point I do not know how to make PS send a command to the Sonicwall, e.g. configure, then subsequent commands to add address objects etc.
I know I could paste in all the commands but I want PS to put in the commands.
Or have PS paste text from a file??
Or do I ‘pipe’ the commands somehow??
I think invoke-command is what I need but Idk how to specify it properly, I’m new to this level of PS scripting.
Thank you, Tom

example code, all sonicwalls are LINUX or similar:

foreach ($IP in $IPs) {
    $address = 'admin@172.' + $IP + '.0.1'
    write-host $address
    ssh $address
# after this I manually type the password and the admin prompt appears
# I want to know how to make PS put in the commands below after the admin prompt appears...its the actual sonicwall admin cli interface
# this is an example what commands should go in after typing password
    configure    # should go to config prompt
    show access-rules   # example command
}

Thank you, Tom

What it looks like after I log in:

admin@172.16.0.1
admin@172.16.0.1's password:
Copyright (c) 2019 SonicWall, Inc.
Using username 'admin'.
Password:
admin@0017C51A37>

I want the PS script to send configure to the admin prompt, which makes the prompt change to a config mode prompt, then send more commands…

Thank you, Tom

I’ve tried numerous things.
It’s like after I put in my password, it no longer ‘knows’ anything about PS etc.
It just sits at the admin prompt and nothing happens until I type something.
I can manually paste lines in but I want PS to put the lines in.
Please help!!
I need to automate a couple hundred lines of Sonicwall code.
Thank you, Tom

Not exactly a PowerShell solution, but you might have better luck using the Windows Bash shell for this.

Sonicwalls have proprietary OSes…I checked, there’s no bash, ssh only. It’s a firewall after all. :slight_smile:

There is Bash in windows. Install it. Then, do your SSH from there:

Again, not PowerShell … but it looks pretty easy.

I installed the things and will reboot later. Meanwhile I shall remain skeptical. :slight_smile: Sonicwalls are hard to work with.

Hey, if you dont mind, let us know if you found a solution. Thanks.

I believe the Posh-SSH module may help. You can maintain sessions with it. Maybe worth checking out

Thank you for the idea…even though this is becoming superseded by openssh included into powershell etc. etc.
However all I know about this is that it exists, it’s a module.
I found and installed it…I think. :slight_smile:
Where can I find something (tutorial!!) about establishing a session and sending commands to the session??
Sonicwalls are very hard to work with.
Thank you, Tom

I now know how to create the ssh session with posh-ssh. :slight_smile:
What I don’t know and need guidance with is how to use posh-ssh to send the commands, e.g. configure, carriage return (e.g. go to the next line), enter next command, please see code posted above.
Thank you, Tom

I did find some things and have started working out how to script what I need. Thank you, everyone. :slight_smile:

Sorry, I must have missing something here. My suggestion to use SFU and Bash should have been as simple as following the instructions on the link I sent. Did that not work?

Just curious. I did not test, was basing my suggestion on Theory :slight_smile:

Hello, month later. :slight_smile:
Basically solution is like this:

#get ssh information
$ServerIP = "172.x.x.x"
#$Credential = New-Object System.Management.Automation.PSCredential ($UserID, $Password)
$SessionSSH = New-SSHSession -ComputerName $ServerIP -Credential (Get-Credential admin)
Get-SSHSession | fl
$session = Get-SSHSession -Index 0
# $stream is normally 1000 for the final number, changed it to 3000
$stream = $session.Session.CreateShellStream("dumb", 0, 0, 0, 0, 3000)
#$stream.Write("show ver`n")
Start-Sleep 1
$stream.Write("no cli pager session`n") #keep console from breaking with too much output
Start-Sleep 1
# HOSTS
Start-Sleep 1
$stream.write("configure`n")
Start-Sleep 1
$stream.Write("yes`n") # preempts previous editing session if necessary
Start-Sleep 1
$stream.write("address-object ipv4 O365_Host1 host 13.107.140.6 zone WAN`n")
Start-Sleep 1
$stream.write("address-object ipv4 O365_Host2 host 13.107.18.15 zone WAN`n")
Start-Sleep 1
$stream.write("exit`n")
Start-Sleep 1
$stream.write("yes`n")
Start-Sleep 1
$stream.write("configure`n")
Start-Sleep 1
# O365 GROUP
$stream.write("address-group ipv4 O365_Group`n")
Start-Sleep 1
$stream.write("address-object ipv4 O365_Host1`n")
Start-Sleep 1
$stream.write("exit`n")
Start-Sleep 1
$stream.write("exit`n")
Start-Sleep 1
$stream.write("yes`n")
Start-Sleep 1

Etc. etc.
You may have to adjust the quantity of exit statements involved.
Actual script with all code is about 1200 lines something.
The FQDNs must then be added to the address group manually, SonicOS 5.x and 6.5.x don’t do this with code.

HTH :slight_smile: tom

You could reduce a lot of that code by doing something like this:

$commandList = @(
    "configure`n"
    "yes`n"
    "address-object ipv4 O365_Host1 host 13.107.140.6 zone WAN`n"
    "address-object ipv4 O365_Host2 host 13.107.18.15 zone WAN`n"
    "exit`n"
    "yes`n"
    "configure`n"
)

foreach ($command in  $commandList) {
    $stream.write($command)
    Start-Sleep 1
}

I apologize for not responding sooner to anything here.

@matt-bloomfield – I understand your suggestion but it took so long to get the script set up and I do not need to worry about execution time so my way works for me fine. Also the script involved I only must run it once per Sonicwall and never again…except if I need to script new things, for which I could try the above command list…OTOH there’s 100+ objects involved.

Thank you, Tom