'  
'   Rebex Sample Code License
' 
'   Copyright 2026, REBEX CR s.r.o.
'   All rights reserved.
'   https://www.rebex.net/
' 
'   Permission to use, copy, modify, and/or distribute this software for any
'   purpose with or without fee is hereby granted.
' 
'   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
'   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
'   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
'   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
'   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
'   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
'   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
'   OTHER DEALINGS IN THE SOFTWARE.
' 

Imports System

Imports Rebex.Net
Imports Rebex.TerminalEmulation

''' <summary>
''' The RExec class executes one command on the remote host.
''' </summary>
Public Class RExec
    ''' <summary>
    ''' The main entry point for the application.
    ''' </summary>
    ''' <param name="args">Program arguments.</param>
    ''' <returns>Exit code.</returns>
    <STAThread()> _
    Public Shared Function Main(ByVal args As String()) As Integer
            ' set the trial license key
            Rebex.Licensing.Key = Rebex.Samples.TrialKey.Key


        ' if bad number of arguments was specified, show help and return
        If args.Length <> 4 Then
            ShowHelp()
            Return 1
        End If

        ' initialize variables from the arguments
        Dim host As String = args(0)
        Dim login As String = args(1)
        Dim password As String = args(2)
        Dim command As String = args(3)

        ' create an Ssh object
        Dim ssh As New Ssh()

        Try
            ' connect to the server
            ssh.Connect(host)

            ' login to the server
            ssh.Login(login, password)

            ' run the command
            Dim response As String = ssh.RunCommand(command)

            ' write the response
            Console.Write(response)
        Catch e As Exception
            Console.WriteLine("Exception occured: {0}", e)
            Return 1
        Finally
            ' disconnect from the server
            If Not ssh Is Nothing Then
                ssh.Disconnect()
            End If
        End Try

        Return 0
    End Function

    ''' <summary>
    ''' Shows syntax and example.
    ''' </summary>
    Private Shared Sub ShowHelp()
        Console.WriteLine("--------------------------------------------------------------------")
        Console.WriteLine("Rebex RExec (Remote Exec) - executes a command on the remote host.")
        Console.WriteLine("--------------------------------------------------------------------")
        Console.WriteLine("Note: If the command requires user interaction, RExec will time out.")
        Console.WriteLine("Syntax: RExec host login password command")
        Console.WriteLine("Example: RExec sample.host.com test test123 ""ls -l""")
    End Sub
End Class
