login about faq

I am trying to run a backup script on my server. I want to follow the output from the command.

I have tried ChannelReceiveUntilMatchAsync and a task but the task always finishes a few seconds after it starts and that ends the backup script.

How can I execute the shell script and follow the output using?

asked Mar 28 at 16:01

kevindcs's gravatar image

kevindcs
11556


So after much more trial and error I finally found something that works. I Wanted to put it here for all the others out there that asked this question

Here it is:

    Dim ssh As New Chilkat.Ssh()

    '  Any string automatically begins a fully-functional 30-day trial.
    Dim success As Boolean = ssh.UnlockComponent("Anything for 30-day trial")
    If (success <> True) Then
        Console.WriteLine(ssh.LastErrorText)
        Exit Sub
    End If

    '  Connect to an SSH server:
    Dim hostname As String
    Dim port As Integer

    Dim cmdOutput As String

    '  Hostname may be an IP address or hostname:
    hostname = "*** your server ip ***"
    port = 22

    success = ssh.Connect(hostname, port)
    If (success <> True) Then
        Invoke(New DelegateUpdateCommandProgress(AddressOf UpdateCommandProgress), ssh.LastErrorText)
        Console.WriteLine(ssh.LastErrorText)
        Exit Sub
    End If

    '  Wait a max of 5 seconds when reading responses..
    ssh.EnableEvents = True

    ssh.IdleTimeoutMs = 2000
    '  Authenticate using login/password:
    success = ssh.AuthenticatePw("username", "your password")
    If (success <> True) Then
        Invoke(New DelegateUpdateCommandProgress(AddressOf UpdateCommandProgress), ssh.LastErrorText)
        Console.WriteLine(ssh.LastErrorText)
        Exit Sub
    End If

    '  Open a session channel.  (It is possible to have multiple
    '  session channels open simultaneously.)
    Dim channelNum As Integer
    channelNum = ssh.OpenSessionChannel()
    If (channelNum < 0) Then
        Console.WriteLine(ssh.LastErrorText)
        Exit Sub
    End If

    Dim termType As String = "dumb"
    Dim widthInChars As Integer = 180
    Dim heightInChars As Integer = 40
    '  Use 0 for pixWidth and pixHeight when the dimensions
    '  are set in number-of-chars.
    Dim pixWidth As Integer = 0
    Dim pixHeight As Integer = 0
    success = ssh.SendReqPty(channelNum, termType, widthInChars, heightInChars, pixWidth, pixHeight)
    If (success <> True) Then
        Invoke(New DelegateUpdateCommandProgress(AddressOf UpdateCommandProgress), ssh.LastErrorText)
        Console.WriteLine(ssh.LastErrorText)
        Exit Sub
    End If

    '  Start a shell on the channel:
    success = ssh.SendReqShell(channelNum)
    If (success <> True) Then
        Console.WriteLine(ssh.LastErrorText)
        Exit Sub
    End If
    Console.WriteLine("Shell Requested")

    '  Get a directory listing:
    Dim cmd As String = "<full path to your command>" + vbLf

    success = ssh.ChannelSendString(channelNum, cmd, "ansi")
    If (success <> True) Then
        Console.WriteLine("ssh.ChannelSendString" + ssh.LastErrorText)
        Exit Sub
    End If

    ssh.IdleTimeoutMs = 2000

    Dim n = ssh.ChannelReadAndPoll(channelNum, 250)

    If (success <> True) Then
        Console.WriteLine("task.run" + ssh.LastErrorText)
        Exit Sub
    End If
    Console.WriteLine("task started")
    While n > 0
        ssh.ChannelRead(channelNum)
        cmdOutput = ssh.GetReceivedText(channelNum, "ansi")

        If cmdOutput.Contains("JOBDONE") Then
            Console.WriteLine("Task Finished")
            Exit While
        End If

        Console.WriteLine(cmdOutput)
        Invoke(New DelegateUpdateCommandProgress(AddressOf UpdateCommandProgress), cmdOutput)

    End While

    ssh.ChannelSendClose(channelNum)

    ssh.Disconnect()

I am running this in a background thread. I created a Delegate that updates the text box.

At the end of my script I echo out JOBDONE and look for this in the command output on each poll. I tried using ChannelReceiveUntilMatch but it would never work right.

This isn't complete and could use more error checking etc but the basics are there.

link

answered Apr 01 at 16:27

kevindcs's gravatar image

kevindcs
11556

This is a good article/response to read to get a better understanding of SSH automation: http://www.chilkatforum.com/questions/1272/understanding-ssh-automation

In a nutshell, your application is replacing a human at a terminal. I think conceptually understanding the application's role as SSH client should help in understanding what needs to be done in any given case.

link

answered Mar 28 at 19:11

chilkat's gravatar image

chilkat ♦♦
11.8k316358420

Thank you for the response. I did see this article and I did read it.

Unfortunately it doesn't help me solve my problem.

I have tried several of the functions listed there. For example, this one:

ChannelReceiveToClose ( Async version ) -- Receives channel data until the channel is closed by the server.

How do I know when the channel is closed? If it did close, what is causing it to close?

I know that when I run the program, the backup process does run. Even after I exit the application it still runs and finishes.

I have used a chilkat task to do a loop to read the data but after a few seconds, task.finished is set to True and the loop exits.

Same thing with:

ChannelReceiveUntilMatch ( Async version ) -- Receives channel data until a match string appears.

I put an "echo JOBDONE" at the end of my script and told it to wait until it finds that. But after a few seconds, again the task.finished becomes true and the loop exits.

I searched the forums and there are several people, at least 4 that i found, that have asked this same question.

Maybe you could add it to your examples?

(Mar 29 at 08:46) kevindcs

Please review my follow up.

(Mar 29 at 08:46) kevindcs
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×89
×22
×9
×3

Asked: Mar 28 at 16:01

Seen: 486 times

Last updated: Apr 01 at 16:27

powered by OSQA