Archived Forum Post

Index of archived forum posts

Question:

SSH and tail -f

May 29 '17 at 16:57

Hi, I'm trying to use the ssh component on a VB 6.0 project and I want to run a "tail -f /var/log/messages" on te remote server and retrive the log lines as they are written. How can I accomplish that? because I read that all the methods wait for a certain criteria (message, channel closed, etc) to retrieve the messages from the channel.

Regards, Max


Answer

It's actually very easy. :)

A remote shell session is a logical channel within the connection to the SSH server. (Many shell sessions can be running simultaneously, each with its own logical channel.)

You can refer to this example for how to start a shell session: https://www.example-code.com/vb6/ssh_quick_shell_session.asp

Once the shell session is started, anything your program sends on the logical channel (indicated by the channel number) becomes stdin for the session. In other words, your VB6 program is like a person typing at the shell command prompt. Likewise, the shell session's stdout (and stderr) is what is sent back on the logical channel.

So.. your program would send the "tail -f /var/log/messages" command, making sure to also send the newline. This is like someone typed that command and pressed return. The command starts running, and the output will get sent back on the logical channel.

Your VB6 program can then start a timer that fires periodically (perhaps 4 times per second is plenty), that calls ssh.ChannelPoll to see if anything is available on the channel (i.e. if the "tail -f" sent anything to stdout/stderr). If so, then call ssh.ChannelRead to get whatever is available. Then call ssh.GetReceivedText to access the data that was received by the call to ChannelRead. (And of course, append the text to your display.)