Board logo

标题: [转载代码] PowerShell中使用telnet连接交换机 [打印本页]

作者: DAIC    时间: 2013-9-16 21:56     标题: PowerShell中使用telnet连接交换机

  1. $remoteHost = "hostname"
  2. $port = 23
  3. $socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port)
  4. if($socket -eq $null) { return; }
  5. $stream = $socket.GetStream()
  6. $writer = new-object System.IO.StreamWriter($stream)
  7. $buffer = new-object System.Byte[] 1024
  8. $encoding = new-object System.Text.AsciiEncoding
  9. function telnet($command){
  10.     #$command = read-host
  11.     $writer.WriteLine($command)
  12.     $writer.Flush()
  13.     start-sleep -m 500
  14.     while($stream.DataAvailable){
  15.         $read = $stream.Read($buffer, 0, 1024)
  16.         write-host -n ($encoding.GetString($buffer, 0, $read))
  17.     }
  18. }
  19. #hostname:要连接的交换机ip输入telnet “你要输入的指令如用户名和密码” #该功能主要的意义是可以通过轮循来同时配置多台设备.
复制代码
http://hi.baidu.com/shrekzz/item/81cff5a74bf9192e8819d3fe




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2