简体   繁体   English

Telnet的Powershell命令替代品,用于管理路由器

[英]Powershell command alternative to Telnet for managing router

  1. I need to reboot my wireless router (ZyXEL P-660HN-T1A) at a scheduled time (using task scheduler in Windows 7). 我需要在计划的时间(使用Windows 7中的任务计划程序)重新引导我的无线路由器(ZyXEL P-660HN-T1A)。

  2. The live and manual telnet command-line version to the task above is this: 上面任务的实时和手动telnet命令行版本是这样的:

     telnet 192.168.1.1 password: ********** sys reboot exit 
  3. Now I want alternatively to use Powershell to automate and script (using probably TcpClient) the above commands and then run that script at the required time with Windows Task Scheduler. 现在,我想替代地使用Powershell来自动化和脚本化(可能使用TcpClient)上述命令,然后在所需时间使用Windows Task Scheduler运行该脚本。

Some time ago I found a proper script here: https://github.com/chrisdee/Scripts/blob/master/PowerShell/Working/telnet/PowerShellTelnetRemoteSession.ps1 前一段时间,我在这里找到了合适的脚本: https : //github.com/chrisdee/Scripts/blob/master/PowerShell/Working/telnet/PowerShellTelnetRemoteSession.ps1

## PowerShell: Script To Telnet To Remote Hosts And Run Commands Against Them With Output To A File ##
## Overview: Useful for Telnet connections to Cisco Switches and other devices. Can add additional command strings
## Usage Examples: Add your environment specific details into the parameters below, or include when calling the script:
## ./PowerShellTelnetRemoteSession.ps1 "127.0.0.1" "23" "admin" "password" "term len 0" "en" "enablepassword" "show interface"
param(
[string] $remoteHost = "",
[int] $port = 23,
[string] $username = "",
[string] $password = "",
[string] $termlength = "term len 0", #Useful for older consoles that have line display limitations
[string] $enable = "en", #Useful for appliances like Cisco switches that have an 'enable' command mode
[string] $enablepassword = "",
[string] $command1 = "show interface", #You can add additional commands below here with additonal strings if you want
[int] $commandDelay = 1000
)
[string] $output = ""
## Read output from a remote host
function GetOutput
{
## Create a buffer to receive the response
$buffer = new-object System.Byte[] 1024
$encoding = new-object System.Text.AsciiEncoding
$outputBuffer = ""
$foundMore = $false
## Read all the data available from the stream, writing it to the
## output buffer when done.
do
{
## Allow data to buffer for a bit
start-sleep -m 1000
## Read what data is available
$foundmore = $false
$stream.ReadTimeout = 1000
do
{
try
{
$read = $stream.Read($buffer, 0, 1024)
if($read -gt 0)
{
$foundmore = $true
$outputBuffer += ($encoding.GetString($buffer, 0, $read))
}
} catch { $foundMore = $false; $read = 0 }
} while($read -gt 0)
} while($foundmore)
$outputBuffer
}
function Main
{
## Open the socket, and connect to the computer on the specified port
write-host "Connecting to $remoteHost on port $port"
trap { Write-Error "Could not connect to remote computer: $_"; exit }
$socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port)
write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter $stream
## Receive the output that has buffered so far
$SCRIPT:output += GetOutput
$writer.WriteLine($username)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($termlength)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($enable)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($enablepassword)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command1) #Add additional entries below here for additional 'strings' you created above
$writer.Flush()
Start-Sleep -m $commandDelay
$SCRIPT:output += GetOutput
## Close the streams
$writer.Close()
$stream.Close()
$output | Out-File ("C:\BoxBuild\$remoteHost.txt") #Change this to suit your environment
}
. Main

You could easily modify the value of $command1 variable to output your required commands. 您可以轻松地修改$ command1变量的值以输出所需的命令。 I also removed the $username input and changed the values of $password, $remoteHost, $port to the required ones. 我还删除了$ username输入,并将$ password,$ remoteHost,$ port的值更改为所需的值。

For the part of scheduling the task, after saving the edited script mentioned above, I ran it through a command like the following with the maximum privileges from a Task Scheduler event in windows: 对于计划任务,在保存了上面提到的编辑脚本之后,我通过以下命令使用Windows中的Task Scheduler事件的最大特权运行了以下命令:

powershell -executionpolicy bypass -file "C:\PowerShellTelnetRemoteSession.ps1"

"-executionpolicy bypass" is so to bypass powershell security for unsigned scripts. “ -executionpolicy旁路”是为了绕过未签名脚本的PowerShell安全性。

If you use Cisco UCS it has a module for PS management of devices. 如果使用Cisco UCS,则具有用于设备PS管理的模块。 Not very familiar with it, I just know it exists. 对它不是很熟悉,我只是知道它的存在。

http://blogs.cisco.com/tag/powershell/ http://blogs.cisco.com/tag/powershell/

http://www.cisco.com/c/en/us/td/docs/unified_computing/ucs/sw/msft_tools/powertools/ucs_powertool_book/ucs_pwrtool_bkl1.pdf http://www.cisco.com/c/en/us/td/docs/unified_computing/ucs/sw/msft_tools/powertools/ucs_powertool_book/ucs_pwrtool_bkl1.pdf

This guy talks about about some other options too.. 这个家伙也在谈论其他一些选择。

http://www.orcsweb.com/blog/james/fun-with-powershell-plink-cisco-ios-and-powershell/ http://www.orcsweb.com/blog/james/fun-with-powershell-plink-cisco-ios-and-powershell/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM