LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-05-2008, 10:14 PM   #1
aarontwc
Member
 
Registered: Oct 2008
Posts: 66

Rep: Reputation: 15
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
 
Old 11-05-2008, 10:43 PM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
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";
}

Last edited by estabroo; 11-05-2008 at 10:47 PM. Reason: accidentally hit return early
 
Old 11-05-2008, 11:03 PM   #3
aarontwc
Member
 
Registered: Oct 2008
Posts: 66

Original Poster
Rep: Reputation: 15
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
 
Old 11-05-2008, 11:55 PM   #4
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
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";
}
 
Old 11-18-2008, 07:54 PM   #5
aarontwc
Member
 
Registered: Oct 2008
Posts: 66

Original Poster
Rep: Reputation: 15
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
 
Old 11-19-2008, 03:02 AM   #6
aarontwc
Member
 
Registered: Oct 2008
Posts: 66

Original Poster
Rep: Reputation: 15
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
 
Old 11-19-2008, 11:48 AM   #7
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to create ip tunnel ???? wahaha Linux - Software 5 03-19-2007 02:45 AM
setting up an ssh soxy or local ssh tunnel from within an ssh soxy Mangenius Linux - Networking 0 03-05-2007 04:15 PM
How to create an SSH tunnel at boot up fregster Linux - General 7 11-15-2006 05:02 AM
need to create one gre tunnel with different sessions sureshchandak Linux - Kernel 1 06-23-2006 02:41 PM
ssh tunnel crep Linux - Networking 2 08-25-2004 09:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration