Windows-Server-2008
Cisco 配置備份或使用 Windows 腳本重新啟動
我們有一個擁有大量 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
但是這個問題是發送鍵被發送到控制台會話,我試圖找到一個不需要使用者登錄的解決方案。
有沒有人有任何想法?我對 VBS、PowerShell 和批處理有一定的了解。
我想通了(這個會重新啟動它們,但它可以很容易地重新加密以進行備份)這是一個 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 }
我懷疑它是否完美,但它確實有效。