Read() public method

Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
The sum of and is larger than the buffer length. is null. or is negative. An I/O error occurs. The stream does not support reading. Methods were called after the stream was closed.
public Read ( byte buffer, int offset, int count ) : int
buffer byte An array of bytes. When this method returns, the buffer contains the specified byte array with the values between and ( + - 1) replaced by the bytes read from the current source.
offset int The zero-based byte offset in at which to begin storing the data read from the current stream.
count int The maximum number of bytes to be read from the current stream.
return int
Example #1
2
        /// <summary>
        /// Dump the input until we see a particular string in the returning text.
        /// </summary>
        /// <param name="s"></param>
        /// <param name="refreshTimeout">If we see something back from the host, reset the timeout counter</param>
        /// <param name="p"></param>
        /// <param name="failNow">Function that returns true if we should throw out right away</param>
        private void DumpTillFind(ShellStream s, string matchText, 
            Action<string> ongo = null, 
            bool dontdumplineofmatch = true, 
            int secondsTimeout = 60*60, 
            bool refreshTimeout = false,
            Func<bool> failNow = null
            )
        {
            var lb = new LineBuffer(ongo);
            if (dontdumplineofmatch)
            {
                lb.Suppress(matchText);
            }

            var timeout = DateTime.Now + TimeSpan.FromSeconds(secondsTimeout);
            bool gotmatch = false;
            while (timeout > DateTime.Now)
            {
                s.Expect(TimeSpan.FromMilliseconds(100), new ExpectAction(matchText, l => { lb.Add(l); gotmatch = true; }));
                gotmatch = gotmatch || lb.Match(matchText);
                if (gotmatch)
                    break;

                var data = s.Read();
                if (data != null && data.Length > 0 && refreshTimeout)
                {
                    timeout = DateTime.Now + TimeSpan.FromSeconds(secondsTimeout);
                }

                lb.Add(data);

                if (failNow != null && failNow())
                {
                    throw new SSHCommandInterruptedException("Calling routine requested termination of command");
                }
            }
            if (!gotmatch)
            {
                throw new TimeoutException(string.Format("Waiting for '{0}' back from host and it was not seen inside of {1} seconds.", matchText, secondsTimeout));
            }
            lb.DumpRest();
        }
Example #2
0
        public void ExeSSH(Object s)
        {
            using (var conninfo = new PasswordConnectionInfo(ip, log, pass))
            {
                //  try

                Renci.SshNet.SshClient client = new Renci.SshNet.SshClient(conninfo);

                //  conninfo.Timeout = TimeSpan.FromSeconds(50);
                try
                {
                    client.Connect();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    Renci.SshNet.ShellStream stream = client.CreateShellStream("ssh", 180, 324, 1800, 3600, 8000);
                    foreach (string command in list)
                    {
                        stream.Write(command + "\n");
                        System.Threading.Thread.Sleep(5000);
                        string temp_string = stream.Read();
                        //    File.WriteAllText(path, temp_string, Encoding.UTF8);
                        File.WriteAllText("C:/" + ip + ".txt", temp_string, Encoding.UTF8);
                        System.Threading.Thread.Sleep(5000);
                    }
                    client.Disconnect();
                }
                catch (Exception ex)
                {
                }
            }
        }
Example #3
0
 public void ExSSH(string ip, string log, string pass, List <String> list)
 {
     //     foreach (string ipadd in ip)
     //     {
     using (var conninfo = new PasswordConnectionInfo(ip, log, pass))
     {
         Renci.SshNet.SshClient client = new Renci.SshNet.SshClient(conninfo);
         client.Connect();
         Renci.SshNet.ShellStream stream = client.CreateShellStream("ssh", 180, 324, 1800, 3600, 8000);
         foreach (string command in list)
         {
             // var command = "show config modified";
             stream.Write(command + "\n");
             System.Threading.Thread.Sleep(10000);
             string temp_string = stream.Read();
             //  Console.Write(temp_string);
             File.WriteAllText("C:/" + ip + " thread n- " + System.Threading.Thread.CurrentThread.ManagedThreadId + ".txt", temp_string, Encoding.UTF8);
             //     System.Threading.Thread.Sleep(5000);
         }
         client.Disconnect();
         //   }
     }
 }