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

AndyL picture AndyL · Jun 8, 2011 · Viewed 14.5k times · Source

I'd like to access a camera through it's Telnet capability. The problem is, it has Password-protection. This is no problem when doing it via Terminal, as I just use telnet 10.30.blah.blah then enter my password when prompted. But in php, I don't see the opportunity to input a password.

$con = fsockopen("10.30.blah.blah", 25);
$msg = "camera move left";
fwrite($con, $msg);

Anybody have any ideas?

UPDATE: I tried just using fputs to output the password as @Cfreak said, but still to no avail. If I do exactly what the script is trying in terminal, it works. Here's the code now:

$con = fsockopen("10.30.blah.blah", 23, $errno, $errstr, 30);
$pass = "admin";
sleep(5);
fputs($con, $pass);
sleep(5);
$msg = "camera move left";
fputs($con, $msg);

UPDATE: Found that I needed a \r at the end of my $msg variable. Thanks for the help!

Answer

Cfreak picture Cfreak · Jun 8, 2011

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.