我需要在预定时间重新启动无线路由器(ZyXEL P-660HN-T1A)(使用Windows 7中的任务调度程序)。
上述任务的实时和手动 telnet 命令行版本是:
telnet 192.168.1.1
password: **********
sys reboot
exit
现在我想要使用 Powershell 来自动化和编写脚本(可能使用TcpClient)上面的命令,然后在Windows任务计划程序的所需时间运行该脚本。
答案 0 :(得分:1)
前段时间我在这里找到了一个合适的脚本: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
您可以轻松修改$ command1变量的值以输出所需的命令。 我还删除了$ username输入并将$ password,$ remoteHost,$ port的值更改为所需的值。
对于安排任务的部分,在保存上面提到的编辑过的脚本之后,我通过以下命令运行它,并获得Windows中任务计划程序事件的最大权限:
powershell -executionpolicy bypass -file "C:\PowerShellTelnetRemoteSession.ps1"
" -executionpolicy bypass"是为了绕过未签名脚本的PowerShell安全性。
答案 1 :(得分:0)
如果您使用Cisco UCS,它有一个用于PS管理设备的模块。我不太熟悉它,我知道它存在。
http://blogs.cisco.com/tag/powershell/
这家伙也谈到其他一些选择..
http://www.orcsweb.com/blog/james/fun-with-powershell-plink-cisco-ios-and-powershell/