Author Topic: GetReceivedText returns different outputs at ssh sessions of 2 equipment  (Read 508 times)

tommylee926@gmail.com

  • Newbie
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
I use Chilkat SSH to start a remote shell by calling ssh.SendReqShell and then run a command by calling ssh.ChannelSendString. I call ChannelReadAndPoll to receive the output, but the 2 seesions return different outputs. why?
Am I missing something?
Please see the pic at the url: https://imgur.com/a/Sttg3

please see the code below.
==================================================================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace To_Cisco_device_through_Linux_jump
{
   class Program
   {
      static string JUMPER = "192.168.2.2";
      static int PORT = 22;
      static string username = "tommy";
      static string passwd = "f0";
      
      static void SendCmdToRouter(string router, List<string> cmdlist)
      {
         Chilkat.Ssh ssh_jumper = new Chilkat.Ssh();

//  Any string automatically begins a fully-functional 30-day trial.
         bool success = ssh_jumper.UnlockComponent("30-day trial");
         if (success != true) {
            Console.WriteLine(ssh_jumper.LastErrorText);
            Console.ReadKey();
            return;
         }

//  Connect to an SSH server:
//  JUMPER may be an IP address or JUMPER:
         success = ssh_jumper.Connect(JUMPER, PORT);
         if (success != true) {
            Console.WriteLine(ssh_jumper.LastErrorText);
            Console.ReadKey();
            return;
         }

//  Wait a max of 10 seconds when reading responses..
         ssh_jumper.IdleTimeoutMs = 10000;

//  Authenticate using login/password:
         success = ssh_jumper.AuthenticatePw(username, passwd);
         if (success != true) {
            Console.WriteLine(ssh_jumper.LastErrorText);
            Console.ReadKey();
            return;
         }

         /*INITIATE SECOND SSH CONNECTION*/
         Chilkat.Ssh ssh_router = new Chilkat.Ssh();
         success = ssh_router.ConnectThroughSsh(ssh_jumper, router, 22);
         if (success != true) {
            Console.WriteLine(ssh_router.LastErrorText);
            Console.ReadKey();
            return;
         }
      
         ssh_router.IdleTimeoutMs = 10000;

         success = ssh_router.AuthenticatePw(username, passwd);
         if (success != true) {
            Console.WriteLine(ssh_router.LastErrorText);
            Console.ReadKey();
            return;
         } else{
            Console.WriteLine("router: " + router + " connected!");
            Thread.Sleep(1500);}

//  Open a session channel.  (It is possible to have multiple
//  session channels open simultaneously.)
         int channelNum_router;
         channelNum_router = ssh_router.OpenSessionChannel();
         if (channelNum_router < 0) {
            Console.WriteLine(ssh_router.LastErrorText);
            Console.ReadKey();
            return;
         }

//  Some SSH servers require a pseudo-terminal
//  If so, include the call to SendReqPty.  If not, then
//  comment out the call to SendReqPty.
//  Note: The 2nd argument of SendReqPty is the terminal type,
//  which should be something like "xterm", "vt100", "dumb", etc.
//  A "dumb" terminal is one that cannot process escape sequences.
//  Smart terminals, such as "xterm", "vt100", etc. process
//  escape sequences.  If you select a type of smart terminal,
//  your application will receive these escape sequences
//  included in the command's output.  Use "dumb" if you do not
//  want to receive escape sequences.  (Assuming your SSH
//  server recognizes "dumb" as a standard dumb terminal.)         
//  Use 0 for pixWidth and pixHeight when the dimensions
//  are set in number-of-chars.
         
         success = ssh_router.SendReqPty(channelNum_router, "dumb", 120, 40, 0, 0);
         if (success != true) {
            Console.WriteLine(ssh_router.LastErrorText);
            Console.ReadKey();
            return;
         }

//  Start a shell on the channel:
         success = ssh_router.SendReqShell(channelNum_router);
         if (success != true) {
            Console.WriteLine(ssh_router.LastErrorText);
            Console.ReadKey();
            return;
         }

//  Start mutil commands in the remote shell.   
         foreach (string myStringList in cmdlist) {
            
            //Console.WriteLine(myStringList);
            success = ssh_router.ChannelSendString(channelNum_router, myStringList, "ansi");
            if (success != true) {
               Console.WriteLine(ssh_router.LastErrorText);
               Console.ReadKey();
               return;
            }
            
            //Thread.Sleep(1000);
            
            
            //last cmd
            if (cmdlist.Last() == myStringList) {
               //  Send an EOF.  This tells the server that no more data will
               //  be sent on this channel.  The channel remains open, and
               //  the SSH client may still receive output on this channel.
               success = ssh_router.ChannelSendEof(channelNum_router);
               if (success != true) {
                  Console.WriteLine(ssh_router.LastErrorText);
                  Console.ReadKey();
                  return;
               }
               Console.WriteLine("sendEOF");
            }
            
            //prevent an infinite wait if the matchPattern never arrives.
            /*ssh_router.ReadTimeoutMs = 5000;
            //  Retrieve the output.
//            success = ssh_router.ChannelReceiveUntilMatch(channelNum_router, "hellocat", "ansi", true);
//            if (success != true) {
//               Console.WriteLine(ssh_router.LastErrorText);
//               Console.ReadKey();
//               return;
//            }*/

            int n = ssh_router.ChannelReadAndPoll(channelNum_router, 2000);
            if (n < 0) {
               Console.WriteLine(ssh_router.LastErrorText);
               Console.ReadKey();
               return;
            }
            //last cmd
            if (cmdlist.Last() == myStringList) {
               //  Close the channel:
               success = ssh_router.ChannelSendClose(channelNum_router);
               if (success != true) {
                  Console.WriteLine(ssh_router.LastErrorText);
                  Console.ReadKey();
                  return;
               }
               Console.WriteLine("channelclose");
               
//  Perhaps we did not receive all of the commands output.
//  To make sure,  call ChannelReceiveToClose to accumulate any remaining
//  output until the server's corresponding "channel close" is received.
               success = ssh_router.ChannelReceiveToClose(channelNum_router);
               if (success != true) {
                  Console.WriteLine(ssh_router.LastErrorText);
                  Console.ReadKey();
                  return;
               }
            }
            //pickup the accumulated output of the command:
            string cmdOutput = ssh_router.GetReceivedText(channelNum_router, "ansi");
            if (ssh_router.LastMethodSuccess != true) {
               Console.WriteLine(ssh_router.LastErrorText);
               Console.ReadKey();
               return;
            }
            Console.WriteLine(cmdOutput);
            //Console.WriteLine(cmdOutput.Replace("echo hellocat", "").Replace("hellocat", ""));
            
         
         }



//  Display the remote shell's command output:
         //Console.WriteLine(cmdOutput);
         Console.ReadKey();
//  Disconnect
         ssh_router.Disconnect();
         ssh_jumper.Disconnect();
      }
      
      static List<string>  ReadConfig()
      {
         string line = null;
         //string cmds = null;
         List<string> myStringLists = new List<string>();
         System.IO.StreamReader file = new System.IO.StreamReader(@"3315_sap.txt");
         while ((line = file.ReadLine()) != null) {
            //System.Console.WriteLine(line); 
            if (!line.StartsWith("#") && line.Length > 0) {
               myStringLists.Add(line.TrimEnd('\r', '\n', ' ') + "\r\n");
               
            }
            
         }     
         //cmds.Remove(cmds.Length - 2, 2);
         file.Close(); 
         return myStringLists;
      }
      static void Main(string[] args)
      {
         //  Important: It is helpful to send the contents of the
         //  ssh.LastErrorText property when requesting support.
   
         Console.Write("請輸入router名稱: ");
         string routername = Console.ReadLine();
         if (routername != "")
         {
            List<string> cmdlist = ReadConfig();
            Console.WriteLine(cmdlist.Count.ToString());
            SendCmdToRouter(routername, cmdlist);      
         }

      }

   }
}