5

I am trying to get a value from a netcat connection started at a php file, but it dies with:

localhost [127.0.0.1] 2000 (?) : Connection refused

I don't know why, but it works well if I ssh it as apache user (www-data). This is what I've done:

  1. Start an endless loop serving a date with a little delay:

    $ (while true; do nc -l -p 2000 -c "sleep 5; date"; done)&
    
  2. Check if is working:

    $ su www-data
    $ nc localhost 2000
    Fri Oct 16 21:33:20 COT 2009
    
  3. Create /var/www/test.php as follows:

    <?php
    exec('nc localhost 2000>>/var/www/dates.txt 2>>/var/www/errors.txt &');
    ?>
    
  4. Run it on a browser:

    http://myserver.com/test.php
    
  5. Finally take a look at both txt's, dates is empty (nothing like the response in #2) and errors has the "Connection refused" error.

The server is a LAMP cluster running Ubuntu Server 9.04 with DRBD and Heartbeat.

What is driving me crazy is that this test.php works well in my laptop (LAMP on Ubuntu Desktop 9.04) and the server seems to have the ports already open and listening:

$ netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:4743            0.0.0.0:*               LISTEN      2326/openhpid   
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      3364/mysqld     
tcp        0      0 0.0.0.0:2000            0.0.0.0:*               LISTEN      9510/nc         
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3470/apache2    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      2320/sshd       
tcp        0      0 127.0.0.1:3551          0.0.0.0:*               LISTEN      2354/apcupsd    
tcp6       0      0 :::22                   :::*                    LISTEN      2320/sshd

This is what I really want to archive: diagram (I don't have enogh points to insert images yet, lol)

  • send the weight value from the truck scale to the server when the server requests it
  • send raw text (epson escaped text format) to the printer's serial port

So in the client pc are constantly running two listening netcat connections, one for getting the weight and the other to print raw text.

3

3 Answers 3

2

This command needs a space between the port number and the redirect.

nc localhost 2000 >>/var/www/dates.txt 2>>/var/www/errors.txt

Incidentally there is sure to be a more efficient and secure way to achieve the direction you're going in.

3
  • I'm going to give it the space between them but that works fine in the console, the problem is making that work from a php. If you know a better way to archive this please tell me!!!
    – coma
    Oct 17, 2009 at 16:31
  • Could you explain the purpose of what you're doing?
    – Dan Carley
    Oct 18, 2009 at 8:49
  • Of course!, let me edit the question to clarify the purpose of this.
    – coma
    Oct 18, 2009 at 17:12
1

Well, it was a permission problem after all... fixed editing /etc/sudoers with visudo to add:

www-data ALL = NOPASSWD: /bin/nc
3
  • 4
    This is horribly insecure. You just allowed anyone who breaks into your web application to listen on any port and worse yet run any application AS ROOT.
    – Bort
    Feb 6, 2013 at 15:40
  • 1
    Well, this was for an intranet app (in the middle of the colombian jungle btw, running by ethernet cables between only two pc's without internet) that we drop about one year ago, in fact this is from 2009 and we are in 2013, so thanks for the downvote dude.
    – coma
    Feb 6, 2013 at 16:40
  • 10
    None of that was in the question. The rest of the users reading this might try the same tactic and fall victim because they aren't in the middle of a jungle.
    – Bort
    Feb 6, 2013 at 16:51
0

Check that the local interface is up.

ip link

should tell you. It's possible that the local interface is down, unlikely, but possible.

You want to see something like :

1: lo: <LOOPBACK,UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

Also, have a look at ip route. See if there's anything absolutely crazy going on there.

1
  • the local interface is up, in fact, "nc localhost 2000" and "nc 127.0.0.1" work in the console but they give me the error when runned in a php file with "exec" or "shell_exec".
    – coma
    Oct 17, 2009 at 16:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .