Netcat "Connection refused" on localhost

I am trying to get a value from a netcat connection running in 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 an apache user (www-data), this is what I did:

1) Start an endless loop serving the date with a slight delay:

$ (while true; do nc -l -p 2000 -c "sleep 5; date"; done)&

2) Check if it works:

$ su www-data
$ nc localhost 2000
Fri Oct 16 21:33:20 COT 2009

3) Create /var/www/test.php as follows:

<pre><?php
exec('nc localhost 2000>>/var/www/dates.txt 2>>/var/www/errors.txt &');
?></pre>

4) Run it in a browser:

http://myserver.com/test.php

5) Finally, take a look at both txt, the dates are empty (nothing like the answer in # 2), and the errors have a "Connection refused" error.

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

, , test.php (LAMP Ubuntu Desktop 9.04), , , :

$ 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

, , , , .

!!!

+3
1

, ... /etc/sudoers visudo :

www-data ALL = NOPASSWD: /bin/nc
0

Source: https://habr.com/ru/post/1720332/


All Articles