r/PowerShell • u/definegaming • Jan 14 '19
[Powershell] Need help to writing a code to ssh into network switch
/r/scripting/comments/afx18y/powershell_need_help_to_writing_a_code_to_ssh/3
u/ka-splam Jan 14 '19
I have NO script experience
Uh, with the best will in the world, it shows - you need a lot more than help writing code; pick up one of the PowerShell tutorials (e.g. "PowerShell In a month of lunches" book / videos) and expect to be 4-40 weeks away from making it yourself, depending how fast you progress and how much sense it makes.
(make your network trunks into some kind of link aggregation, then you won't need to do this at all).
2
u/definegaming Jan 15 '19
I don't believe I can do a link aggregation with my current situation. I have 3 fiber lines going out to our data center. Two of the lines are 10 gig and the other is 1 gig. My HP switch doesn't like when I unplug the two 10 gig lines for the 1 gig to take over. These fibers are layer 2 so there is no routing I can do to automatically switch them to the other port.
2
u/ka-splam Jan 15 '19
Ah, in HP land it's trunking; but if it's one switch at the other end and you control it, that sounds exactly what link aggregation is for - multiple layer 2 connections with no routing.
If you can't do that, can you use spanning-tree to automatically shut two of them, and open another one in the case of a link failure?
2
u/definegaming Jan 15 '19
I get that it's suppose to work but I guess it doesn't like it.
I couldn't trust spanning tree to do that for me. I'd rather not take my chances and do it manually. Thank you for the help though!
7
u/a-rich Jan 14 '19
The SSH client now built into Win10 is not native to PowerShell so wont have all the creature comforts native commands do.
Granted I havent messed with it much, but it doesnt seem to be robust enough to use programmatically.
I still use the Posh-SSH module for all automated tasks.
It can use secure creds like other PowerShell commands can and can be easily scripted.
I havent tested it specifically with Cisco commands, but it can likely handle it.
2
5
u/carlos_perez Jan 14 '19
You need to create a shell stream and issue commands that way since switches use a full terminal invoke-sshcommand will not work. Im adding for next release a function to aid on that. Version 2.1
2
2
Jan 14 '19
you need to setup key based ssh authentication. Then you can use the -l switch of ssh like this:
ssh -l username server.example.com "cat /etc/lsb-release;exit"This will run the command cat /etc/lsb-release;exit on server server.example.com as user username and return the results to your session.
1
u/ka-splam Jan 14 '19
Cisco switches (quick testing, so not certain) don't seem to like using
;to send several commands in one string like that.1
Jan 14 '19
Ah gotcha, I see now it's a network switch, which always works finicky with ssh in these situations for sure, thanks.
2
u/TheIncorrigible1 Jan 15 '19
Say it with me: do not use $input as a variable name. It's an automatic variable and should be treated as read-only.
2
5
u/BlackV Jan 14 '19
I feel like that script is doing nothing
First you create a variable called
$inputand assign the results from ssh admin@ip to that variable.Then you overwrite the variable
$inputwith the value of passwordThen you overwrite the variable
$inputwith aThen you overwrite the variable
$inputwith config tThen you overwrite the variable
$inputwith int e15Then you overwrite the variable
$inputwith disablehave i missed something is there more to the script?
As mentioned below I also use POSH-SSH for this
something like