LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Unable to ssh in this shell (https://www.linuxquestions.org/questions/programming-9/unable-to-ssh-in-this-shell-397138/)

hari_sasidharan 12-28-2005 06:07 AM

Unable to ssh in this shell
 
This is the code for a shell
But iam unable to ssh

Tried googling but did not find any useful tips

Code:

void
set_keypress (int fd)
{

  struct termios new_settings;
  if (tcgetattr (fd, &new_settings) < 0)
    {
      perror ("error getting the attributes for terminal ");
      exit (-1);
    }
  new_settings.c_lflag &= ~ICANON;
  new_settings.c_cc[VTIME] = 0;
  new_settings.c_cc[VMIN] = 1;
  if (tcsetattr (fd, TCSANOW, &new_settings) < 0)
    {
      perror ("error setting the attributes for terminal  ");
      exit (-1);
    }
  return;
}

int
open_pty_pair (int *amaster, int *aslave)
{
  int master, slave;
  char *name;
  master = getpt ();
  if (master < 0)
    {
      perror ("The master pty couldn't be opend");
      return -1;
    }
  if (grantpt (master) < 0 || unlockpt (master) < 0)
    {
      perror ("Grant pt failed ");
      close (master);
      return -1;
    }
  name = ptsname (master);
  if (name == NULL)
    {
      perror ("Error getting the master name");
      close (master);
      return -1;
    }
  slave = open (name, O_RDWR);
  if (slave == -1)
    {
      perror ("Error opening the slave");
      close (master);
      return -1;
    }
  if (isastream (slave))
    {

      if (ioctl (slave, I_PUSH, "ptem") < 0
          || ioctl (slave, I_PUSH, "ldterm") < 0)
        {
          perror ("Streams file : ioctl failed");
          close (master);
          close (slave);
          return -1;
        }
    }
  *amaster = master;
  *aslave = slave;
  return 1;
}

int
main ()
{
  int *amaster = malloc (4), *aslave = malloc (4);
  if (open_pty_pair (amaster, aslave) < 0)
    {
      printf ("Allocation of pseudo terminal failed");
      exit (0);
    }
  int pid = fork ();
  if (!pid)
    {
      close (*amaster);
      dup2 (*aslave, 0);
      dup2 (*aslave, 1);
      dup2 (*aslave, 2);
      execl ("/bin/bash", "/bin/bash", (char *) 0);
    }
  else
    {
      close (*aslave);
      set_keypress (*amaster);
      struct termios new_settings;
      if (tcgetattr (0, &new_settings) < 0)
        {
          perror ("Error getting the attributes for terminal 0");
          exit (0);
        }
      new_settings.c_lflag &= (~ECHO);
      if (tcsetattr (0, TCSANOW, &new_settings) < 0)
        {
          perror ("Error unsetting echo for terminal 0");
          exit (0);
        }

      int flags;
      if ((flags = fcntl (*amaster, F_GETFL, 0)) < 0)
        {
          perror ("fcntl:getfl failed\n");
          exit (-1);
        }
      flags |= O_NONBLOCK;
      if (fcntl (*amaster, F_SETFL, flags) < 0)
        {
          perror ("Fcntl:setfl failed\n");
          exit (-1);
        }

      int ppid = fork ();
      if (!ppid)
        {
          int ch;
          set_keypress (0);
          while (1)
            {
              ch = fgetc (stdin);
              if (ch > 0)
                {
                  if (write (*amaster, &ch, 1) < 0)
                    {
                      perror ("error writing to the master ");
                    }
                }
            }
        }
      else
        {
          int len;
          char buffer;
          dup2 (*amaster, 0);
          while (1)
            {
              if ((buffer = fgetc (stdin)) != -1)
                {
                  fprintf (stdout, "%c", buffer);
                }
            }
        }
    }
  waitpid (pid, NULL, 0);
  return 0;
}


scuzzman 12-28-2005 06:51 AM

1) Code is best posted in [_code]...[_/code] blocks so it's actually readable.
2) What errors are you getting?
3) What language is that?

hari_sasidharan 12-29-2005 02:53 AM

This is the c code for a shell
After compiling and executing the code i get a shell..... i am able to do most of the things including telnetting to a system
but when i ssh i am not probed for passwd


All times are GMT -5. The time now is 01:12 AM.