Quantcast
Channel: sshnet Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1729

New Post: Tailing a file as its being written

$
0
0
So here's how I did it, it works-ish, but sometimes randomly disconnects or stops tailing. Let me know if you have any questions.
PasswordConnectionInfo connectionInfo = new PasswordConnectionInfo(LogData.IpAddress, LogData.Username, LogData.Password);

using (var ssh = new SshClient(connectionInfo))
{
    ssh.Connect();
    string reply = String.Empty;

    using (var shellStream = ssh.CreateShellStream("dumb", 0, 0, 0, 0, BUFFERSIZE)) // BUFFERSIZE Set to 1024 for me
    {
        // Wait for prompt
        reply = shellStream.Expect(new Regex(LogData.ServerName.ToLower() + @":.*>"), new TimeSpan(0, 0, 10));
        shellStream.WriteLine(command);  // command is the tail command, ie "tail -f filename"
    
        // Keep reading until a cancel request is given
        while (!WorkerThread.CancellationPending)
        {
            string result = shellStream.ReadLine(new TimeSpan(0, 0, 10));
    
            if (!String.IsNullOrEmpty(result) && !result.Contains(LogData.ServerName.ToLower()))
                UpdateTailActiveTextBox(result);
        }
    }
}

Viewing all articles
Browse latest Browse all 1729

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>