SSH to a remote server and find the specific pid and kill it
up vote
0
down vote
favorite
I want to remotely log into a server and then find the pid of the specific port. After that kill that pid.
I tried this,
ssh -T test@192.168.94.139 ; "netstat -lnpt ; awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}'"
I should do this just only using netstat command
linux shell
add a comment |
up vote
0
down vote
favorite
I want to remotely log into a server and then find the pid of the specific port. After that kill that pid.
I tried this,
ssh -T test@192.168.94.139 ; "netstat -lnpt ; awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}'"
I should do this just only using netstat command
linux shell
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to remotely log into a server and then find the pid of the specific port. After that kill that pid.
I tried this,
ssh -T test@192.168.94.139 ; "netstat -lnpt ; awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}'"
I should do this just only using netstat command
linux shell
I want to remotely log into a server and then find the pid of the specific port. After that kill that pid.
I tried this,
ssh -T test@192.168.94.139 ; "netstat -lnpt ; awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}'"
I should do this just only using netstat command
linux shell
linux shell
asked Nov 26 at 11:54
Janith
84
84
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
You miss the piping feature (| instead of ;) in your command. I propose this one:
ssh -t test@192.168.94.139 "sudo netstat -plnt | grep 2020 | awk '{print $7}' | sed s+/.*++g | sort -u | xargs kill"
I assume here you have sudo installed and permissions to use it on your server. My tests showed you may need it in order to get netstat to work properly with -p option.
I prefer here using grep+sed to avoid over-complicated (in my opinion) awk script.
-t: allows you to enter your user password to performsudocommand
grep 2020: filters the port number
awk '{print $7}': letsawkchoose the right field
sed s+/.*++g: removes the/and following characters (note: the+sign replaces the traditional/here for shortness)
sort -u: just in case you would have several processes working with the port (happens if both IPv4 and IPv6 are used)
xargs kill: callskillusing the standard input as a parameter
You may need to add sudo to the last one, if the process to kill is owned by another user: sudo xargs kill
As an example, you could also use variables (note here the semi-colons used to separate commands without piping) :
server=test@192.168.94.139 ; port=2020 ; ssh -t $server "sudo netstat -plnt | grep $port | awk '{print $7}' | sed s+/.*++g | sort -u | xargs kill"
sometimesxargs killworking and sometimes not. Then I putxargs -i kill -kill {}and It is working fine.
– Janith
Nov 27 at 4:30
kill -killwill forcibly kill the process (whilekillwill gently ask for the process to terminate, but it may not do it)
– lauhub
Nov 27 at 9:15
For debugging purposes, you can add the-toption to xargs (it will echo the command to be executed):xargs -t kill
– lauhub
Nov 27 at 9:15
add a comment |
up vote
0
down vote
You got close enough to your goal. There are some missing points though.
- No Semicolon after host IP required, the remote command should go right after the IP. So, remove the semicolon.
- Replace the other semicolon in front of awk with a pipe sign
|- that way the netstat output will go to the awk. - All the dollar sign needs to be escaped with a backslash. otherwise it is evaluated on the local machine
You will get the PID printed after above points done (if such is there). Then you will need to pass that PID to kill command. I'd suggest you use xargs kill as it looks the simplest here. PID will be accepted by kill command that way.
So the final version may look like:
ssh -T test@192.168.94.139 "netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}') | xargs kill"
Not the best look, but i suits your approach.
Thank you for your support. I got errors.sudo ssh -T zissa@192.168.94.139 "sudo netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}'"awk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7}awk: cmd. line:1: ^ syntax errorawk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7}awk: cmd. line:1: ^ 1 is invalid as number of arguments for sub
– Janith
Nov 27 at 6:45
netstat output may vary, so the column 7 did not look as expected. if you are still solving ti, i'd suggest you to look at the netstat output to figure out how the awk needs to be adjusted for the line of interest to extract pid
– Tagwint
Nov 27 at 10:57
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You miss the piping feature (| instead of ;) in your command. I propose this one:
ssh -t test@192.168.94.139 "sudo netstat -plnt | grep 2020 | awk '{print $7}' | sed s+/.*++g | sort -u | xargs kill"
I assume here you have sudo installed and permissions to use it on your server. My tests showed you may need it in order to get netstat to work properly with -p option.
I prefer here using grep+sed to avoid over-complicated (in my opinion) awk script.
-t: allows you to enter your user password to performsudocommand
grep 2020: filters the port number
awk '{print $7}': letsawkchoose the right field
sed s+/.*++g: removes the/and following characters (note: the+sign replaces the traditional/here for shortness)
sort -u: just in case you would have several processes working with the port (happens if both IPv4 and IPv6 are used)
xargs kill: callskillusing the standard input as a parameter
You may need to add sudo to the last one, if the process to kill is owned by another user: sudo xargs kill
As an example, you could also use variables (note here the semi-colons used to separate commands without piping) :
server=test@192.168.94.139 ; port=2020 ; ssh -t $server "sudo netstat -plnt | grep $port | awk '{print $7}' | sed s+/.*++g | sort -u | xargs kill"
sometimesxargs killworking and sometimes not. Then I putxargs -i kill -kill {}and It is working fine.
– Janith
Nov 27 at 4:30
kill -killwill forcibly kill the process (whilekillwill gently ask for the process to terminate, but it may not do it)
– lauhub
Nov 27 at 9:15
For debugging purposes, you can add the-toption to xargs (it will echo the command to be executed):xargs -t kill
– lauhub
Nov 27 at 9:15
add a comment |
up vote
0
down vote
accepted
You miss the piping feature (| instead of ;) in your command. I propose this one:
ssh -t test@192.168.94.139 "sudo netstat -plnt | grep 2020 | awk '{print $7}' | sed s+/.*++g | sort -u | xargs kill"
I assume here you have sudo installed and permissions to use it on your server. My tests showed you may need it in order to get netstat to work properly with -p option.
I prefer here using grep+sed to avoid over-complicated (in my opinion) awk script.
-t: allows you to enter your user password to performsudocommand
grep 2020: filters the port number
awk '{print $7}': letsawkchoose the right field
sed s+/.*++g: removes the/and following characters (note: the+sign replaces the traditional/here for shortness)
sort -u: just in case you would have several processes working with the port (happens if both IPv4 and IPv6 are used)
xargs kill: callskillusing the standard input as a parameter
You may need to add sudo to the last one, if the process to kill is owned by another user: sudo xargs kill
As an example, you could also use variables (note here the semi-colons used to separate commands without piping) :
server=test@192.168.94.139 ; port=2020 ; ssh -t $server "sudo netstat -plnt | grep $port | awk '{print $7}' | sed s+/.*++g | sort -u | xargs kill"
sometimesxargs killworking and sometimes not. Then I putxargs -i kill -kill {}and It is working fine.
– Janith
Nov 27 at 4:30
kill -killwill forcibly kill the process (whilekillwill gently ask for the process to terminate, but it may not do it)
– lauhub
Nov 27 at 9:15
For debugging purposes, you can add the-toption to xargs (it will echo the command to be executed):xargs -t kill
– lauhub
Nov 27 at 9:15
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You miss the piping feature (| instead of ;) in your command. I propose this one:
ssh -t test@192.168.94.139 "sudo netstat -plnt | grep 2020 | awk '{print $7}' | sed s+/.*++g | sort -u | xargs kill"
I assume here you have sudo installed and permissions to use it on your server. My tests showed you may need it in order to get netstat to work properly with -p option.
I prefer here using grep+sed to avoid over-complicated (in my opinion) awk script.
-t: allows you to enter your user password to performsudocommand
grep 2020: filters the port number
awk '{print $7}': letsawkchoose the right field
sed s+/.*++g: removes the/and following characters (note: the+sign replaces the traditional/here for shortness)
sort -u: just in case you would have several processes working with the port (happens if both IPv4 and IPv6 are used)
xargs kill: callskillusing the standard input as a parameter
You may need to add sudo to the last one, if the process to kill is owned by another user: sudo xargs kill
As an example, you could also use variables (note here the semi-colons used to separate commands without piping) :
server=test@192.168.94.139 ; port=2020 ; ssh -t $server "sudo netstat -plnt | grep $port | awk '{print $7}' | sed s+/.*++g | sort -u | xargs kill"
You miss the piping feature (| instead of ;) in your command. I propose this one:
ssh -t test@192.168.94.139 "sudo netstat -plnt | grep 2020 | awk '{print $7}' | sed s+/.*++g | sort -u | xargs kill"
I assume here you have sudo installed and permissions to use it on your server. My tests showed you may need it in order to get netstat to work properly with -p option.
I prefer here using grep+sed to avoid over-complicated (in my opinion) awk script.
-t: allows you to enter your user password to performsudocommand
grep 2020: filters the port number
awk '{print $7}': letsawkchoose the right field
sed s+/.*++g: removes the/and following characters (note: the+sign replaces the traditional/here for shortness)
sort -u: just in case you would have several processes working with the port (happens if both IPv4 and IPv6 are used)
xargs kill: callskillusing the standard input as a parameter
You may need to add sudo to the last one, if the process to kill is owned by another user: sudo xargs kill
As an example, you could also use variables (note here the semi-colons used to separate commands without piping) :
server=test@192.168.94.139 ; port=2020 ; ssh -t $server "sudo netstat -plnt | grep $port | awk '{print $7}' | sed s+/.*++g | sort -u | xargs kill"
answered Nov 26 at 13:52
lauhub
430616
430616
sometimesxargs killworking and sometimes not. Then I putxargs -i kill -kill {}and It is working fine.
– Janith
Nov 27 at 4:30
kill -killwill forcibly kill the process (whilekillwill gently ask for the process to terminate, but it may not do it)
– lauhub
Nov 27 at 9:15
For debugging purposes, you can add the-toption to xargs (it will echo the command to be executed):xargs -t kill
– lauhub
Nov 27 at 9:15
add a comment |
sometimesxargs killworking and sometimes not. Then I putxargs -i kill -kill {}and It is working fine.
– Janith
Nov 27 at 4:30
kill -killwill forcibly kill the process (whilekillwill gently ask for the process to terminate, but it may not do it)
– lauhub
Nov 27 at 9:15
For debugging purposes, you can add the-toption to xargs (it will echo the command to be executed):xargs -t kill
– lauhub
Nov 27 at 9:15
sometimes
xargs kill working and sometimes not. Then I put xargs -i kill -kill {} and It is working fine.– Janith
Nov 27 at 4:30
sometimes
xargs kill working and sometimes not. Then I put xargs -i kill -kill {} and It is working fine.– Janith
Nov 27 at 4:30
kill -kill will forcibly kill the process (while kill will gently ask for the process to terminate, but it may not do it)– lauhub
Nov 27 at 9:15
kill -kill will forcibly kill the process (while kill will gently ask for the process to terminate, but it may not do it)– lauhub
Nov 27 at 9:15
For debugging purposes, you can add the
-t option to xargs (it will echo the command to be executed): xargs -t kill– lauhub
Nov 27 at 9:15
For debugging purposes, you can add the
-t option to xargs (it will echo the command to be executed): xargs -t kill– lauhub
Nov 27 at 9:15
add a comment |
up vote
0
down vote
You got close enough to your goal. There are some missing points though.
- No Semicolon after host IP required, the remote command should go right after the IP. So, remove the semicolon.
- Replace the other semicolon in front of awk with a pipe sign
|- that way the netstat output will go to the awk. - All the dollar sign needs to be escaped with a backslash. otherwise it is evaluated on the local machine
You will get the PID printed after above points done (if such is there). Then you will need to pass that PID to kill command. I'd suggest you use xargs kill as it looks the simplest here. PID will be accepted by kill command that way.
So the final version may look like:
ssh -T test@192.168.94.139 "netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}') | xargs kill"
Not the best look, but i suits your approach.
Thank you for your support. I got errors.sudo ssh -T zissa@192.168.94.139 "sudo netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}'"awk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7}awk: cmd. line:1: ^ syntax errorawk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7}awk: cmd. line:1: ^ 1 is invalid as number of arguments for sub
– Janith
Nov 27 at 6:45
netstat output may vary, so the column 7 did not look as expected. if you are still solving ti, i'd suggest you to look at the netstat output to figure out how the awk needs to be adjusted for the line of interest to extract pid
– Tagwint
Nov 27 at 10:57
add a comment |
up vote
0
down vote
You got close enough to your goal. There are some missing points though.
- No Semicolon after host IP required, the remote command should go right after the IP. So, remove the semicolon.
- Replace the other semicolon in front of awk with a pipe sign
|- that way the netstat output will go to the awk. - All the dollar sign needs to be escaped with a backslash. otherwise it is evaluated on the local machine
You will get the PID printed after above points done (if such is there). Then you will need to pass that PID to kill command. I'd suggest you use xargs kill as it looks the simplest here. PID will be accepted by kill command that way.
So the final version may look like:
ssh -T test@192.168.94.139 "netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}') | xargs kill"
Not the best look, but i suits your approach.
Thank you for your support. I got errors.sudo ssh -T zissa@192.168.94.139 "sudo netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}'"awk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7}awk: cmd. line:1: ^ syntax errorawk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7}awk: cmd. line:1: ^ 1 is invalid as number of arguments for sub
– Janith
Nov 27 at 6:45
netstat output may vary, so the column 7 did not look as expected. if you are still solving ti, i'd suggest you to look at the netstat output to figure out how the awk needs to be adjusted for the line of interest to extract pid
– Tagwint
Nov 27 at 10:57
add a comment |
up vote
0
down vote
up vote
0
down vote
You got close enough to your goal. There are some missing points though.
- No Semicolon after host IP required, the remote command should go right after the IP. So, remove the semicolon.
- Replace the other semicolon in front of awk with a pipe sign
|- that way the netstat output will go to the awk. - All the dollar sign needs to be escaped with a backslash. otherwise it is evaluated on the local machine
You will get the PID printed after above points done (if such is there). Then you will need to pass that PID to kill command. I'd suggest you use xargs kill as it looks the simplest here. PID will be accepted by kill command that way.
So the final version may look like:
ssh -T test@192.168.94.139 "netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}') | xargs kill"
Not the best look, but i suits your approach.
You got close enough to your goal. There are some missing points though.
- No Semicolon after host IP required, the remote command should go right after the IP. So, remove the semicolon.
- Replace the other semicolon in front of awk with a pipe sign
|- that way the netstat output will go to the awk. - All the dollar sign needs to be escaped with a backslash. otherwise it is evaluated on the local machine
You will get the PID printed after above points done (if such is there). Then you will need to pass that PID to kill command. I'd suggest you use xargs kill as it looks the simplest here. PID will be accepted by kill command that way.
So the final version may look like:
ssh -T test@192.168.94.139 "netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}') | xargs kill"
Not the best look, but i suits your approach.
answered Nov 26 at 13:33
Tagwint
1,4481714
1,4481714
Thank you for your support. I got errors.sudo ssh -T zissa@192.168.94.139 "sudo netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}'"awk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7}awk: cmd. line:1: ^ syntax errorawk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7}awk: cmd. line:1: ^ 1 is invalid as number of arguments for sub
– Janith
Nov 27 at 6:45
netstat output may vary, so the column 7 did not look as expected. if you are still solving ti, i'd suggest you to look at the netstat output to figure out how the awk needs to be adjusted for the line of interest to extract pid
– Tagwint
Nov 27 at 10:57
add a comment |
Thank you for your support. I got errors.sudo ssh -T zissa@192.168.94.139 "sudo netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}'"awk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7}awk: cmd. line:1: ^ syntax errorawk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7}awk: cmd. line:1: ^ 1 is invalid as number of arguments for sub
– Janith
Nov 27 at 6:45
netstat output may vary, so the column 7 did not look as expected. if you are still solving ti, i'd suggest you to look at the netstat output to figure out how the awk needs to be adjusted for the line of interest to extract pid
– Tagwint
Nov 27 at 10:57
Thank you for your support. I got errors.
sudo ssh -T zissa@192.168.94.139 "sudo netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}'" awk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7} awk: cmd. line:1: ^ syntax error awk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7} awk: cmd. line:1: ^ 1 is invalid as number of arguments for sub– Janith
Nov 27 at 6:45
Thank you for your support. I got errors.
sudo ssh -T zissa@192.168.94.139 "sudo netstat -lnpt | awk '$4 ~ /:2020$/ {sub(//.*/, "", $7); print $7}'" awk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7} awk: cmd. line:1: ^ syntax error awk: cmd. line:1: $4 ~ /:2020$/ {sub(//.*/, , $7); print $7} awk: cmd. line:1: ^ 1 is invalid as number of arguments for sub– Janith
Nov 27 at 6:45
netstat output may vary, so the column 7 did not look as expected. if you are still solving ti, i'd suggest you to look at the netstat output to figure out how the awk needs to be adjusted for the line of interest to extract pid
– Tagwint
Nov 27 at 10:57
netstat output may vary, so the column 7 did not look as expected. if you are still solving ti, i'd suggest you to look at the netstat output to figure out how the awk needs to be adjusted for the line of interest to extract pid
– Tagwint
Nov 27 at 10:57
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484184%2fssh-to-a-remote-server-and-find-the-specific-pid-and-kill-it%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown