Make PHP respond to telnet (PHP Telnet Server) - php

I have a project to do and it goes like this:
When you open a telnet console and telnet to my server, PHP should respond with:
"Hello, What is your name?"
And you type in your name and so on. How can I do this in PHP? Is it even possible?

See http://php.net/manual/en/book.sockets.php
This is a good tutorial to get you started. http://devzone.zend.com/article/1086
And google is your friend here as well: http://www.google.ca/search?q=php+socket+server

Actual TELNET can be a little difficult to get correct; there are a lot of control sequences that are used to influence how a terminal displays its contents.
If you simply want to write a service that people can use via TELNET, then it would probably be best to use an existing TELNET server (Ubuntu has several packages available, for simple TELNET: inetutils-telnetd, telnetd; for encrypted TELNET: telnetd-ssl; for Kerberos-authenticated TELNET: heimdal-servers, krb5-telnetd.) to provide TELNET functionality, and set your PHP program as the login shell for whichever user you want everyone to log in as. (See passwd(5) for information on the login shell.)
If you want to do all the socket operations yourself, then you'll need to use the BSD sockets support; create a socket, bind a server name to the socket, listen on the socket, and when a new connection comes in, fork a new process to handle the new client, and accept the new connection.
Once you have a new process with a connected client, you can use the standard read and write operations to send and receive data to the client. If you really want to support the full TELNET specification, it'll take some clever programming. If you don't mind not having access to advanced terminal features (such as provided by readline(3), ncurses(3), or slang) then you may need to drop characters with the eighth bit turned on to get rid of the TELNET control characters from your clients. (I've not tried this, because I've always used the netcat tool when I wanted a clean unencrypted and unauthenticated connection between hosts.)

Related

PHP script running as Socket Server and Client simultaneously

For a new project I need to implement remote desktop protocols. The addresses of the remote need to be secured and may never get sent to the client. After a lot of research and some tests I found Guacamole, which also has a Java client. The project is designed as an API though, so I started porting some bits of the Java client example to PHP.
The use case will be the following:
User logs into my service (Laravel application)
WebSocket connection establishes to a constantly running PHP script (using HOA\WebSocket)
Upon authorization a TCP socket needs to be established to the Guacamole Daemon
Commands coming via WebSocket need to be directed to the Guacamole Daemon and vice versa
What makes this complicated is the fact that the application needs to be able to serve multiple clients simultaneously. Multiple TCP sockets need to be established and multiple WebSocket connections need to be managed all at once.
For my simple test I opened the socket via fsockopen and then looped to wait for data. With this I obviously can't listen to multiple sockets at once (at least realistically), but I stumbled upon the React Socket Client library:
Think of this library as an async version of fsockopen() or stream_socket_client().
This sounds like it is what I need, but then again, I'm using HOA and its WeSocket server, which apparently also runs in a loop (when invoking WebsocketConnectionHandler->run()).
Should I even be using React's Socket Client or should I try to use HOA's Socket library instead (seeing as I'm already using WebSocket from that)? Are React and HOA even compatible in their event loop, so could I listen to WebSocket clients and a TCP connection at the same time?
If so, could anyone give me some hints or examples on how to get started with coupling these two? Thanks!

Request service name via TCP socket

I'm making a simple port scanner in PHP, and am trying to figure out how I'd request the name of the service I'm connecting to, similar to how Nmap is able to discover services.
I'm using fsockopen() to open the socket, so I'd use fwrite($socket, "WHAT SERVICE AM I COMMUNICATING WITH?") to ask the question, then listen using fgets($socket).
How do I ask the service what it is?
It's hard coded into the program what the different ports are for. The remote applications don't report them. There are tons of lists on the internet that provide this information.
Same thing for looking at standard services and responses from similar services on different OSs. That's how nmap guesses what OS it's talking to.
Here is a decent starting point.
Here is the source file with the port mappings for nmap.
The scary part is the code that tells which versions and such is running. That's here and I'm glad I don't have to write this :\
Last one, pretty sure this is the file that says how to guess which OS a remote computer is: here

PHP Socket Help Understanding

Im trying to understand how a socket works in PHP.
Lets say I have a file called socket.php, and this creates a socket bound to my localhost on port 99.
Then I run the socket in a while loop so it's constantly connected.
is there a function in PHP to make calls to that socket while its listening?
Another question is: If I have another service such as Java running on a socket -- is it a bad idea to use PHP to connect to the socket to make a call. I ask because I could potentially be recreating new socket connections many, many times.
So is having to reconnect to a socket hundreds of times in PHP bad? Or should I re-use the same socket connection somehow? (I am thinking in terms of AJAX calls to PHP which connects to a Java Socket).
Edit: You can see the example code: https://github.com/JREAM/sandbox/tree/master/php
Im trying to communicate with in socket.php and socket_send.php -- I am leaving socket.php running and opening another console and running socket_send.php and trying to get a result into the console.
Answer to your first Question: I suggest going over here everything you need about sockets is there. Basically the function you want to use is socket_read or socket_recvfrom if using UDP.
Answer to your second Question: Sockets are just a way to send messages to services. It doesn't matter if a client is in php and the server is in Java. Think of it this way. Does it matter that you are viewing a web-page on a linux Web Server with a windows Box?

PHP Post Data Over VPN

I need to connect to a webservice which is behind of a VPN via PHP. My server is Debian Linux (Squeeze).
Is it possible to accomplish this via PHP on Linux?
Is it risky to do this if it is possible? (When VPN connection hangs etc., does the operating system or any other what-so-over handles the situation)
I have only one network card, therefore I really wonder whether it is possible to keep server online for normal users while "posting data over an accomplished VPN connection in the background".
Although my question seems to a conceptual question, any specific help is also welcome.
Server OS : Debian Linux Squeeze (x64)
Web Server : Apache HTTP
PHP Version: 5.3
Framework: Symfony 1.4
VPNs are at a network layer below PHP, PHP won't know or care that the connection is over a VPN or a normal connection. It's handled by the network stack.
If you use a permanent one (e.g. IPSEC) then PHP doesn't need to create the connection, it's just there to use when PHP connects to an IP address that is in the VPN. It is selected to use by the network layer when it does the routing, not by PHP. This is true even if you create the VPN on demand, as jderda suggested using exec() or similar. But a permanent connection is better (IPSEC).
So to answer your questions:
The question doesn't make sense, the only way PHP could do this is using PPTP or similar and exec() to bring the connection up, but better to use IPSEC
If the VPN connection hangs/dies PHP won't get a connection to the remote end and will timeout the connection.
Yes it is.
From PHP point of view, the VPN is just a plain network connection. It does not require additional handling.
If you want to dynamicaly estabilish a VPN connection, you'll probably need to use exec() and some commandline tool for estabilishing a connection. But as such connection doesn't interfere with normal network communication (as long as it's properly configured, with other subnet ip range), you should estabilish it once and keep it active for PHP and other apps to use.

PHP doing SNMP walk on remote devices/clients?

I am trying to create a new application for our company to do an SNMP walk.
Here's what I am trying to achieve. The customer logs into our website. Clicks a button saying "Do SNMP Walk" and a PHP script will do a walk, and parse the data into the MySQL DB, and that's it.
Is this possible to do with PHP?
I was reading on http://php.net/manual/en/function.snmpwalk.php and I seen that it looks like it is, but I wasn't sure because the clients wouldn't be on the same network as the server the script is running from (we'd be using godaddy.)
Thanks!
If you're trying to SNMP walk devices in the client's network from your GoDaddy server, then the answer is no: you won't be able to do it.
SNMP is typically not passed through firewalls, so you would need to deploy your PHP code on the same network as the client.
This is wrong... snmp, is passed like any other data packets, unless the router is specifically configured to block it. I frequently use it to access routers across the internet to different providers and such.
Its possible that GoDaddy has blocks on that, but I have never seen this. You may want to install a copy of snmp on your computer and do an snmpwalk from the commandline.

Categories