Thread: SSH tunnel

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    9

    SSH tunnel

    hi all,

    i need to transfer data securely and was thinking about using ssh to do so. But i don't know how to treat ssh within my C code.
    So,
    how do i stabilish a ssh connection within my C program? or
    how do i open a ssh tunnel within my C program!?

    maybe you guys know where i can find info about this... i'd aprecciate that too.

    thanks in advance...
    Breno

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Why not use a standard 'scp' or 'sftp' application, both of which operate over a secure link (afaik)

  3. #3
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235

    SSH tunnel

    This probably belongs in the Linux specific board, as it's not really a C language specific query (or response, for that matter.)

    There are programs on Unix/Linux/... that use SSH as a secure tunnel between clients and servers residing on different nodes on a network. These gather the information required from the user for authentication, then create a pipe to ssh that specifies (in the ssh command line) the executable on the remote system that is to be run. The local program then writes the data to the pipe, the remote program sees the data on its standard input, and things proceed as they would for any piped program.

    I've not done this myself, but I'm sure there are specific issues that you need to be aware of when doing this. I believe that CVS does this (when the environment is set up to use ssh rather than rsh, though I suspect that it does exactly the same thing for rsh that it does for ssh). It may be that ssh gets the users password from /dev/tty rather than stdin, which would mean that the users gets prompted for the password without your program having to deal with it.

    I'd suggest reading the man page (and any other available documentation) on ssh for more information.

    I hope this helps.
    Last edited by filker0; 01-16-2006 at 12:26 PM. Reason: Add a comment about this being a platform issue
    Insert obnoxious but pithy remark here

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    29

    api

    I think he's talking about programming with the openssh API. I tried googling myself and the link I remembred was gone but I suggest you simply go on irc.freenode.net and ask in #c (keyword usually c) or in #linux. Just be respectful.

  5. #5
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by stormy
    I think he's talking about programming with the openssh API. I tried googling myself and the link I remembred was gone but I suggest you simply go on irc.freenode.net and ask in #c (keyword usually c) or in #linux. Just be respectful.
    You might find some information on www.openssl.org.

    It's not for the faint of heart though...

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    9
    Cool,

    i'll try taking a look at these places you all told me.

    But i don't know if i made myself clear enough.

    Suppose i created a ssh tunnel manually, i mean, i execute, for example, ssh -L root 192.168.0.74 -l 5555:150.18.7.104:5555 on a shell to create a ssh tunnel.

    After doing that, i run my program, that sends data periodically throught this tunnel. What happens if the other end closes the connection? i'll get broken pipe, right?

    Do i lose the ssh tunnel when that happens!? if i do, how can i automatically reopen this tunnel?

    thanks again.
    Breno

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    My feeling is that in all your stumbling around in the dark, you're going to lose the security you desire.

    Sure you might eventually make something which works, but chances of it being secure as well seem remote.

  8. #8
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    Quote Originally Posted by kastrup_carioca

    Suppose i created a ssh tunnel manually, i mean, i execute, for example, ssh -L root 192.168.0.74 -l 5555:150.18.7.104:5555 on a shell to create a ssh tunnel.

    After doing that, i run my program, that sends data periodically throught this tunnel. What happens if the other end closes the connection? i'll get broken pipe, right?

    Do i lose the ssh tunnel when that happens!? if i do, how can i automatically reopen this tunnel?
    before sending any data through the pipe, test for a valid connection.
    if no connection, open one, send the data, close the connection.
    [ bad practice to keep any connection open unless it is actively being used, it's eating resources for no real purpose to do so. ]
    if either end drops the connection the tunnel is gone.
    Quote Originally Posted by Jeff Henager
    If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    9
    thanks jaqui...

    yeah, i test for the connection before i send any data, and i send data all the time, so i can't close the connection after each and every send().

    So, the tunnel really goes down if the other end breaks the connection.... cool, that's what i thought would happen..

    but now is where the problem is: when the tunnel goes down, how do i reopen it? and i need to do that automatically, either within my c program or writing a script to do so....

    thanks
    breno

  10. #10
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    call ssh in a system call, pass it the login data required [ destination, username, password..]
    I see 4 functions for doing this:
    1) testing connection status
    2) opening connection
    3) sending data [ naturally ]
    4) closing connection

    then run them in this order:
    1,2,1,3 ....... [ repeating as often as needed. ] when shutting down, call 4 before the app sends a kill signal.
    Quote Originally Posted by Jeff Henager
    If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    9
    Thanks Jaqui,

    but how do i call ssh in a system call!?... that's my major problem...
    do you know how to do it or where can i find info on how to do it?

    thanks again..
    breno

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SSH via C program
    By yogesh3073 in forum C Programming
    Replies: 5
    Last Post: 01-12-2011, 04:13 PM
  2. ssh daemon question
    By Overworked_PhD in forum Linux Programming
    Replies: 4
    Last Post: 07-07-2009, 11:44 AM
  3. Windows SSH Wrapper
    By pobri19 in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-04-2009, 04:36 AM
  4. using ssh
    By kris.c in forum Tech Board
    Replies: 3
    Last Post: 12-21-2006, 06:23 AM
  5. carpal tunnel
    By confuted in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-22-2003, 06:28 PM