# Netstat AND rmsock

**URL:** <https://community.unix.com/t/netstat-and-rmsock/379515>\
**Category:** AIX\
**Created:** [August 7, 2020, 4:40pm UTC](https://community.unix.com/t/netstat-and-rmsock/379515 "2020-08-07T16:40:34Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![big123456](https://community.unix.com/letter_avatar/big123456/32/6_22734026db12803ebb3e059622ac3534.png) [@big123456](https://community.unix.com/u/big123456)\
**Post date:** [August 7, 2020, 4:40pm UTC](https://community.unix.com/t/netstat-and-rmsock/379515/1 "2020-08-07T16:40:34Z")

</div>

Hi,  
Is there a way to execute these two commandes on one line?:

```nohighlight
netstat -Aan | grep LISTEN |awk '{ print $1}'

```

And give the output as parameter to rmsock tcpcb

Something like:

```nohighlight
netstat -Aan | grep LISTEN |awk '{ print $1}' | rmsock tcpcb

```

Thanks.

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [August 7, 2020, 5:15pm UTC](https://community.unix.com/t/netstat-and-rmsock/379515/2 "2020-08-07T17:15:11Z")

</div>

> [@big123456](#):
>
> `netstat -Aan | grep LISTEN |awk '{ print $1}' | rmsock tcpcb`

I don't know the specifics of `rmsock` and/or if the order of parameters/switches matter - you'll have to adjust

`rmsock tcpcb "$(netstat -Aan | awk '/LISTEN/{ print $1}' )"`

---

<div class="post-metadata">

**Author:** ![big123456](https://community.unix.com/letter_avatar/big123456/32/6_22734026db12803ebb3e059622ac3534.png) [@big123456](https://community.unix.com/u/big123456)\
**Post date:** [August 10, 2020, 3:19pm UTC](https://community.unix.com/t/netstat-and-rmsock/379515/3 "2020-08-10T15:19:29Z")

</div>

> [@vgersh99](#):
>
> rmsock tcpcb "$(netstat -Aan | awk '/LISTEN/{ print $1}' )"

Thanks.  
I received:  
usage: rmsock Addr TypeOfAddr

Regards.

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [August 10, 2020, 3:28pm UTC](https://community.unix.com/t/netstat-and-rmsock/379515/4 "2020-08-10T15:28:43Z")

</div>

given the usage message you got, how do you think you need to adjust the command?

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_22734026db12803ebb3e059622ac3534.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [August 10, 2020, 5:41pm UTC](https://community.unix.com/t/netstat-and-rmsock/379515/5 "2020-08-10T17:41:57Z")

</div>

In your other case, you can try the long shell command in the given URL.  
The following is an attempt to shorten the command:

```nohighlight
netstat -Aan | grep -w LISTEN | while read sockaddr protocol x x port x; do set -- $(rmsock "$sockaddr" tcpcb | grep socket); test -n "$9" && ps -ef | grep -w "$9"; echo "$port"; done

```

If it does not work, then turn debug mode on with

```nohighlight
set -x

```

and reply with the result.

---

<div class="post-metadata">

**Author:** ![big123456](https://community.unix.com/letter_avatar/big123456/32/6_22734026db12803ebb3e059622ac3534.png) [@big123456](https://community.unix.com/u/big123456)\
**Post date:** [August 11, 2020, 9:20am UTC](https://community.unix.com/t/netstat-and-rmsock/379515/6 "2020-08-11T09:20:38Z")

</div>

> [@vgersh99](#):
>
> rmsock tcpcb "$(netstat -Aan | awk '/LISTEN/{ print $1}' )"

may be:

```plaintext
rmsock "$(netstat -Aan | awk '/LISTEN/{ print $1}' )" tcpcb
The socket 0xf1000e00143a2808 is being held by proccess 10747994 (sshd).

```

Then it returns only one row. It escapes the others.  
Regards.

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [August 11, 2020, 12:34pm UTC](https://community.unix.com/t/netstat-and-rmsock/379515/7 "2020-08-11T12:34:55Z")

</div>

> [@big123456](#):
>
> Then it returns only one row. It escapes the others.

maybe (if rmsoxk doesn'take multiple addresses?)

```plaintext
while addr in "$(netstat -Aan | awk '/LISTEN/{ print $1}' )"
do
   rmsock "${addr}" tcpcb
done

```

---

<div class="post-metadata">

**Author:** ![big123456](https://community.unix.com/letter_avatar/big123456/32/6_22734026db12803ebb3e059622ac3534.png) [@big123456](https://community.unix.com/u/big123456)\
**Post date:** [August 12, 2020, 6:23am UTC](https://community.unix.com/t/netstat-and-rmsock/379515/8 "2020-08-12T06:23:41Z")

</div>

> [@vgersh99](#):
>
> `rmsock "${addr}" tcpcb`

Thank you.  
while addr in "$(netstat -Aan | awk '/LISTEN/{ print $1}' )"

> do  
> rmsock "${addr}" tcpcb  
> done  
> ksh: addr: not found.

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [August 12, 2020, 9:06am UTC](https://community.unix.com/t/netstat-and-rmsock/379515/9 "2020-08-12T09:06:42Z")

</div>

Sorry, not `while` but ` for`

---

<div class="post-metadata">

**Author:** ![big123456](https://community.unix.com/letter_avatar/big123456/32/6_22734026db12803ebb3e059622ac3534.png) [@big123456](https://community.unix.com/u/big123456)\
**Post date:** [August 12, 2020, 1:00pm UTC](https://community.unix.com/t/netstat-and-rmsock/379515/10 "2020-08-12T13:00:45Z")

</div>

> [@vgersh99](#):
>
> ```plaintext
> addr in "$(netstat -Aan | awk '/LISTEN/{ print $1}' )"
> do
> rmsock "${addr}" tcpcb
> done
> 
> ```

Thanks.  
for addr in "$(netstat -Aan | awk '/LISTEN/{ print $1}' )"

> do  
> rmsock "${addr}" tcpcb  
> done  
> The socket 0xf1000e00143a2808 is being held by proccess 10747994 (sshd).

Again just one row.  
Regards.

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [August 12, 2020, 1:04pm UTC](https://community.unix.com/t/netstat-and-rmsock/379515/11 "2020-08-12T13:04:17Z")

</div>

then `netstat -Aan | awk '/LISTEN/{ print $1}'` return just ONE value.  
add `set -x` to the top of your script to enable debugging.

Sorry, - remove `"` around `$(netstat -Aan | awk '/LISTEN/{ print $1}' )`  
My bad....

---

<div class="post-metadata">

**Author:** ![big123456](https://community.unix.com/letter_avatar/big123456/32/6_22734026db12803ebb3e059622ac3534.png) [@big123456](https://community.unix.com/u/big123456)\
**Post date:** [August 12, 2020, 2:19pm UTC](https://community.unix.com/t/netstat-and-rmsock/379515/12 "2020-08-12T14:19:37Z")

</div>

Thank yuo.

> for addr in $(netstat -Aan | awk '/LISTEN/{ print $1}' )  
> do  
> rmsock "${addr}" tcpcb  
> done

Worked fine.

Regards.  
PS for webmaster.  
This black background is HORRIBLE.

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [August 18, 2020, 8:16am UTC](https://community.unix.com/t/netstat-and-rmsock/379515/13 "2020-08-18T08:16:30Z")

</div>

Split this topic on August 18, 2020, 8:16am UTC

2 posts were split to a new topic: [Thoughts on the Default Dark Theme for the Site](https://community.unix.com/t/thoughts-on-the-default-dark-theme-for-the-site/379647)
