Ever need to automate a command on a Cisco device? We recently had issues with offsite backups because our MPLS link was only 5Mbit, but our VPN mesh has a 100mib pipe. Well offsite backups would saturate the link and would take more than the weekend to complete. We use this powershell script to disable our MPLS link to allow backups to complete in a timely manner, after they complete there is another script that enables the interface again.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# Name: ShutdownMPLS.ps1 # Author: Dylan Zeleski # Company: # Date: 7/23/14 ################################################################################################# # Description: Script connects to cisco device and runs commands # ################################################################################################# ################################################################################################# # Requirements: Powershell 3.0, .Net 4.0 Posh-SSH module # ################################################################################################# #Set Creds $secpasswd = ConvertTo-SecureString "USERNAME" -AsPlainText -Force $creds = New-Object System.Management.Automation.PSCredential ("PASSWORD", $secpasswd) #Build SSH session New-SSHSession -ComputerName "HOSTNAMEORIP" -Credential $creds -AcceptKey $true #Build open stream for use in cisco devices $session = Get-SSHSession -Index 0 $stream = $session.Session.CreateShellStream("dumb", 0, 0, 0, 0, 1000) $stream.Write("config t`n") $stream.Write("int gig 0/2`n") $stream.Write("shutdown`n") $stream.Write("do logout`n") $stream.Write("logout`n") Remove-SSHSession -Index 0 |
How do I get output of the above result?
$stream.read()
Hey man, thanks for this, very useful, going to automate a lot of things with this.
I previously used to work a lot with Telnet but boss wants to finally make the move to SSH and your little script there just saved me a lot of research.
No problem!