Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] calling telnet from a caml program
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Alan Schmitt <alan.schmitt@p...>
Subject: Re: [Caml-list] calling telnet from a caml program
In response to my own post, if someone has the same problem ... I simply
used Cash. Here is the result:

let pid, pin, pout, pty_name = Cash.fork_pty_session (fun () -> Cash.exec
"/usr/bin/telnet" [])
in
let str = String.create 1024 in
let res = input pin str 0 1024 in
print_int res;
print_newline ();
print_endline str;
flush stdout

compiled with:
ocamlc -I +site-lib/pcre unix.cma str.cma camlp4/camlp4.cma pcre.cma cash.cma main.ml -o test

I get:
schmitta@localhost:~/SpeedTouchPro> ./test 
8
telnet> 

which is what I wanted ;-)

I still have a couple other small issues, but it feels like a giant leap
;-)

Alan

* Alan Schmitt (alan.schmitt@polytechnique.org) wrote:
> Hi,
> 
> I've spent a couple hours trying to make this work, but it does not want
> to ... I want to call telnet from a caml program, send some commands,
> and read some output. However there is something weird going on, as
> telnet does not seem to flush anything.
> 
> I've tried the following:
> 
> let pin,pout = Unix.open_process "/usr/bin/telnet" in
> print_endline "got it";
> flush stdout;
> output_string pout "open 127.0.0.1\r\n";
> flush pout;
> let str = input_line pin in
> print_endline str;
> output_string pout "quit\r\n";
> flush pout;
> print_endline "got it";
> flush stdout
> 
> which does not work (there should at least be the line "Trying
> 127.0.0.1")
> 
> and the following (but I am probably completely mistaken about the
> handling of pipes):
> 
> let outin, outout = Unix.pipe () in
> let inin, inout = Unix.pipe () in
> let pid = Unix.create_process "telnet" [| |] inin outout Unix.stderr in
> Unix.set_nonblock outin;
> let str = String.create 5 in
> let res = Unix.read outin str 0 1 in
> print_int res;
> print_newline ();
> print_endline str;
> flush stdout
> 
> (this should at least return the "t" of the telnet prompt ... but it
> fails with an exception saying there is no data to be read).
> 
> I'd really someone to point to me what I am doing wrong ...
> 
> Best,
> 
> Alan
> 
> 
> -- 
> The hacker: someone who figured things out and made something cool happen.
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> 


-- 
The hacker: someone who figured things out and made something cool happen.
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners