Address already in use but nothing in netstat or lsof - python

I try to start the Python SimpleHTTPServer on port 7054 :
$ sudo python -m SimpleHTTPServer 7054
...
socket.error: [Errno 98] Address already in use
So, I ran the following commands :
$ sudo netstat -ntpu | grep 7054
$ sudo lsof -i -n -P | grep 7054
But I have no results.

From the netstat manpage:
netstat [address_family_options] [--tcp|-t] [--udp|-u] [--raw|-w] [--listening|-l] [--all|-a] [--numeric|-n] [--numeric-hosts] [--numeric-ports]
[--numeric-users] [--symbolic|-N] [--extend|-e[--extend|-e]] [--timers|-o] [--program|-p] [--verbose|-v] [--continuous|-c]
I use the following options:
sudo netstat -tanl | grep 7054
Which is --numeric, --tcp, --all, --listening
I think the minimal netstat options you need to show the pid of the process listening on a particular port are -nlp.
The lsof options you specify work for me. Using the example code at https://wiki.python.org/moin/UdpCommunication#Receiving and python -m SimpleHTTPServer 7054:
$ netstat -nlp | grep 7054
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 0.0.0.0:7054 0.0.0.0:* LISTEN 20458/python
udp 0 0 0.0.0.0:7054 0.0.0.0:* 20498/python
$ lsof -i -n -P | grep 7054
python 20458 michael 3u IPv4 143736 0t0 TCP *:7054 (LISTEN)
python 20498 michael 3u IPv4 173739 0t0 UDP *:7054
Extra credit: stick it in an alias:
listening() {
netstat -nlp | grep $1
}
And use it:
$ listening 7054

An address can be in use, but not shown by lsof, ss or netstat once bind has been used on a SOCK_STREAM socket, but before the named socket has been set the LISTEN state.
This was found with a test performed using AlmaLinux 8.6 with a 4.18.0-372.19.1.el8_6.x86_64 Kernel.
The source for the test program is in bind_local.c
Start the test program, specifying an IPv6 link-local address and port number (10000) to bind to:
[mr_halfword#haswell-alma ibv_message_passing]$ ibv_message_passing_c_project/bin/debug/bind_local/bind_local -6 fe80::207:43ff:fe15:2298%4 -p 10000 -l
fd 3 bound to fe80::207:43ff:fe15:2298 scope-id 4 port 10000
Press enter to listen on port
At the above port a SOCK_STREAM socket has been created, bind called and getsockname used to get the socket name which is displayed (i.e. the address the socket has been bound to).
The socket file-descriptor the test program has bound is shown as socket 398999:
[mr_halfword#haswell-alma ~]$ ls -l /proc/`pgrep bind_local`/fd
total 0
lrwx------. 1 mr_halfword mr_halfword 64 Sep 10 17:08 0 -> /dev/pts/0
lrwx------. 1 mr_halfword mr_halfword 64 Sep 10 17:08 1 -> /dev/pts/0
lrwx------. 1 mr_halfword mr_halfword 64 Sep 10 17:08 2 -> /dev/pts/0
lrwx------. 1 mr_halfword mr_halfword 64 Sep 10 17:08 3 -> 'socket:[398999]'
In this state attempting to use nc to listen on port 10000 fails with Address already in use, but neither lsof not ss show the address:
[mr_halfword#haswell-alma ~]$ nc -l 10000
Ncat: bind to :::10000: Address already in use. QUITTING.
[mr_halfword#haswell-alma ~]$ sudo lsof -i -n -P | grep 10000
[mr_halfword#haswell-alma ~]$ sudo ss -nlp | grep 10000
[mr_halfword#haswell-alma ~]$
Cause the test program to call listen on the bound socket, by pressing return:
Press return to exit
Now that the bound socket is in the LISTEN state attempting to use nc to listen on port 10000 fails with Address already in use, but now lsof and ss are showing the address and which program is using the address:
[mr_halfword#haswell-alma ~]$ sudo lsof -i -n -P | grep 10000
bind_loca 16929 mr_halfword 3u IPv6 398999 0t0 TCP [fe80::207:43ff:fe15:2298]:10000 (LISTEN)
[mr_halfword#haswell-alma ~]$ sudo ss -nlp | grep 10000
tcp LISTEN 0 1 [fe80::207:43ff:fe15:2298]%enp1s0f4d1:10000 [::]:* users:(("bind_local",pid=16929,fd=3))
I haven't yet tried looking at the Linux Kernel source code to determine if a SOCK_STREAM socket which has been bound to an address by being named, but left in that state, has any user space method which can locate the program using the address.
The reason the above was investigating how the iwpmd iWARP Port Mapper Daemon was claiming TCP ports, for which was unable to find a way to list the claimed TCP ports.

Related

Finding on which port a given server is running within python program

I am developing a python 3.11 program which will run on a few different servers and needs to connect to the local Redis server. On each machine the latter might run on a different port, sometimes the default 6379 but not always.
On the commandline I can issue the following command which on both my Linux and MacOS servers works well:
(base) bob#Roberts-Mac-mini ~ % sudo lsof -n -i -P | grep LISTEN | grep IPv4 | grep redis
redis-ser 60014 bob 8u IPv4 0x84cd01f56bf0ee21 0t0 TCP *:9001 (LISTEN)
What's the better way to get the running port using python functions/libraries?
What if you run your commands within a py script using the os library:
import os
cmd = 'ls -l' <-- change the command you want to run
os.system(cmd)
or else you could also use subprocess library as well:
import subprocess
print(subprocess.check_output(['ls', '-l']))

How to fix 8000 port is already in use django runserver problem? [duplicate]

Restarting the Django server displays the following error:
this port is already running....
This problem occurs specifically on Ubuntu and not other operating systems. How can I free up the port to restart the server?
A more simple solution just type sudo fuser -k 8000/tcp.
This should kill all the processes associated with port 8000.
EDIT:
For osx users you can use sudo lsof -t -i tcp:8000 | xargs kill -9
netstat -ntlp
It will show something like this.
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:8000 0.0.0.0:* LISTEN 6599/python
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN -
tcp 0 0 192.168.124.1:53 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN -
tcp6 0 0 :::3306 :::* LISTEN
So now just close the port in which Django/python running already by killing the process associated with it.
kill -9 PID
in my case
kill -9 6599
Now run your Django app.
ps aux | grep -i manage
after that you will see all process
ubuntu#ip-10-154-22-113:~/django-apps/projectname$ ps aux | grep -i manage
ubuntu 3439 0.0 2.3 40228 14064 pts/0 T 06:47 0:00 python manage.py runserver project name
ubuntu 3440 1.4 9.7 200996 59324 pts/0 Tl 06:47 2:52 /usr/bin/python manage.py runserver project name
ubuntu 4581 0.0 0.1 7988 892 pts/0 S+ 10:02 0:00 grep --color=auto -i manage
kill -9 process id
e.d kill -9 3440
`enter code here`after that :
python manage.py runserver project name
By default, the runserver command starts the development server on the internal IP at port 8000.
If you want to change the server’s port, pass it as a command-line argument. For instance, this command starts the server on port 8080:
python manage.py runserver 8080
lsof -t -i tcp:8000 | xargs kill -9
Sorry for comment in an old post but It may help people
Just type this on your terminal
killall -9 python3
It will kill all python3 running on your machine and it will free your all port. Greatly help me when to work in Django project.
We don't use this command { sudo lsof -t -i tcp:8000 | xargs kill -9 } Because it's close all tabs...You should use to
ps -ef | grep python
kill -9 process_id
ps -ef | grep python (show all process with id)
kill -9 11633
(11633 is a process id to :- /bin/python manage.py runserver)
>> ps aux | grep manage
ubuntu 3438 127.0.0 2.3 40256 14064 pts/0 T 06:47 0:00 python manage.py runserver
>> kill -9 3438
Type 'fg' as command after that Ctrl-C.
Command:
Fg will show which is running on background. After that Ctrl-C will stop it.
fg
ctl-c
In terminal, Type ps aux | grep runserver
Hit Enter
Use PID among the result execute kill -9 <PID>
For an instance, If result of step 1 is as follow
root 1041 0.0 0.1 266912 34580 pts/3 S+ 11:31 0:01 python3 manage.py runserver 0.0.0.0:3030
root 1696 4.5 0.1 126128 40708 ? S Feb14 925:43 /usr/local/bin/python manage.py runserver 0.0.0.0:8000
1041 and 1696 are PIDs. We need to choose whichever process we want to kill among them.
This is an expansion on Mounir's answer. I've added a bash script that covers this for you. Just run ./scripts/runserver.sh instead of ./manage.py runserver and it'll work exactly the same way.
#!/bin/bash
pid=$(ps aux | grep "./manage.py runserver" | grep -v grep | head -1 | xargs | cut -f2 -d" ")
if [[ -n "$pid" ]]; then
kill $pid
fi
fuser -k 8000/tcp
./manage.py runserver
For me, this happens because my API request in Postman is being intercepted by a debugger breakpoint in my app... leaving the request hanging. If I cancel the request in Postman before killing my app's server, the error does not happen in the first place.
--> So try cancelling any open requests you are making in other programs.
On macOS, I have been using sudo lsof -t -i tcp:8000 | xargs kill -9 when I forget to cancel the open http request in order to solve error = That port is already in use. This also, complete closes my Postman app, which is why my first solution is better.
Click the arrow in the screenshot and find the bash with already running Django server. You were getting the message because your server was already running and you tried to start the server again.
Dont use CTRL + Z to stop server, use CTRL + C to stop the server, I had also had the same problem in my linux (fedora) , I used to stop the server using CTRL + Z and again I used to kill the server using sudo fuser -k 8000/tcp command, which worked fine. But later when I started using CTRL + C , I didnot get that port running issue anymore.
if you have face this problem in mac you just need to open activity monitor and force quite python then try again
In case You are using the VSC's screen terminal, The error might be due to the fact that you already runserver in some other shell.
Just click on the dropbox on the left of the + sign in the header of the terminal of VSC and select some other shell and check if the server is already running there. Quit that server and you are ready to launch a another server.
I was trying all the solutions but they were not working i suggest you to keep press the power button or if your battery is removeable then remove it all the process will be killed and your local host will be reset

Execute telnet command for Port check by SSH to remote hosts and get the output to a file

I have a text file like below with host, site and port to telnet
Hostname site port
appwlsqa02.comp.xxx.com fgh-fst13-scan.comp.xxx.com 1521
appwlsqa03.comp.xxx.com fgh-fst23-scan.comp.xxx.com 1521
cappwlsqa01.comp.xxx.com fgh-fst13-scan.comp.xxx.com 1521
My goal is to ssh each hostname and execute telnet command
for example for the first row in the file
ssh appwlsqa02.comp.xxx.com
then,
telnet fgh-fst13-scan.comp.xxx.com 1521
I have tried below:
while read HOST site port ; do ssh $HOST "echo exit | telnet $site $port " < /dev/null; done < text.txt`
I have to read hostnames as one variable $HOST and host to check as $site and port as$port
Update:
Here is the script i have developed based on sugegstions:
# /bin/bash
while read HOST site port ;
do
echo $HOST $site $port
ssh -n $HOST "telnet $site $port | grep "Connected" " >> t.txt &
PID=$!
sleep 2
kill "$PID"
if [ -s t.txt ]
then
echo from $HOST to $site $port is open >> succ.txt
else
echo from $HOST to $site $port is closed >> fail.txt
fi
rm t.txt
done < text.txt
When trying to use telnet, you need to run something like expect. Telnet grabs the terminal io. Expect replaces the terminal with a pseudo terminal and allows you to script the input and output.

Why the python-tcpdum command can't capture packet in a file

I have the following TcpDump command written in Python but it doesn't give me any output file with the requested packets although I have TcpDump installed and tested on my Ubuntu VM :
command = 'sudo /usr/sbin/tcpdump -i eth1 {} -c {} -s 0 -w {}'\
.format( 'tcp host 10.0.2.15','30000',
'/home/results/xyz.pcap')
cat test.py
import os
command = '/usr/sbin/tcpdump -i eth1 {} -c {} -s 0 -w {}'.format( 'host 192.168.254.74','30000','res.pcap')
print(command)
os.system(command)
sudo python test.py
/usr/sbin/tcpdump -i eth1 host 192.168.1.10 -c 30000 -s 0 -w res.pcap
tcpdump: listening on eth1, link-type EN10MB (Ethernet), capture size 10 bytes
^C0 packets captured
6 packets received by filter
0 packets dropped by kernel
ls -l | grep test
-rw------- 1 admin admin 155 Dec 2 23:05 test.py
Seems to work just fine for me.
The test file is 'test.py'. I run it under sudo and exit after some time. I can see that 6 packets were captured and the file size is > 0.
Make sure the command itself runs properly outside of python.

port conflict between Hadoop and python

I am installing Hadoop 2.5.0 on a Ubuntu 12.04 cluster, 64-bit. At the end of the instructions I type $ jps on the master node and do not get a NameNode. I checked the Hadoop logs and found:
BindException error stating :9000 is already in use.
$ netstat -a -t --numeric-ports -p | grep :9000 returns that python is listening on this port. It appears I need to move python 2.7 to another port. How do I move python?
Followed the command below, the pid=2346.
$ ps -p 2346
PID TTY TIME CMD
2346 ? 01:28:13 python
Tried second command:
$ ps -lp 2346
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 0 2346 1 0 80 0 - 332027 poll_s ? 01:28:30 python
more detail:
$ ps -Cp 2346
PID TTY STAT TIME COMMAND
2346 ? Ssl 88:34 /usr/lib/cmf/agent/build/env/bin/python /usr/lib/cmf/agent/src/cmf/agent.py --package_dir /usr/lib/cmf
It appears a failed Cloudera Hadoop distribution installation has not been removed. It installed python 2.7 automatically. Not sure what else is automatically running. Will attempt to uninstall python 2.7.
To be clear a program written in python is using port 9000 and not python2.7 itself.
You need to track down this program and then work out how to config it to listen on a different port. You could use this command to get the full details of the process listening on port 9000
netstat -a -t --numeric-ports -p | grep :9000 | awk '{print $7}' | sed -e 's/\/.*//' | xargs echo ps -lp
It appears Cloudera installed python 2.7. This was removed / replace with python 3.2.
The $jps command on Hadoop now returns the expected results including NameNode.

Categories