netstat: parse (awk?) output to return "Program name" not "PID/Program name" - bash

UBUNTU 14.04
netstat -p outputs both "PID/Program name" in the same column. I just want "Program name" in that column. What's the easiest way to do this?
Actual Output
root#neo4j1:~# netstat -tlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 *:ssh *:* LISTEN 1020/sshd
tcp6 0 0 [::]:ssh [::]:* LISTEN 1020/sshd
tcp6 0 0 [::]:7473 [::]:* LISTEN 31380/java
tcp6 0 0 [::]:7474 [::]:* LISTEN 31380/java
Desired Output
root#neo4j1:~# netstat -tlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State Program name
tcp 0 0 *:ssh *:* LISTEN sshd
tcp6 0 0 [::]:ssh [::]:* LISTEN sshd
tcp6 0 0 [::]:7473 [::]:* LISTEN java
tcp6 0 0 [::]:7474 [::]:* LISTEN java

Try
netstat -tlp | sed 's,[0-9]\+/,,'

Using grouping with Sed:
netstat -tlp | sed 's/\(^.*\)\( [0-9]*\/\)\(.*$\)/\1\3/g'
Though RTLinuxSW's answer is much cleaner.

Related

What is CSlistener and why is it running on port 9000 on my mac and restarts itself every time I stop it

Context:
Woke up and couldn't start my app that uses port 9000 because address was already in use. Message on my terminal is
Error response from daemon: Ports are not available: listen tcp 0.0.0.0:9000: bind: address already in use
The command netstat -anvp tcp | awk 'NR<3 || /LISTEN/' lists a process running on port 9000 like;
Active Internet connections (including servers)
Proto Recv-Q Send-Q Local Address Foreign Address (state) rhiwat shiwat pid epid state options
tcp4 0 0 127.0.0.1.9000 *.* LISTEN 131072 131072 1260 0 0x0080 0x00000006
tcp4 0 0 127.0.0.1.3306 *.* LISTEN 131072 131072 997 0 0x0100 0x00000006
tcp4 0 0 127.0.0.1.33060 *.* LISTEN 131072 131072 997 0 0x0000 0x00000006
there are more processes listed, i just trimmed it. I can see stuff like 127.0.0.1.3306 which i think is mysql, thats fine, but the process running at 127.0.0.1.9000 is what am not sure about.
Killing the process with sudo kill 1260, the ID of the process is 1260 and running the command netstat -anvp tcp | awk 'NR<3 || /LISTEN/' lists the process again with a different process ID.
Running sudo lsof -i TCP:9000 prints
php-fpm 2997 root 9u IPv4 0xf6c73412e60c41cb 0t0 TCP localhost:cslistener (LISTEN)
php-fpm 3000 _www 10u IPv4 0xf6c73412e60c41cb 0t0 TCP localhost:cslistener (LISTEN)
php-fpm 3001 _www 10u IPv4 0xf6c73412e60c41cb 0t0 TCP localhost:cslistener (LISTEN)
So after seeing this printed I googled cslistener, notice that there is a string TCP localhost:cslistener in what's printed.
One find suggested looking for it in the file /etc/services and sure I found it, the lines looked like
...
cslistener 9000/udp # CSlistener
cslistener 9000/tcp # CSlistener
...
So, my question is, what is cslistener and why is it running on port 9000 and restarting itself everytime i kill it.

golang http server http.ListenAndServe only works for localhost?

I tied to implement an HTTP server in Azure Linux VM using golang. Below is the simple golang server code, listening on port 30175. And there is no firewall on that port.
package main
import (
"fmt"
"log"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}
func main() {
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":30175", nil))
}
The result of sudo netstat -tlnp is:
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 1605/vsftpd
tcp 0 0 127.0.0.1:3350 0.0.0.0:* LISTEN 1873/xrdp-sesman
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1697/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1379/cupsd
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 4879/8
tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 15507/9
tcp 0 0 0.0.0.0:3389 0.0.0.0:* LISTEN 1859/xrdp
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 2112/python
tcp6 0 0 :::22 :::* LISTEN 1697/sshd
tcp6 0 0 ::1:631 :::* LISTEN 1379/cupsd
tcp6 0 0 ::1:6010 :::* LISTEN 4879/8
tcp6 0 0 ::1:6011 :::* LISTEN 15507/9
tcp6 0 0 :::30175 :::* LISTEN 46595/HttpHandler
I can get response only in localhost, but no response from remote server:
curl localhost:30175
Hi there, I love !
curl serveripaddress:30175
not working
Your problem is that your server is listening on tcp6 stack. Try to explicitly use tcp with “0.0.0.0:6789” instead of just port “:6789”
This has nothing to do with your code. It is a typical firewall issue.
By default (on *nix platform) all the incoming traffic is blocked and all outgoing is allowed. You need to open the ports on your OS to allow incoming traffic to hit your servers. try installing ufw utility and run sudo ufw allow 30175
From the question, it seems your server is using tcp6. Ideally, it should not cause the problem because tcp6 is supposed to support both IPV4 as well as IPV6. But I would recommend you to downgrade it to tcp, if that makes sense.
You are trying to output to console. Did you try to send it as a response?
func handler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf("Hi there, I love %s!", r.URL.Path[1:])));
}
No if you don't specify the host part of the address the server will listen on every available unicast address and every available anycast address of the system. So I would guess a problem in name resolution or routing.
This is due to Linux listen rules.
There is a reject all rule on my rules.
# listen rules
sudo iptables -L INPUT --line-numbers
sudo iptables -D INPUT 8

What's the underlying transport layer protocol when etcd node sends out a heartbeat

Should it be UDP or TCP? If it's the latter one, wouldn't it cost too much overhead on network via three-time shaking and four-time waving?
$ sudo netstat -nap | grep etcd
[sudo] password for emma:
tcp 0 0 127.0.0.1:7001 0.0.0.0:* LISTEN 21731/etcd
tcp 0 0 127.0.0.1:4001 0.0.0.0:* LISTEN 21731/etcd
tcp 0 0 127.0.0.1:2379 0.0.0.0:* LISTEN 21731/etcd
tcp 0 0 127.0.0.1:2380 0.0.0.0:* LISTEN 21731/etcd

unable to stream to crtmpserver, Error sending data: Broken pipe

Using Gstreamer latest version
gst-launch-1.0 version 1.8.1
GStreamer 1.8.1
command :
gst-launch-1.0 -m -v --gst-debug-level=2 autovideosrc ! video/x-raw,width=640,height=480 ! videoconvert ! tcpclientsink host="hidden_ip" port=1935
to stream to crtmpserver ( on ubuntu AWS ec2 ) which has following open , publicly accessible and working ports
sudo netstat -putan|grep crtmp
tcp 0 0 0.0.0.0:9999 0.0.0.0:* LISTEN 7235/crtmpserver
tcp 0 0 0.0.0.0:1935 0.0.0.0:* LISTEN 7235/crtmpserver
tcp 0 0 0.0.0.0:9999 0.0.0.0:* LISTEN 11734/crtmpserver
tcp 0 0 0.0.0.0:1935 0.0.0.0:* LISTEN 11734/crtmpserver
tcp 0 0 0.0.0.0:9999 0.0.0.0:* LISTEN 11689/crtmpserver
tcp 0 0 0.0.0.0:1935 0.0.0.0:* LISTEN 11689/crtmpserver
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 7235/crtmpserver
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 11734/crtmpserver
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 11689/crtmpserver
tcp 0 0 0.0.0.0:6665 0.0.0.0:* LISTEN 7235/crtmpserver
tcp 0 0 0.0.0.0:6665 0.0.0.0:* LISTEN 11734/crtmpserver
tcp 0 0 0.0.0.0:6665 0.0.0.0:* LISTEN 11689/crtmpserver
tcp 0 0 0.0.0.0:6666 0.0.0.0:* LISTEN 7235/crtmpserver
tcp 0 0 0.0.0.0:6666 0.0.0.0:* LISTEN 11734/crtmpserver
tcp 0 0 0.0.0.0:6666 0.0.0.0:* LISTEN 11689/crtmpserver
Error :
ERROR: from element /GstPipeline:pipeline0/GstTCPClientSink:tcpclientsink0: Error while sending data to "hidden_ip:1935".
Additional debug info:
gsttcpclientsink.c(217): gst_tcp_client_sink_render (): /GstPipeline:pipeline0/GstTCPClientSink:tcpclientsink0:
Only 236024 of 614400 bytes written: Error sending data: Broken pipe
Execution ended after 0:00:01.308461393
complete error trace
http://pastebin.com/aEFYb5q6
All help much appreciated ,
Thanks in anticipation
update :
After some workarounds on iptable and gst plugins
Im am finally getting seeking query error . Any ideas on how to solve this will be very welcomed
gst script from ubuntu 15.10
gst-launch-1.0 -m -v --gst-debug-level=2 v4l2src ! video/x-h264,width=320,height=240,framerate=10/1 ! h264parse ! flvmux ! rtmpsink location ='rtmp://<hidden_ip>/live'
traces from gst
flvmux gstflvmux.c:1238:gst_flv_mux_write_header:<flvmux0> downstream did not handle seeking query
............
flvmux gstflvmux.c:1246:gst_flv_mux_write_header:<flvmux0> downstream is not seekable, but streamable=false. Will ignore that and create streamable output instead
........
gstbasesrc.c(2948): gst_base_src_loop (): /GstPipeline:pipeline0/GstV4l2Src:v4l2src0:
streaming task paused, reason not-negotiated (-4)
Execution ended after 0:00:00.213998674

Problem connecting a linux server and windows client with sockets

Hello all
I am trying to learn more about sockets and how to use them and I have been stuck on an issue for a while now.
I started with Beej's guide to network programming and I made the talker and listener from this page on the linux (Fedora 14) machine I am working on. It works and I can get it to talk to each other.
Then I went on to Windows (7) and worked with this tutorial and got that up and running and can send messages to myself on the windows machine. The only change I really have is that I am using getHostbyAddr and not by name.
It is when I glue the two together that I start to get issues, or rather one issue for now. I am running listener from Beej on the linux machine and I try to have the client from Johnnie send it a message. I get a winsock error 10061 on the windows machine and nothing ever shows up on the linux (not surprisingly). I have tested this with the firewall on the linux and I still get that error.
What can I do to fix this?
Thank you
Edited to add some more info:
When I run tcpdump I get this
[root#localhost ~]# tcpdump tcp port 4950
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
12:08:56.246753 IP TLARGE.WIFI.schoolname.EDU.62394 > hmd46.cs.schoolname.edu.sybasesrvmon: Flags [S], seq 150153995, win 8192, options [mss 1460,nop,wscale 2,nop,nop,sackOK], length 0
12:08:56.246794 IP hmd46.cs.schoolname.edu.sybasesrvmon > TLARGE.WIFI.schoolname.EDU.62394: Flags [R.], seq 0, ack 150153996, win 0, length 0
12:08:56.746170 IP TLARGE.WIFI.schoolname.EDU.62394 > hmd46.cs.schoolname.edu.sybasesrvmon: Flags [S], seq 150153995, win 8192, options [mss 1460,nop,wscale 2,nop,nop,sackOK], length 0
12:08:56.746221 IP hmd46.cs.schoolname.edu.sybasesrvmon > TLARGE.WIFI.schoolname.EDU.62394: Flags [R.], seq 0, ack 1, win 0, length 0
12:08:57.246138 IP TLARGE.WIFI.schoolname.EDU.62394 > hmd46.cs.schoolname.edu.sybasesrvmon: Flags [S], seq 150153995, win 8192, options [mss 1460,nop,nop,sackOK], length 0
12:08:57.246185 IP hmd46.cs.schoolname.edu.sybasesrvmon > TONJELARGE.WIFI.schoolname.EDU.62394: Flags [R.], seq 0, ack 1, win 0, length 0
^C
6 packets captured
6 packets received by filter
0 packets dropped by kernel
Running netstat gives me this:
[root#localhost ~]# netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:58661 0.0.0.0:* LISTEN 1083/rpc.statd
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1013/rpcbind
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1265/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1148/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1554/sendmail: acce
tcp 0 0 :::56315 :::* LISTEN 1083/rpc.statd
tcp 0 0 :::111 :::* LISTEN 1013/rpcbind
tcp 0 0 :::22 :::* LISTEN 1265/sshd
tcp 0 0 ::1:631 :::* LISTEN 1148/cupsd
Both of these were from the linux machine
Error 10061 means WSAECONNREFUSED. In the link you posted I see the client is using port 80. Are you sure you modified it to 4950 ?
Something is definitely getting through to the server, otherwise it wouldn't send you the "I don't like you" RST (that's what connection refused means: not only does it refuse your connection, to add insult to injury it's telling you).
EDIT 1
From the netstat output it seems nobody is listening on 4950.
EDIT 2
I finally brought myself to read that beej stuff (to be honest I always considered his tutorials the worst). Didn't this set off any alarm ? You're creating a udp socket, that's why nobody is listening in TCP 4950, that's why you can't connect.
hints.ai_socktype = SOCK_DGRAM;
You have two options:
Use a UDP socket in the windows side
Change the code on the server side to use TCP.
The server code as it stands isn't suitable for TCP (recvfrom and all that stuff) but should be easily adapted).

Resources