Re: One for the Unix gurus
Re: One for the Unix gurus
- Subject: Re: One for the Unix gurus
- From: p3consulting <email@hidden>
- Date: Sat, 26 Jun 2004 17:51:34 +0200
Have you taken a look at expect ?
(man 3 expect)
It's possible to link a C (and thus Objective-C) program to libexpect
(not installed on Mac OS X you have to download and compile expect AND
tcl - the one shipping with Mc OS X doesn't include private headers
required to compile expect)
from man libexpect (after installing it)
"exp_spawnl and exp_spawnv fork a new process so that its stdin, stdout,
and stderr can be written and read by the current process. file
is the
name of a file to be executed. The arg pointers are
null-terminated
strings. Following the style of execve(), arg0 (or argv[0]) is
custom-
arily a duplicate of the name of the file."
I have done a very basic testing by connecting to a ftp server and it
worked fine:
#include <stdio.h>
#include "expect.h"
void timedout()
{
fprintf(stderr,"timed out\n");
exit(-1);
}
int main()
{
int fd;
FILE *fp ;
exp_loguser = 0;
exp_timeout = 3600;
if (NULL == (fd =
exp_spawnl("ftp","ftp","-V","your.ftp.server",NULL))) {
perror("ftp");
exit(-1);
}
if (NULL == (fp = fdopen(fd,"r+")))
return(0);
setbuf(fp,(char *)0);
if (EXP_TIMEOUT == exp_fexpectl(fp,
exp_regexp,"Name.*:",0,
exp_end)) {
timedout();
}
fprintf(fp,"your_login\r\n");
if (EXP_TIMEOUT == exp_fexpectl(fp,
exp_glob,"Password:",0,
exp_end)) {
timedout();
}
fprintf(fp,"your_password\n");
fprintf(fp,"%c",0x03); // disconnect
return 0 ;
}
Of course establishing a true dialogue will require a lot more work to
get working the regular expressions used in exp_fexpectl for every
situation...
Pascal Pochet
email@hidden
----------------------------------
PGP
KeyID: 0x208C5DBF
Fingerprint: 9BFB 245C 5BFE 7F1D 64B7 C473 ABB3 4E83 208C 5DBF
Le juin 26, 2004, ` 02:31, Glenn Zelniker a icrit :
>
I need to integrate some ssh functions (primarily scp) into an
>
application and I've been experimenting with writing wrappers around
>
Unix commands. I'm taking the usual approach -- using NSTask, NSPipe,
>
and NSFileHandle. I launch the task from the proper location, pass the
>
command-line arguments via an NSArray, pipe stdin, stdout, and stderr
>
as needed, and react to NSNotifications triggered by asynchronous
>
background reads. This works fine for simple commands like "ls" and
>
I've verified that I can execute "ls" with multiple command-line
>
arguments, such as "ls -l -a /myfiles." I've also verified that I get
>
output through the pipe I set up for stdout and errors through the
>
pipe for stderr. So far, so good...
>
>
The problem is that I can't seem to get things like scp or ftp
>
working. For example, when running ftp from the command line, one
>
normally sees on the terminal a stream of information welcoming you to
>
the host, disclaimers, and finally an FTP> prompt. When I run ftp from
>
the GUI I've written (and I'm sure the arguments are being parsed
>
correctly), I only see the FTP> prompt in the NSTextView I've set up
>
to log stderr. Where does the other "stuff" go? It doesn't seem to go
>
to the pipe I've set up for stdout. When I set the associated handle
>
for readInBackgroundAndNotify, I'm looking out for
>
NSFileHandleReadCompletionNotification. Should I be using a different
>
notification and is this why I'm missing the welcome notice? Is there
>
a place besides stdout and stderr that it's going to?
>
>
I won't even talk about the stuff I'm trying to send to the command's
>
input -- I want to understand what's happening with the output first!
>
Any help would be appreciated immensely.
>
>
Glenn Zelniker
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.