LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 02-07-2004, 08:53 AM   #1
ailiez
LQ Newbie
 
Registered: Feb 2004
Posts: 3

Rep: Reputation: 0
Pseudo Terminal Problems


Hi,

I want to programmatically control the SSH or telnet to login from a running process. I tried using redirection through pipes but ssh returned an error saying something like stdin is not a terminal. Now, my search has finally led to me a solution: through the use of pseudoterminals. So I made a test program (listed below).

Supposedly I can write to stdin by writing to the file desc of the master pty. But it seems the co-process (ssh) is not reading what I wrote from the slave pty. I don't know what's wrong?

Here is the test program:
#include <pty.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>

main()
{
int fds;
char sSlave[80];
int n;
char buffer[256];
pid_t pid;

if (pid = forkpty(&fds,sSlave, NULL, NULL ) == 0)
{
execl("/usr/bin/ssh","ssh", "root@localhost", NULL);
return 2;
}

// Simulate a user typing his username and password
// to the terminal

write(fds, "username", strlen("username"));
write(fds, "password", strlen("password"));

}

Thanks.
 
Old 02-08-2004, 12:08 PM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
A working implementation
Code:
#include <pty.h>
#include <iostream>


void readAndShow(int fd,char *buffer,int size) {
        int ret=read(fd,buffer,size);
        std::cout << "Bytes: " <<ret << std::endl;  
        if(ret>0) buffer[ret]=0; else return;
        std::cout << "Out: \n" <<buffer << "\n---\n";
        sleep(3);
}

int main() {
        int fds;
        char sSlave[80];
        int n;
        char buffer[256];
        pid_t pid;
        
        if (pid = forkpty(&fds,sSlave, NULL, NULL ) == 0) {
                execl("/usr/bin/ssh","ssh", "root@localhost", NULL);
                return 2;
        }

        // Simulate a user typing his username and password
        // to the terminal
        readAndShow(fds,buffer,250);
        write(fds,"aRootPW\n",strlen("aRootPW\n"));
        readAndShow(fds,buffer,250);
        readAndShow(fds,buffer,250);
        readAndShow(fds,buffer,250);
}
Sample run
Code:
toni@mario:~/lwork/tst$ ./ptytst 
Bytes: 27
Out: 
root@localhost's password: 
---
Bytes: 2
Out: 


---
Bytes: 250
Out: 
Linux mario 2.4.24 #1 pe helmikuun 6. 12:59:24 EET 2004 i686 GNU/Linux
Olet mariolla. 
Että sellasta...
No mail.
Last login: Sun Feb  8 19:02:51 2004 from localhost
root@mario:
---
Bytes: 3
Out: 
~$ 
---
toni@mario:~/lwork/tst$
btw. if you are trying to automate something with ssh, I recommend using ssh keys instead of passwords. In case things has to be done without user interaction, I would use passwordless keys instead of password contained in the software.
See the ssh2 howto for details.
 
Old 02-09-2004, 02:58 AM   #3
ailiez
LQ Newbie
 
Registered: Feb 2004
Posts: 3

Original Poster
Rep: Reputation: 0
tnx...=)

i have tried the program and its working tnx tnx...

we are trying to create a program that will allow the user to send files to the remote computer without the user having to do anything except use out program to send the file, this is why we have ruled out using keyless login...

have a good day...
 
  


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
pseudo-Xinerama someone Linux - Software 0 08-25-2005 11:42 PM
Regarding Pseudo tty, Pseudo terminals ? mqureshi Programming 0 07-30-2005 11:51 AM
Switch to pseudo terminal you didn't log out of? xnomad Linux - General 2 07-19-2005 01:35 AM
Questions about (Pseudo)Terminal devices beginner16 Programming 0 04-09-2004 11:03 AM
Pseudo tty configuration RH 7.3 Dalma Linux - Networking 1 06-17-2003 01:53 PM


All times are GMT -5. The time now is 02:00 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration