I'm looking for a cross-platform way to check if a port is in use so I can print a warning message.
I have accomplished this in macOS:
if lsof -Pi :8080 -sTCP:LISTEN -t >/dev/null; then
echo "Port 8080 is already in use"
fi
But this bash script needs to also work on Windows machines. I was looking at something like netstat -aon | find "8080", but I'm unsure how to use that in my script (don't have a Windows machine).
Is there an efficient way to do this? If it helps, the Windows machines have WSL available to them (but lsof doesn't appear to work yet).
While I'm certain the other answers would work in certain situations, I just wanted to note I ended up going with the following solution for my own work:
if echo PING | nc localhost 8080 >/dev/null; then
echo "Port 8080 in use"
fi
Im not sure how your network looks like but maybe nmap will be a solution for you. You can scan ports for every pc in your local network using only linux.
If port is open, that mean is in use. This solution will work for any system.
apt-get install nmap
for example like this:
if nmap -p 8080 <ip address> | grep -wq "open"
then
echo "Port 8080 is already in use"
fi
Use netstat (works on windows, mac, wsl2)
Example netstat -a | grep :8080 where 8080 is the port you are interested in.
If you are on wsl1 you will need to use the windows netstat as the one included in wsl is broken. Simply set an alias to the windows netstat before using it alias netstat='/mnt/c/Windows/System32/netstat.exe'
Related
I need to find if there is any application running on port 9000 on my mac.
sudo lsof -i :9000
-i will list the applications using the mentioned port
In order to find if there is any process running on port 9000, use following command in terminal>> netstat -vanp tcp | grep 9000
I have two laptops that each has connected to the Internet by landline (two different lines/modem). One is Mac and the other is Windows. I want to send a message from Windows to the Mac.
I installed NetCat on Mac using, brew install netcat, based on this page.
I installed NetCat on Windows based on this page.
I ran following command in Mac terminal in order to see what is my IP address: $ ipconfig getifaddr en0 it printed: 169.254.136.39
In Mac terminal: $ nc -u -l -p 4444. Seems it is ready for incomming messages.
In Windows terminal: $ nc -u 169.254.136.39 4444. I get a new line and I assume connection has established. However, when I enter a message and hit Enter, nothing happens :(
When I replace above command with nc -u -v -n 169.254.136.39 4444 in Windows and nc -u -l -n -p 4444 in Mac, then I get: (UNKNOWN) [169.254.136.39] 4444 (?) open
I have no idea what is wrong? I confirm Firewal is off on my Mac.
How can I edit meta.db in influxdb? I am getting 8086: bind: address already in use.
This ip is only available in meta.db which I found by influxd config on terminal.
You can change port from 8086 to something else in /etc/influxdb/influxdb.conf if its already in use. Or you can kill the service that is using port 8086.
you can check with, sudo lsof -n -i :8086 | grep LISTEN who is using port 8086
when I run this in terminal:
lsof -n -i4TCP:8081
I get this
node 10901 me 28u IPv6 0xbcad49 0t0 TCP *:sunproxyadmin (LISTEN)
foo 11957 me 15u IPv4 0xbcad49 0t0 TCP 127.0.0.1:61127->127.0.0.1:sunproxyadmin (CLOSE_WAIT)
What is this sunproxyadmin?
Per http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml?search=8081, TCP port 8081 is the well known port for sunproxyadmin the same way 80 is the well known port for http. In this case, you have a node process that is listening on port 8081, but lsof is trying to be helpful and show the well known port for this. Under linux, this is defined in /etc/services; I would expect OS X is similar.
Edit 1: Note that per Apple Man Pages, passing -P
inhibits the conversion of port numbers to port names for network files.
Inhibiting the conversion may make lsof run a little faster. It
is also useful when port name lookup is not working properly.
This should cause lsof to not print out the confusing sunproxyadmin for something that just happens to use the port that Sun registered.
Edit 2: The second column in your response (e.g. 10901 in the first row, which is the one you want, and 11957 in the second row) should be the process ID. If you do ps aux | grep 10901 (or ps elf | grep [pid], as I can't remember which works right for OSX and don't have it handy) you should get something like:
apache 19783 0.0 0.2 251888 8580 ? S Oct07 0:00
/usr/sbin/httpd -DFOREGROUND
(or to make something up:
nodeuser 10901 0.0 0.2 251888 8580 ? S Oct07 0:00 node index.js
)
You can kill it with kill -9 10901 (or whatever the PID was) though you might find it comes back if it's running as a service or what.
This is useful enough to add to your bash profile:
function findbyport()
{
sudo lsof -P -iTCP:$1 -sTCP:LISTEN
}
Kill it, do in your terminal
sudo lsof -i :8081
from there get the PID number and then run
kill -9 <PID NUMBER>
You can check on FB documentation for more info
If you don't want to kill sunproxyadmin process, let try to start React native in different port with command:
react-native start --port your_port
Then open Dev settings (see how to open dev menu), and modify Debug server host & port for device to: your_local_ip:your_port
There is this MACAFEE antivirus running on my Mac. I am able to kill it(Even though I should not be killing it, I tried it, and looks like it never dies! Sudo has no power after all!).So after a lot of research I have tried this one.
Step 1 : Get the process' PID
sudo lsof -n -i4TCP:8081
Step 2 : Find the launchd endpoint
sudo launchctl list | grep
Step 3 : Remove mcafee
sudo launchctl remove com.mcafee.agent.macmn
If this one works for you pls say thanks to me and as well as https://fantashit.com/unable-to-perform-react-native-start/
Im trying to automate a docker run shell script that needs to spin up an X11 app running on a docker-machine VM with virtualbox driver on OSX/mac. To get the x11 app to forward the X11 display to the desktop host i need to get the ip address of the correct vboxnet* (i assume) network interface (not the docker ip) and export that ip into the docker container.
what i am currently doing is assuming that the docker-machine VM ip is tied to the "vboxnet1" interface name and regexp'ing it out of ifconfig.
i see that the virtualbox DHCP process that spins up when i start docker-machine has a trunk name that corresponds to the interface i want - but again this seems like an assumption and i would think there is some command i could run to be certain which address it is so my docker run script will work on any machine.
/Applications/VirtualBox.app/Contents/MacOS/VBoxNetDHCP --ip-address
192.168.99.2 --lower-ip 192.168.99.100 --mac-address XXXXXXXXXXX
--netmask 255.255.255.0 --network HostInterfaceNetworking-vboxnet1
--trunk-name vboxnet1 --trunk-type netadp --upper-ip 192.168.99.254
Is there a way to definitively determine which network interface to find the ip address?
This bash script seems to work on OSX machines:
#!/bin/bash
CONFIG=$( ifconfig | grep -A 2 'vboxnet1')
regex="([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})"
if [[ $CONFIG =~ $regex ]]; then
DISPLAY=${BASH_REMATCH[1]}
DISPLAY+=":0"
fi
ifconfig $(route -n get $(docker-machine ip default) | grep interface: | awk '{print $2}') | grep inet | awk '{print $2}'