返回列表 发帖

[转载代码] PowerShell中使用telnet连接交换机

$remoteHost = "hostname"
$port = 23
$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
function telnet($command){
    #$command = read-host
    $writer.WriteLine($command)
    $writer.Flush()
    start-sleep -m 500
    while($stream.DataAvailable){
        $read = $stream.Read($buffer, 0, 1024)
        write-host -n ($encoding.GetString($buffer, 0, $read))
    }
}
#hostname:要连接的交换机ip输入telnet “你要输入的指令如用户名和密码” #该功能主要的意义是可以通过轮循来同时配置多台设备.COPY
http://hi.baidu.com/shrekzz/item/81cff5a74bf9192e8819d3fe

返回列表