I copied the following script and run it to have it listen on port 80. But netstat doesn't show port 80. Why does netstat not sow it, or the Perl script is not correct?
#!/usr/bin/perl -w
use Socket;
use IO::Handle;
$port=80;
$host='localhost';
$packhost=inet_aton($host);
$address=sockaddr_in($port,$packhost);
socket(SERVER,AF_INET,SOCK_STREAM,getprotobyname('tcp'));
bind(SERVER,$address);
listen(SERVER,10);
while( 1 ) {
next unless (accept(CLIENT,SERVER));
CLIENT->autoflush(1);
$msg_out="WHAT DO YOU WANT?\n";
send(CLIENT,$msg_out,0);
close CLIENT;
}
close SERVER;
exit 1;
What platform are you on? How are you invoking netstat?
On Windows XP, after running the script with admin privileges, netstat -a gives me:
TCP aardvarkvi:http aardvarkvi:0 LISTENING
Binding to ports below 1024 requires root privileges on *nix systems. Since you do not (or, shall I say, code you seem to have blindly copied does not) check the return values of various calls, you would not know if they failed.
In general, you should not have to use Socket.pm. Stick with IO::Socket and avoid blindly copying code without knowing what it does.
You might also want to look into HTTP::Daemon.
It's likely that netstat is replacing the numeric port number by the name from /etc/services. For example:
~, 503> netstat -a | more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:svn *:* LISTEN
One thing that you can do is grep netstat's output to find all sockets where it's listening:
netstat -a | grep LISTEN | grep tcp
You can also tell netstat to show numeric addresses rather than doing a hostname or services lookup (and there's another option where you can limit just port numbers; do man netstat):
netstat -an | grep LISTEN | grep tcp
Sorry, my fault, when I run netstat, I didn't put the option -a. When use netstat -a, it shows that port.
Related
i actually use a dbeaver client which executes a pg_dump command
on a remote server. The command starts exactly like that :
pg_dump --verbose --host=127.0.0.1 --port=47855 --username=user-accounet..
i dont't know how dbeaver creates ssh tunnel (it uses a bastion)
but, it is not the question.
the question is : when i excute the command below..
lsof -i -P -n | grep pg_dump
i get this :
pg_dump 14144 parcss-alexco 3u IPv4 397966 0t0 TCP 127.0.0.1:35978->127.0.0.1:47855 (ESTABLISHED)
what is this adress ip : 35978 ?
what kind of binding 35978 --> 47855 means ?
Does it concern a remote ip adresse ? local ?
i'd like to understand..
A TCP connection is between to sockets. A fully specified socket has an IP address and a port number.
pg_dump uses port 47855 on 127.0.0.1 for the remote socket for the connection, but what about the socket on the other end? The IP address is clear, but what is the port number? Since the socket is not explicitly bound to a certain port with bind(2), connect(2) will assign an “ephemeral port” number. This happens to be 35978 in your case.
I have a Abinitio process running on a Red hat server.
This process is basically a webservice, which is bound to a port on this running unix server.
The front end sends request to this UNIX PORT, which is in turn read by abinitio process and processed further.
How can I identify if the process is bound to the unix port or not?
I face a weird situation every Monday(over the weekend), When I try to hit the webservice through SOAP , I get socket timeout exception.
TO solve this I have to stop and start the process.
I want to identity which all sockets are not responding , given the list of ports.
netstat -lnp will list the pid and process name next to each listening port. This will work under Linux, but not all others (like AIX.) Add -t if you want TCP only.
root#c27bf9ed63c5:/# netstat -lnpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 1/java
tcp6 0 0 :::8009 :::* LISTEN 1/java
tcp6 0 0 :::8080 :::* LISTEN 1/java
Since this is on a UNIX, lsof -i :port_number will give you details of what process is bound to the given port
Use netstat command.
netstat -anp | grep pid
You can also use the nmap to check which ports are listening for a machine.
nmap -sT -O machine_ip
Why the following command (on my Debian 8.4 machine) doesn't output an error of type "Address already in use" when I execute it from two different terminals?
netcat -p 1234 -l
I wonder why it doesn't throw an error since it starts two processes listening on the same port.
Doesn't netcat use sockets? How is it possible?
On my system, running strace nc -l 1234 finishes with:
socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) = 3
setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
setsockopt(3, SOL_SOCKET, SO_REUSEPORT, [1], 4) = 0
bind(3, {sa_family=AF_INET, sin_port=htons(1234), sin_addr=inet_addr("0.0.0.0")}, 16) = 0
listen(3, 1) = 0
accept(3,
So the socket is setup with the options SO_REUSEADDR and SO_REUSEPORT, which allow multiple processes to bind to the same port and same listening address. See man 7 socket or this detailed answer. The goal of this option is to allow an easy form of load balancing: incoming connections to the port will be redirected to one of the processes (apparently at random).
The -p option specifies a source port, not a listening port.
The -l option puts netcat into listening mode.
In your example, 1234 is the input value for the -p option, not the -l option, which means there is no explicit listening port being specified. If netcat is not failing, then most likely netcat is binding to port 0 instead, which tells the listening socket to bind to a random available ephemeral port. As such, your two netcat instances would actually be listening on different ports. Use netstat to verify.
According to the Linux manpage for netcat:
-l' Used to specify that nc should listen for an incoming connection rather than initiate a connection to a remote host. It is an error to use this option in conjunction with the -p, -s, or -z options. Additionally, any timeouts specified with the -w option are ignored.
-p source_port
Specifies the source port nc should use, subject to privilege restrictions and availability. It is an error to use this option in conjunction with the -l option.
So technically, your example may not be valid to begin with.
However, on some systems, including some Debian installs, depending on which flavor of netcat you use (in particular, the traditional flavor), you actually may need to use -l and -p together, but you need to swap their order to specify a listening port correctly, eg:
nc -l -p 1234
I can connect fine with PHP and locally using Psql, but Perl does not.
my $dbh = DBI->connect("DBI:Pg:dbname=mydb,host=localhost:5432","user","pass",{'RaiseError' => 1});
I believe the error is because my socket is in tmp:
postgres#host/opt/psql/bin $ netstat -an | grep 5432
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN
tcp6 0 0 :::5432 :::* LISTEN
unix 2 [ ACC ] STREAM LISTENING 24728255 /tmp/.s.PGSQL.5432
unix 3 [ ] STREAM CONNECTED 24729004 /tmp/.s.PGSQL.5432
And when I run my simple perl script it seems to look in /var/run:
./test.pl
DBI connect('dbname=mydb','user',...) failed: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"? at ./test.pl line 6
I tried to simply create a symlink, but that doesn't seem to be working:
sudo ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432
ln: failed to create symbolic link `/var/run/postgresql/.s.PGSQL.5432': No such file or directory
Some other simple stuff:
pg_hba.conf trusts all localhost connections as well as those of my subnet.
postgresql.conf has the following:
listen_addresses = '*'
See http://www.postgresql.org/docs/9.2/static/libpq-connect.html#LIBPQ-CONNECT-HOST:
host
Name of host to connect to. If this begins with a slash, it specifies
Unix-domain communication rather than TCP/IP communication; the value
is the name of the directory in which the socket file is stored. The
default behavior when host is not specified is to connect to a
Unix-domain socket in /tmp (or whatever socket directory was specified
when PostgreSQL was built). On machines without Unix-domain sockets,
the default is to connect to localhost.
So a connection string that has host=/tmp should make your Pg client look in the right place (which is supposed to be the default anyway).
I am having some problems with memcached and one idea I am having is that perhaps it is already running on the port I am trying to run it on, started by some other user on our network. Is there a way to tell what memcached ports are currently in use?
To see if it is running you could also try telnetting into the port:
telnet localhost 11211
If this works you will see the following (telling you that the given port is open):
Connected to localhost.
Escape character is '^]'.
Now if memcached IS running you can see some basic stats by issuing the given command:
stats
If this fails you will know that memcached is not running.
Try
netstat -ap | grep TheChosenPort#
and see if anything is listening on those TCP or UDP ports.
netstat
In Linux, check via netstat, e.g.
$ sudo netstat -nap | grep memcached
tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 5067/memcached
ps
Use ps to filter the processes:
$ ps wuax | grep memcache
497 5067 0.0 1.3 384824 53928 ? Ssl Apr11 1:28 memcached -d -p 11211 -u memcached -m 64 -c 1024 -P /var/run/memcached/memcached.pid -l 127.0.0.1
The port can be found next to -p, e.g. -p 11211. If port hasn't been specified, default is 11211.
Bash
You can send stats command to the given port and see if the memcached responds, e.g.
exec 3<>/dev/tcp/localhost/11211; printf "stats\nquit\n" >&3; cat <&3
Telnet
Use telnet to connect to the host and run stats (as above), e.g.
$ telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
stats
STAT pid 23669
STAT uptime 433859
Then hit Ctrl-] and Ctrl-D to finish.
Use the following command
ps -U user | grep -v grep | grep memcached
You can check memcached status
service memcached status
You will see a line like this at the bottom:
└─1560 /usr/bin/memcached -vv -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid
The -p 11211 is what port it's running on.
If you're asking this question, it sounds like you're running a really old version. If you did this on a recent version, you'd see this:
% ./memcached
failed to listen on TCP port 11211: Address already in use