How do I use fsockopen() to open a Telnet connection with a password?

不问归期 提交于 2019-12-01 12:01:44

You just output it. Some examples I've seen use fputs. You might have to sleep for a second to make sure the prompt comes up. There's actually an example in the comments on the fsockopen manual page: http://php.net/manual/en/function.fsockopen.php

Really though I'd recommend looking for a module that does this. A quick google shows there are several out there. I don't want to recommend a particular one because I haven't used any of them.

It would be a better idea to use proc_open to run telnet rather than trying to implement your own protocol stack (there's more to telnet than just reading and writing from sockets). Indeed, telnet is inherently insecure and should be avoided if at all possible. (basic http authentication without SSL is just as bad).

However unlike SMTP or HTTP, it's not a very complex protocol - and it should be fairly straightforward to implement a simple client using sockets. The code you've provided neither reads the username / password prompt nor writes responses to the socket - so either you've got some very strange ideas about how to login over telnet or the code snippet is irrelevant.

Cfreak said "You might have to sleep for a second to make sure the prompt comes up" - this is not correct - you must wait for the username prompt, the password prompt and the initial CLI prompt before sending a response using telnet. Indeed there is a whole programming language (expect) written to work around this kind of odd behaviour in telnet.

and BTW, telnet runs on port 23 - port 25 is used for SMTP

There is a class implementing loginc over telnet right here : http://www.dali.net.nz/Telnet.class.php.txt

See function login($username, $password).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!