;
; Copyright by Alexander Koch, also EDV Beratung info@also-edv-beratung.de 064423373580
;

#include <Constants.au3>
;#include <myinclude\iif.au3>
;#include <timer.au3>
Opt("MustDeclareVars", 1)
Global $plink_exe = @ScriptDir & "\3thPartytools\" & "plink.exe"
Global $pscp_exe = @ScriptDir & "\3thPartytools\" & "pscp.exe"
Global $PuttyConection
Global $Expand

If Not FileExists($plink_exe) Then
	ConsoleWrite('@@ Error Putty fehlt ' & $plink_exe & @CRLF & '>Error code: ' & @error & @CRLF)
	Exit 1
EndIf
If Not FileExists($pscp_exe) Then
	ConsoleWrite('@@ Error Putty fehlt ' & $plink_exe & @CRLF & '>Error code: ' & @error & @CRLF)
	Exit 1
EndIf

Func PuttyConnect($Protocol, $Host, $Port = "", $User = "", $Pass = "", $RFile = "", $LFile = "")
	If $Protocol = "" Then Return SetError(1, 1)
	If $Host = "" Then Return SetError(1, 2)
	If $User = "" Then Return SetError(1, 4)
	If $Pass = "" Then Return SetError(1, 5)
	Local $Wait = False
	Local $PuttyConectionString, $PID
	Switch $Protocol
		Case "ssh"
			$Expand = " "
			$PuttyConectionString = $plink_exe & " -batch -ssh "
			$PuttyConectionString &= iif($Pass = "", "", " -pw " & $Pass & " ")
			$PuttyConectionString &= iif($Port = "", "-P 22 ", "-P " & $Port & " ")
			$PuttyConectionString &= iif($User = "", $Host, $User & "@" & $Host)
			$PuttyConection &= iif($RFile = "", "", $RFile)
		Case "ssh_commandfile"
			$Expand = " "
			$PuttyConectionString = $plink_exe & " -batch -ssh "
			$PuttyConectionString &= iif($Pass = "", "", " -pw " & $Pass & " ")
			$PuttyConectionString &= iif($Port = "", "-P 22 ", "-P " & $Port & " ")
			$PuttyConectionString &= iif($User = "", $Host, $User & "@" & $Host)
			$PuttyConectionString &= iif($RFile = "", "", " -m " & $RFile)
		Case "ssh_nobatch"
			$Expand = " "
			$PuttyConectionString = $plink_exe & " -ssh "
			$PuttyConectionString &= iif($Pass = "", "", " -pw " & $Pass & " ")
			$PuttyConectionString &= iif($Port = "", "-P 22 ", "-P " & $Port & " ")
			$PuttyConectionString &= iif($User = "", $Host, $User & "@" & $Host)
		Case "scp_get"
			If $RFile = "" Then Return SetError(1, 6)
			If $LFile = "" Then Return SetError(1, 7)
			$PuttyConectionString = $pscp_exe & " -batch "
			$PuttyConectionString &= iif($Pass = "", "", " -pw " & $Pass & " ")
			$PuttyConectionString &= iif($Port = "", "-P 22 ", "-P " & $Port & " ")
			$PuttyConectionString &= iif($User = "", $Host & ":" & $RFile, $User & "@" & $Host & ":" & $RFile & " ")
			$PuttyConectionString &= iif($LFile = "", $RFile, $LFile)
			$Wait = True
		Case "scp_put"
			If $RFile = "" Then Return SetError(1, 6)
			If $LFile = "" Then Return SetError(1, 7)
			$PuttyConectionString = $pscp_exe & " -batch "
			$PuttyConectionString &= iif($Pass = "", "", " -pw " & $Pass & " ")
			$PuttyConectionString &= iif($Port = "", "-P 22 ", "-P " & $Port & " ")
			$PuttyConectionString &= iif($LFile = "", $RFile & " ", $LFile & " ")
			$PuttyConectionString &= iif($User = "", $Host & ":" & $RFile, $User & "@" & $Host & ":" & $RFile & " ")
			$Wait = True
		Case "telnet"
			$Expand = ""
			$PuttyConectionString = $plink_exe & " -batch -telnet "
			$PuttyConectionString &= iif($Port = "", "-P 23 ", "-P " & $Port & " ") & $Host
		Case "rlogin"
			$Expand = ""
			$PuttyConectionString = $plink_exe & " -batch -rlogin "
			$PuttyConectionString &= iif($Port = "", "-P 512 ", "-P " & $Port & " ") & $Host
		Case Else
			Return SetError(3)
	EndSwitch
	$PID = Run($PuttyConectionString, @ScriptDir, @SW_HIDE, $STDIN_CHILD + $STDERR_CHILD + $STDOUT_CHILD)
	If @error Then
		Return SetError(2, 0,0)
	Else
		If $Wait Then
			Return PuttyWaitForAnswer($PID)
		Else
			Return $PID
		EndIf
	EndIf
	return 0
EndFunc   ;==>PuttyConnect

Func PuttyConunicate($MSG, $PID = $PuttyConection)
	if ProcessExists($PID) Then
		StdinWrite($PID, $MSG & $Expand & @LF)
		if @error Then
			Return -1
		Else
			Return PuttyWaitForAnswer($PID)
		EndIf
	Else
		Return -1
	EndIf
EndFunc   ;==>PuttyConunicate

Func PuttyDisconnect($PID = $PuttyConection)
	ProcessClose($PID)
EndFunc   ;==>PuttyDisconnect

Func PuttyUpdate($PID = $PuttyConection)
	;If StdoutRead ( $PuttyConection) = 0 Then Return
	Local $recv = ""
	Sleep(50)
	$recv = StdoutRead($PID) & StderrRead($PID)
	If Not @error Then
		If $recv <> "" Then
			$recv = StringReplace($recv, @LF, @CRLF)
			$recv = StringReplace($recv, @CR & @CRLF, @CRLF)
		EndIf
		Return $recv
	Else
		Return seterror(1,0,$recv)
	EndIf
EndFunc   ;==>PuttyUpdate

Func PuttyWaitForAnswer($PID = $PuttyConection, $TimeOut = 2000)
	Local $RC = ""
	Local $StartTime = TimerInit()
	do
		$RC = PuttyUpdate($PID)
		If Not ProcessExists($PID) Then ExitLoop
		If TimerDiff($StartTime) > $TimeOut Then ExitLoop
	Until not $RC = ""
	Return $RC
EndFunc   ;==>PuttyWaitForAnswer

Func PuttyConnectGetPubKey($Protocol, $Host, $Port = "", $User = "", $Pass = "", $RFile = "", $LFile = "")
	Local $PID, $RC[3]
	$RC[1] = PuttyConnect($Protocol, $Host, $Port, $User, $Pass, $RFile, $LFile)
	$RC[2] = PuttyWaitForAnswer($RC[0])
	If StringInStr($RC[2], "The server's host key is not cached in the registry") Then
		$RC[1] = PuttyConnect("ssh_nobatch", $Host, $Port, $User, $Pass, "exit")
		PuttyConunicate("y",$RC[1])
		$RC[0] = 1
	ElseIf StringInStr($RC[2],"WARNING - POTENTIAL SECURITY BREACH!") Then
		$RC[0] = 0
	EndIf
	$RC[0] = 1
	Return $RC
EndFunc

Func iif($Vergleich, $Return1, $Return2 = "")
	If $Vergleich Then
		Return $Return1
	Else
		Return $Return2
	EndIf
EndFunc   ;==>iif



