2

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.

Jeff
  • 685
  • 7
  • 17
  • 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) – jj33 Jan 12 '11 at 17:00
  • It needs to be a Windows solution, unfortunately. I could only find documentation for Linux or OSX. – Jeff Jan 12 '11 at 17:09

3 Answers3

2

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.

Jeff
  • 685
  • 7
  • 17
1

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

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

joeqwerty
  • 109,901
  • 6
  • 81
  • 172
0

Have you checked the archive directive of the IOS ? You could point it to a tftp server and every time a config is changed it just archive.. http://www.cisco.com/en/US/docs/ios/12_3t/fun/command/reference/cfrgt_01.html#wp1094316

voodooo
  • 254
  • 2
  • 6