# encoding: ascii
# api: powershell
# title: scriptable telnet client
# description: I wrote this a while back to be able to automate stuff on a hp san and ilo. It’s pretty much mritten ad-hoc so please feel free to improve on it. Anyway, might be useful for someone out there. The function send-command logs on to a telnet server and executes the piped in commands.
# version: 0.1
# type: function
# author: David Sjstrand
# license: CC0
# function: read-stream
# x-poshcode-id: 2683
# x-archived: 2017-05-22T05:09:06
# x-published: 2011-05-18T03:49:00
#
# Example:
# Get-content commands.txt | send-command -hostname telnetserver.somewhere.com -user someuser -password somepassword
#
function read-stream ([Parameter(Posistion=0,Mandatory=$true)][validatenotnull()]
		[System.Net.Sockets.NetworkStream]$stream,
		[String]$expect = "")
{
	$buffer = new-object system.byte[] 1024
	$enc = new-object system.text.asciiEncoding

	## Read all the data available from the stream, writing it to the 
	## screen when done.

	## Allow data to buffer
	start-sleep -m 100
	$output = ""
	while($stream.DataAvailable -or $output -notmatch $expect)
	{   
		$read = $stream.Read($buffer, 0, 1024)    
		$output = "$output$($enc.GetString($buffer, 0, $read))"
		## Allow data to buffer for a bit 
		start-sleep -m 100
	}
	$output.split("`n")
}

function send-command ([parameter(position=0,Mandatory=$true)][validatenotnull()]
		[String]$hostname,
	[parameter(position=1,Mandatory=$true)][validatenotnull()]
		[String]$User,
	[parameter(position=2,Mandatory=$true)][validatenotnull()]
		[String]$Password, 
	[parameter(position=3,Mandatory=$true,valuefrompipeline=$true)][validatenotnull()]
		[String]$InputObject,
		[string]$Expect = "")
{
	begin
	{
		
		$sock = new-object system.net.sockets.tcpclient($hostname,23)
		$str = $sock.GetStream()
		$wrt = new-object system.io.streamwriter($str)
		
		read-stream($str)
		$wrt.writeline($user)
		$wrt.flush()
		read-stream($str)
		$wrt.writeline($password)
		$wrt.flush()
		read-stream($str, $expect)
	}
	process
	{
		$wrt.writeline($InputObject)
		$wrt.flush()
		read-stream($str, $expect)
	}
	end
	{
		$wrt.writeline("exit")
		$wrt.flush()
		read-stream($str)

		## Close the streams 
		$wrt.Close()
		$str.Close()
		$sock.close()
	}
}
