Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: Execute a command on a different tty/pts  (Read 5297 times)

0 Members and 1 Guest are viewing this topic.

Rajiv

  • Guest
Execute a command on a different tty/pts
« on: March 11, 2015, 12:08:16 pm »
A simple example to illustrate the use of this is as follows.

You are logged into your home computer remotely via ssh and want to open an application on the home computer. Lets imagine, you want to open browser on your home system. When you are loggeng into a system via ssh, you will be using a sudo terminal. And if you try to open an application, it tries to open it in your psudo terminal, which will fail eventually ( if you are not logged into the system with the ssh -X option ).

But what we are trying to do it to open the application ( in this case, a browser ) on the home system from a remote system. What you will have to do is, you will have to execute the command to open the application on the psudo terminal that is open on the home computer.

You can simply redirect the output to a different psudo terminal by doing the following

command > /dev/pts/3

But this will only exucute the command in the pts that you are logged in and will only redirect the output to the output buffer of a different pts.

Following is program that will help you execute commands on a different psudo terminal.
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <string.h>
#include <unistd.h>

void print_help(char *prog_name) {
        printf("Usage: %s [-n] DEVNAME COMMAND\n", prog_name);
        printf("Usage: '-n' is an optional argument if you want to push a new line at the end of the text\n");
        printf("Usage: Will require 'sudo' to run if the executable is not setuid root\n");
        exit(1);
}

int main (int argc, char *argv[]) {
    char *cmd, *nl = "\n";
    int i, fd;
    int devno, commandno, newline;
    int mem_len;
    devno = 1; commandno = 2; newline = 0;
    if (argc < 3) {
        print_help(argv[0]);
    }
    if (argc > 3 && argv[1][0] == '-' && argv[1][1] == 'n') {
        devno = 2; commandno = 3; newline=1;
    } else if (argc > 3 && argv[1][0] == '-' && argv[1][1] != 'n') {
        printf("Invalid Option\n");
        print_help(argv[0]);
    }
    fd = open(argv[devno],O_RDWR);
    if(fd == -1) {
        perror("open DEVICE");
        exit(1);
    }
    mem_len = 0;
    for ( i = commandno; i < argc; i++ ) {
        mem_len += strlen(argv[i]) + 2;
        if ( i > commandno ) {
            cmd = (char *)realloc((void *)cmd, mem_len);
        } else { //i == commandno
            cmd = (char *)malloc(mem_len);
        }

        strcat(cmd, argv[i]);
        strcat(cmd, " ");
    }
  if (newline == 0)
        usleep(225000);
    for (i = 0; cmd[i]; i++)
        ioctl (fd, TIOCSTI, cmd+i);
    if (newline == 1)
        ioctl (fd, TIOCSTI, nl);
    close(fd);
    free((void *)cmd);
    exit (0);
}
You will have to compile the c program written above. To do this, copy the code into a file ( ttyecho.c ) then compile it using the make command.

Code: [Select]
make ttyecho
But before you do this, make sure your linux system has a c compiler.

Once this is done, you can execute any command on any psudo terminal in your system. Following is an example of how you can do this.

Code: [Select]
./ttyecho -n /dev/pts/5 "/usr/bin/firefox"
You can find the terminals open in your system with the ps -a command. The above command will open firefox on the screen of the home system, provided pts/5 ( which runs bash ) is open in the system.

If you think about it, you can control what is displayed on the screen of your system by logging into it remotely.

Happy remote controlling  ;)
« Last Edit: March 11, 2015, 12:10:06 pm by Rajiv »