UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2017-11-14 10:45 AM

xdev
Member
Registered: 2017-11-14
Posts: 2

ssh interaction using libexpect and c

Hi,

I have created a c program using expect to login a server using ssh, the following is the program

#include <sys/wait.h>
#include <tcl8.6/expect.h>
int main()
{
exp_is_debugging = 0;
exp_timeout = 60;


FILE *expect = exp_popen((char *) "ssh [email protected]");
if (expect == 0) {
    return 1;
}

enum { denied, invalid, command_not_found, 
        command_failed, prompt };

switch (exp_fexpectl(expect,
            exp_glob, "password: ", prompt,
            exp_end)) {
    case prompt:
            // continue 
            break;
    case EXP_TIMEOUT:
            return 1;
}

   fprintf(expect, "%s\r","password");

 switch (exp_fexpectl(expect,
            exp_glob, "denied", denied, // 1 case       
            exp_glob, "invalid password", invalid, // another case
            exp_glob, "#", prompt, // third case           
            exp_end)) {
    case denied:
    printf("denied %d",exp_glob); 
             break;
    case invalid:
            break;
    case EXP_TIMEOUT:
    printf("EXP_TIMEOUT"); 
            break;
    case prompt:
            printf("loggedin");        
            break;
    default:
    //return 0;
            break;
}
}

I am able to login to the server but it immediately gone to my terminal after this, what i am looking to login to the server and need to interact with that terminal. Now, it is showing my terminal after that, do anyone have any idea on this ? i need to stay on that logged server's terminal

Offline

#2 2017-11-19 10:22 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: ssh interaction using libexpect and c

Hi xdev,

Apologies for the late reply, this forum isn't as active as it used to be, so we check it less often.

The reason your program exits is because it's done. If you want to keep the connection open
and interact with it as a user, you probably need to read standard input and write it to the other
end (and vice versa) in a loop.

It is unclear to me what your goal is: If you want a human to use ssh, then why have your
program in between? If you want to do things automatically, why not use ssh to run what
you want directly? Do you need a local program to interact with a remote program, but the
password prompt makes normal piping impossible?

Offline

#3 2017-11-20 02:04 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: ssh interaction using libexpect and c

The traditional method of getting around the password prompt is to use an unencrypted auth key for login instead of a password...  If that's the only reason you're using libexpect, I'd recommend doing the same...

Offline

Board footer

Powered by FluxBB