Cisco Configuration backup or Reboot with Windows Script

5,517

Solution 1

I figured it out(this one reboots them but It can easily be rekeyed for backup) This is a PowerShell Script

#param([String] $remoteHost =$(throw "Please specify the Target Server"),[String] $domain = $(throw "Please specify the #recipient Domain"),[String] $sendingdomain = $(throw "Please specify the Sending Domain"))

param([String] $remoteHost,[String] $domain, [String] $sendingdomain)
$remotehosts ="List","Of","Cisco","IPs"
$theUn = "UserName"
$thePw = "Password"



function readResponse {

while($stream.DataAvailable)
{
$read = $stream.Read($buffer, 0, 1024)
write-host -n -foregroundcolor cyan ($encoding.GetString($buffer, 0, $read))
""
}
}

$port = 23

foreach($remoteHost in $remoteHosts)
{
$socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port)
if($socket -eq $null) { return; }

$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter($stream)
$buffer = new-object System.Byte[] 1024
$encoding = new-object System.Text.AsciiEncoding 

$command = $theUn
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 3000
readResponse($stream)

write-host -foregroundcolor DarkGreen $command
""
$command = $thePw
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 2000
readResponse($stream)
write-host -foregroundcolor DarkGreen $command
""

$command = "en"
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 2000
readResponse($stream)=
write-host -foregroundcolor DarkGreen $command
""

$command = $thePw
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 2000
readResponse($stream)
write-host -foregroundcolor DarkGreen $command
""
$command = "wr"
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 5000
readResponse($stream)
write-host -foregroundcolor DarkGreen $command
""
$command = ""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 2000
readResponse($stream)
write-host -foregroundcolor DarkGreen $command
""

$command = "reload"
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 2000
readResponse($stream)
write-host -foregroundcolor DarkGreen $command
""

$command = ""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 2000
readResponse($stream)
write-host -foregroundcolor DarkGreen $command
""

$writer.Flush()

readResponse($stream)
## Close the streams
$writer.Close() 
start-sleep -m 120000
}

I doubt it's perfect, but it defiantly works.

Solution 2

This doesn't exactly answer your question, but I use Kiwi CatTools for this very thing.

http://www.kiwisyslog.com/kiwi-cattools-overview/

Share:
5,517

Related videos on Youtube

Jeff
Author by

Jeff

I am a LAMP Developer for Orlandobaking.(orlandobaking.com)

Updated on September 17, 2022

Comments

  • Jeff
    Jeff almost 2 years

    We have a client with a lot of Cisco Devices and we would like to automate the backups of these devices through telnet. We have both 2003 and 2008 servers and ideally use tftp to back it up.

    I wrote this:

    Set WshShell = WScript.CreateObject("WScript.Shell") 
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Dim ciscoList
    ciscoList = "D:\Scripts\SwitchList.txt"
    
    Set theSwitchList = fso.OpenTextFile(ciscoList, 1)
    
    Do While theSwitchList.AtEndOfStream <> True
    cisco = theSwitchList.ReadLine
    Run "cmd.exe"
    SendKeys "telnet " 
    SendKeys  cisco
    SendKeys "{ENTER}"
    SendKeys "USERNAME"
    SendKeys "{ENTER}"
    SendKeys "PASSWORD"
    SendKeys "{ENTER}"
    SendKeys "en"
    SendKeys "{ENTER}"
    SendKeys "PASSWORD" 
    SendKeys "{ENTER}" 
    SendKeys "copy startup-config tftp{ENTER}"
    SendKeys "(TFTP IP){ENTER}"
    SendKeys "FileName.txt{ENTER}"
    SendKeys "exit{ENTER}" 'close telnet session' 
    SendKeys "{ENTER}" 'get command prompt back 
    SendKeys "{ENTER}"
    SendKeys "exit{ENTER}" 'close cmd.exe
    On Error Resume Next
      WScript.Sleep 3000
    Loop
    Sub SendKeys(s)
      WshShell.SendKeys s
      WScript.Sleep 300
    End Sub
    
    Sub Run(command)
      WshShell.Run command
      WScript.Sleep 100 
      WshShell.AppActivate command 
      WScript.Sleep 300 
    End Sub
    

    But the problem with this is the sendkeys are sent to the console session, I'm trying to find a solution that would not require a user to be logged in.

    Does anyone have any ideas? I have some knowlage of VBS, PowerShell and a pretty good grasp on batching.

    • jj33
      jj33 over 13 years
      I'm adding this as a comment because it doesn't answer your specific question, but you should consider using rancid to do these backups. It's versioned, so you have a history of changes, it has a community to support it, and most importantly, it will back up stuff that tftp won't (vlan DB, hardware characteristics, etc)
    • Jeff
      Jeff over 13 years
      It needs to be a Windows solution, unfortunately. I could only find documentation for Linux or OSX.
  • Jeff
    Jeff over 13 years
    I've seen that. If we really need to we can purchase this. I'd rather see if it's possible to script it.
  • joeqwerty
    joeqwerty over 13 years
    They do have a free version...