LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   create ssh tunnel using perl (https://www.linuxquestions.org/questions/programming-9/create-ssh-tunnel-using-perl-681413/)

aarontwc 11-05-2008 10:14 PM

create ssh tunnel using perl
 
Hi there,

Using Perl, I would like to start an SSH tunnel and then run vncviewer to connect the port. I understand I can do so very simply with the following command:
system("ssh -l <username> -L 10000:localhost:5901 <remote host>");
vncviewer localhost:10000

But then the process does not execute the vncviewer command.

Any idea? I think I need to do something using either threading or fork. But I am not sure how I can do so with either.

Thanks
aaron

estabroo 11-05-2008 10:43 PM

yes you need to thread or fork, the system call doesn't return until the process it is running has finished when is why vncviewer command isn't running.

Code:

$not_yet = 1;
$pid = fork();
if ($pid == 0) {
  $not_yet = 0;
  system("ssh -l <username> -L 10000:localhost:5901 <remote host>");
  exit(0);
} elsif ($pid > 0) {
  while ($not_yet == 1) {
    sleep(1);
  }
  sleep(5); # not a guarantee that the ssh has already run
  # really should check the output of the vncviewer and react to error conditions
  system("vncviewer localhost:10000");
} else {
  print "fork failed\n";
}


aarontwc 11-05-2008 11:03 PM

Hi there,

Okay, I tried your solution, but ssh is still having control over the process. Basically, when i run
system("ssh -l <username> -L 10000:localhost:5901 <remote host>");

It brings me into the console of the ssh server, and vncviewer does not load.

I tried:
system("ssh -l <username> -L 10000:localhost:5901 <remote host> -N");
system("ssh -l <username> -L 10000:localhost:5901 <remote host> &");

Both didn't work correctly too.

Any idea?

Thanks
Aaron

estabroo 11-05-2008 11:55 PM

yes remove the $not_yet references. I'd forgotten that in perl variable space isn't shared on a fork()

Code:

$pid = fork();
if ($pid == 0) {
  system("ssh -l <username> -L 10000:localhost:5901 <remote host>");
  exit(0);
} elsif ($pid > 0) {
  sleep(5); # not a guarantee that the ssh has already run
  # really should check the output of the vncviewer and react to error conditions
  system("vncviewer localhost:10000");
} else {
  print "fork failed\n";
}


aarontwc 11-18-2008 07:54 PM

Hey estabroo,

I just realised that this solution works but if i were to exit the program (i.e. close vncviewer), the processes are still being run in the background. Are there any ways I can kill the child process and the parent process?

I understand I have to use something like signals. Are there weres that I don't have to use signals. If i have to, do you mind guiding me a little on the implementation of it?

Thanks so much for your help
Aaron

aarontwc 11-19-2008 03:02 AM

Anyone any ideas? I am sure I am just missing something very small here.

How do I actually kill the child process when the program is closed?
i.e. my ssh tunnel is still active even though I closed my program

Thanks!!
Aaron

estabroo 11-19-2008 11:48 AM

you can use the kill() function in perl to send the child process a signal like TERM


All times are GMT -5. The time now is 04:09 AM.