Skip to content

Instantly share code, notes, and snippets.

@hashtafak
Created March 3, 2020 08:03
Show Gist options
  • Save hashtafak/310590dc778d84d1e968bc0bf0429342 to your computer and use it in GitHub Desktop.
Save hashtafak/310590dc778d84d1e968bc0bf0429342 to your computer and use it in GitHub Desktop.
#include-once
Global $Bitvise_Path = "C:\Program Files\Bitvise SSH Client"
Global Const $Bitvise_BvSsh = "BvSsh.exe"
Global Const $Bitvise_BvSshCtrl = "BvSshCtrl.exe"
Global Const $Bitvise_BvSsh_Host = "127.0.0.1"
#cs
+> Bivise Function
_Bitvise_Run($option)
_Bitvise_Add_Option(ByRef $option_string,$paramter,$value)
_Bitvise_Set_Option(ByRef $option_string,$paramter,$value)
_Bitvise_Set_Option(ByRef $option_string,$paramter,$value)
_Bitvise_Set_Port_Forward($port)
_Bitvise_Checking_Connection($port_forward)
_Bitvise_Wait_For_Connection($port_forward,$time_out=10000,$delay=100)
+> Bitvise Control Function
_Bitvise_Ctrl_Login($pid)
_Bitvise_Ctrl_Logout($pid)
_Bitvise_Ctrl_Exit($pid)
#ce
; #FUNCTION# ====================================================================================================================
; Name ..........: _Bitvise_Run
; Description ...: _Bitvise_Run($option). Run Bitvise SSH Client Cmdline option
; Syntax ........: _Bitvise_Run($option)
; Parameters ....: $option - a String contain cmdline option
; Return values .: Pid of the Process.
; Author ........: nlhung
; Modified ......: 06/08/2014 ( dd/mm/yyyy )
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Bitvise_Run($option)
Return Run('"'&$Bitvise_Path&"\"&$Bitvise_BvSsh&'" '&$option, "", @SW_HIDE)
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _Bitvise_Get_Default_Option
; Description ...: _Bitvise_Get_Default_Option($host, $port, $username, $password[, $login = True[, $exit = True]]). Get a String contain cmdline option default.
; Syntax ........: _Bitvise_Get_Default_Option($host, $port, $username, $password[, $login = True[, $exit = True]])
; Parameters ....: $host - ssh hostname.
; $port - ssh port.
; $username - ssh username.
; $password - ssh password.
; $login - [optional] Auto login. Default is True.
; $exit - [optional] Auto exit. Default is True.
; Return values .: a string contain cmdline option.
; Author ........: nlhung
; Modified ......: 06/08/2014 ( dd/mm/yyyy )
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Bitvise_Get_Default_Option($host,$port,$username,$password,$login=True,$exit=True)
Local $option_string = ""
$option_string = "-host="&$host&" -port="&$port&" -username="&$username&" -password="&$password
If $login Then
$option_string &= " -loginOnStartup"
EndIf
If $exit Then
$option_string &= " -exitOnLogout"
EndIf
Return $option_string
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _Bitvise_Add_Option
; Description ...: _Bitvise_Add_Option(Byref $option_string, $paramter, $value). Add an option to already option.
; Syntax ........: _Bitvise_Add_Option(Byref $option_string, $paramter, $value)
; Parameters ....: $option_string - [in/out] a varible option string.
; $paramter - paramter name.
; $value - value.
; Return values .: None
; Author ........: nlhung
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Bitvise_Add_Option(ByRef $option_string,$paramter,$value)
$option_string &= " -"&$paramter&"="&$value
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _Bitvise_Set_Option
; Description ...: _Bitvise_Set_Option(Byref $option_string, $paramter, $value). Set change an option
; Syntax ........: _Bitvise_Set_Option(Byref $option_string, $paramter, $value)
; Parameters ....: $option_string - option string.
; $paramter - paramter name.
; $value - value.
; Return values .: None
; Author ........: nlhung
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Bitvise_Set_Option(ByRef $option_string,$paramter,$value)
Local $s_pattern_escape = "(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\)"
$paramter = StringRegExpReplace($paramter, $s_pattern_escape, "\\$1")
$option_string = StringRegExpReplace($option_string,"(-"&$paramter&"=(?:.*?)(?:\s|$))","-"&$paramter&"="&$value&" ")
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _Bitvise_Set_Port_Forward
; Description ...: _Bitvise_Set_Port_Forward($port). Set Port Forward after logged in.
; Syntax ........: _Bitvise_Set_Port_Forward($port)
; Parameters ....: $port - port number.
; Return values .: 1 or 0.
; Author ........: nlhung
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Bitvise_Set_Port_Forward($port)
Local $x = RegRead("HKEY_CURRENT_USER\Software\EbeStub\BvSshClient","DefaultProfile")
Local $xy = StringRegExpReplace($x,'(7F00000100000004(?:.*?)000000000000000000007F)','7F00000100000004'&StringTrimLeft(StringToBinary($port),2)&'000000000000000000007F',1)
Return RegWrite("HKEY_CURRENT_USER\Software\EbeStub\BvSshClient","DefaultProfile","REG_BINARY",$xy)
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _Bitvise_Checking_Connection
; Description ...: _Bitvise_Checking_Connection($port_forward). Check port open or not.
; Syntax ........: _Bitvise_Checking_Connection($port_forward)
; Parameters ....: $port_forward - port number.
; Return values .: 1 or 0
; Author ........: nlhung
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Bitvise_Checking_Connection($port_forward)
TCPStartup()
Local $connection, $hWnd
$connection = TCPConnect($Bitvise_BvSsh_Host,$port_forward)
If $connection <> -1 Then
TCPCloseSocket($connection)
TCPShutdown()
Return 1
Else
If WinExists("Host Key Verification") Then
$hWnd = WinGetHandle("Host Key Verification")
ControlClick($hWnd, "Accept and &Save", "[CLASS:Button]", "left", 1)
EndIf
TCPShutdown()
Return 0
EndIf
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _Bitvise_Wait_For_Connection
; Description ...: _Bitvise_Wait_For_Connection($port_forward[, $time_out = 10000[, $delay = 100]]). Wait for SSH logged in.
; Syntax ........: _Bitvise_Wait_For_Connection($port_forward[, $time_out = 10000[, $delay = 100]])
; Parameters ....: $port_forward - port number forward after logged in.
; $time_out - [optional] time out. Default is 10000.
; $delay - [optional] time delay. Default is 100.
; Return values .: 0 or time to logged in (> 0)
; Author ........: nlhung
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Bitvise_Wait_For_Connection($port_forward,$time_out=10000,$delay=100)
Local $timer_int = TimerInit()
While TimerDiff($timer_int) < $time_out
If _Bitvise_Checking_Connection($port_forward) Then Return TimerDiff($timer_int)
If Not ProcessExists("BvSsh.exe") Then Return 0
Sleep($delay)
WEnd
Return 0
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _Bitvise_Ctrl_Login
; Description ...: _Bitvise_Ctrl_Login($pid). Control Bitvise to Login
; Syntax ........: _Bitvise_Ctrl_Login($pid)
; Parameters ....: $pid - pid of process access to.
; Return values .: None
; Author ........: nlhung
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Bitvise_Ctrl_Login($pid)
ProcessWait($pid)
Return Run('"'&$Bitvise_Path&"\"&$Bitvise_BvSshCtrl&'" '&$pid&' Login', "", @SW_HIDE)
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _Bitvise_Ctrl_Logout
; Description ...: _Bitvise_Ctrl_Logout($pid). Control Bitvise to Logout
; Syntax ........: _Bitvise_Ctrl_Logout($pid)
; Parameters ....: $pid - pid of process access to.
; Return values .: None
; Author ........: nlhung
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Bitvise_Ctrl_Logout($pid)
ProcessWait($pid)
Return Run('"'&$Bitvise_Path&"\"&$Bitvise_BvSshCtrl&'" '&$pid&' Logout', "", @SW_HIDE)
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _Bitvise_Ctrl_Exit
; Description ...: _Bitvise_Ctrl_Exit($pid). Control Bitvise to Exit
; Syntax ........: _Bitvise_Ctrl_Exit($pid)
; Parameters ....: $pid - pid of process access to.
; Return values .: None
; Author ........: nlhung
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Bitvise_Ctrl_Exit($pid)
ProcessWait($pid)
Return Run('"'&$Bitvise_Path&"\"&$Bitvise_BvSshCtrl&'" '&$pid&' Exit', "", @SW_HIDE)
EndFunc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment