2

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!

AndyL
  • 1,615
  • 1
  • 13
  • 15

3 Answers3

1

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.

Cfreak
  • 19,191
  • 6
  • 49
  • 60
  • Why sleep? Why not check to see if the prompt actually comes up (was received) instead of using a race condition? – netcoder Jun 08 '11 at 14:26
  • @netcoder - true. I didn't state it very clearly. One would need to check for it, sleep if it hasn't appeared and then check again until it does or until some kind of timeout is reached. Hopefully a pre-built module would take care of all of that :) – Cfreak Jun 08 '11 at 14:29
  • @AndyL: Something like [fread](http://php.net/fread) or [fgets](http://php.net/fgets). – netcoder Jun 08 '11 at 15:32
1

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

symcbean
  • 47,736
  • 6
  • 59
  • 94
0

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

See function login($username, $password).

Ugo Méda
  • 1,205
  • 8
  • 23