Latest Blogs

Bash Script Expo #2

Bash Script Expo #2 Screenshot

This little bit here prints out the process ID and process name of the process with the specified open port.
#!/bin/bash
ps -p `netstat -pantu | awk "\\$4 ~ /$1$/ { split(\\$NF,pid,\"/\"); print pid[1]; }"` -o comm= -o pid= 2> /dev/null


`netstat -pantu` prints the open ports using the UDP or TCP protocol and the process IDs of the processes that opened the ports. The awk line parses netstat so that it prints only the process ID. Then the ps line prints out the process name and PID. The ending 2> /dev/null stops ps from printing out its help text when there is no process with the specified open port.

The warning that appears above explains that one must `sudo` to see all the processes.

However, if you'd like to just know the PID of the process with the open port, remove the ps, like so:

#!/bin/bash
netstat -pantu | awk "\\$4 ~ /$1$/ { split(\\$NF,pid,\"/\"); print pid[1]; }"


Then you could do this:
Bash Script Expo #2 Screenshot 2
Which will kill the process that opened the port. (When I ran that command, I ended up crashing Steam...)

Comments

Re: Bash Script Expo #2

Evening dresses
With more 1000 Designer dresses,we supply Evening Dresses,Custom Dresses,formal gowns,cocktail dresses with wholesale price
prom dresses
Dresses, evening, cocktail, prom dresses, formal gowns from dresseslife. Homecoming dresses and bridesmaid
Graduation Dresses
Looking For discount Balenciaga handbags? The store online sells the Balenciaga bags.
Welcome to visit and buy Balenciaga handbags. we offer Balenciaga Handbags,Balenciaga
Balenciaga Handbags
Balenciaga
Balenciaga sale

Re: Bash Script Expo #2

well, your tutorial teaches me a lot, so thanks for sharing it with us