Simple telnet example in LIBCURL - C++

2019-07-14 15:16发布

问题:

I need to fing simple TELNET example in LIBCURL (curl.haxx.se/libcurl) (C++) I searched over this site, but I don't found any simple example. I need only to connect to TELNET, authenticate, and send message.

Thanks

回答1:

All you can do in libcurl is send data and recieve data. There is no way to wait for response or send data based on response. The whole point of libcurl is to handle waiting for data and responses for HTTP or FTP.

That being said, you may be able to do something with CURLOPT_READFUNCTION and CURL_READFUNC_PAUSE. Return PAUSE from READFUNCTION, then call curl_easy_pause(CURLPAUSE_CONT) when you see "login:" in your READFUNCTION. Be prepared to return your user name from READFUNCTION when it gets called.

I've never done this, so I can't vouch for how it works. But from API description, this seems to be the way to go...



回答2:

NOTE: The telnet protocol does not specify any way to login with a specified user and password so curl can't do that automatically. To do that, you need to track when the login prompt is received and send the username and password accordingly.

You can look it here: http://www.cs.sunysb.edu/documentation/curl/index.html

Maybe you should make it manually like:

system("curl telnet://192.168.2.1");

Later it will ask your username and password.