使用 Windows 脚本备份或重启 Cisco 配置

使用 Windows 脚本备份或重启 Cisco 配置

我们有一个拥有大量 Cisco 设备的客户,我们希望通过 telnet 自动备份这些设备。我们拥有 2003 和 2008 服务器,理想情况下使用 tftp 进行备份。

我写了这个:

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

但问题是 sendkeys 被发送到控制台会话,我正在尝试寻找一种不需要用户登录的解决方案。

有人有什么想法吗?我对 VBS、PowerShell 有一些了解,并且对批处理有很好的掌握。

答案1

我找到了答案(这个会重启它们,但可以很容易地重新输入密钥进行备份)这是一个 PowerShell 脚本

#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
}

我怀疑它是否完美,但它确实有效。

答案2

这并不能准确回答您的问题,但我使用 Kiwi CatTools 就是为了解决这个问题。

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

答案3

您检查过 IOS 的存档指令吗?您可以将其指向 tftp 服务器,每次更改配置时,它都会存档。http://www.cisco.com/en/US/docs/ios/12_3t/fun/command/reference/cfrgt_01.html#wp1094316

相关内容