How to get running nginx correct port on linux - shell

I searched Nginx running port with processes name but I get two port.
If i execute this
netstat -antup | grep nginx | awk '{print $4}' | rev | cut -d":" -f1 | rev
The result is as below.
88
88
But I want the result to be like this 88

Related

How to get nginx running exact port on linux

If i execute the below comment, The result will be like this 8060 0.0.0.0
netstat -antup | grep nginx |awk '{print $4 "\t" $5 }' | cut -d ":" -f2
But I want the result to be like this 8060
Not sure what the orignial response from the command is, but just cut the response again is one way
netstat -antup | grep nginx |awk '{print $4 "\t" $5 }' | cut -d ":" -f2 | cut -d " " -f1
Deconstructing the command you provided, netstat -antup prints these information:
$ netstat -antup
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:8060 0.0.0.0:* LISTEN XYZ/nginx
...
You want just the "8060", which is the port, nginx is listening to.
The next part was fine. grep nginx |awk '{print $4}' gives you the corresponding Local Address. You don't need the "\t" $5 part, as the Foreign Address is not relevant here.
The intermediary result should look like this:
$ netstat -antup | grep nginx | awk '{print $4}'
127.0.0.1:8060
Now the port ist the last part after a ":". Be aware, that IPv6 addresses also contain ":"s. So i'd suggest to cut the very last one instead of the 2nd.
You can do so, by reverting the output, cutting the first bit and reverting again:
$ netstat -antup | grep nginx | awk '{print $4}' | rev | cut -d":" -f1 | rev
8060
et voila. the desired output

How to find value of a key on tail -f

My log files are in key-value format. I want to find value of a particular key on tail -f ..
Suppose one of the line in log is:
ts=2016-12-23-18-31-34-849 | deviceType=LENOVO Lenovo A6000 | elapsed=11 | firstHomePage=null | installId=37797b61-0bb1-4c1a-844c-5904c7e83de8 | ip=157.48.104.146
ts=2016-12-23-18-31-34-849 | deviceType=LENOVO Lenovo A6000 | elapsed=15 | firstHomePage=null | installId=37797b61-0bb1-4c1a-844c-5904c7e83de8 | ip=157.48.104.146
I am not sure how do I pipe output of my tail -f so that output should be following
11
15
Use GNU grep with the --line-buffered command to buffer stdout as it arrives in case of continuously growing file. The -o flag for matching only the pattern and -P to enable perl style regEx captures.
tail -f file | grep --line-buffered -oP "elapsed=\K(\d+)"
11
15
From the man grep page,
--line-buffered
Use line buffering on output.
Try grep:
tail log_file | grep -o '\<elapsed=[^[:space:]]*' | cut -d= -f2
awk -F'[=|]' '{print $6}' file
11
15

Get Ports grep using netstat -v

I want to get a list of ports of the established connections using netstat -v grep.
I am trying this:
sudo netstat -v | grep "ESTABLISHED" | cut -d: -f5
Any help?
Try this with $5 and $4:
netstat -v | awk '/ESTABLISHED/ {split($5, array, ":"); print array[2]}'
Please try
netstat -v| grep "ESTABLISHED"| awk '{print $5}' | cut -d ":" -f2

Get mac address from gateway

I want to be able to get the mac address from the gateway with a bash script.
My idea was to get the gateway IP:
netstat -nr | grep default
However I get this:
default 192.168.1.1 UGSc 77 0 en0
I would somehow need to get rid of everything on the line and make it just read the IP so then I could then do the following command:
arp -n -i en0 $ip
If anyone could help me or think of a better way of doing it that would be great!
Please try
netstat -nr | grep default | awk '{print $1}'
If you do lots of bash scripting, you should probably get familiar with awk, which does this type of things (and is pretty mighty by the way).
Another answer that works on Mac OS X is:
route get default | grep '^ gateway:' | cut -f 6 -d " " | xargs arp | cut -f 4 -d " "

Efficient way to get your IP address in shell scripts

Context:
On *nix systems, one may get the IP address of the machine in a shell script this way:
ifconfig | grep 'inet' | grep -v '127.0.0.1' | cut -d: -f2 | awk '{print $1}'
Or this way too:
ifconfig | grep 'inet' | grep -v '127.0.0.1' | awk '{print $2}' | sed 's/addr://'
Question:
Would there be a more straightforward, still portable, way to get the IP address for use in a shell script?
(my apologies to *BSD and Solaris users as the above command may not work; I could not test)
you can do it with just one awk command. No need to use too many pipes.
$ ifconfig | awk -F':' '/inet addr/&&!/127.0.0.1/{split($2,_," ");print _[1]}'
you give direct interface thereby reducing one grep.
ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}'
Based on this you can use the following command
ip route get 8.8.8.8 | awk 'NR==1 {print $NF}'
Look here at the Beej's guide to networking to obtain the list of sockets using a simple C program to print out the IP addresses using getaddrinfo(...) call. This simple C Program can be used in part of the shell script to just print out the IP addresses available to stdout which would be easier to do then rely on the ifconfig if you want to remain portable as the output of ifconfig can vary.
Hope this helps,
Best regards,
Tom.
ifconfig | grep 'broadcast\|Bcast' | awk -F ' ' {'print $2'} | head -n 1 | sed -e 's/addr://g'
May be this could help.
more /etc/hosts | grep `hostname` | awk '{print $1}'
# for bash/linux
ipaddr(){
if="${1:-eth0}"
result=$(/sbin/ip -o -4 addr show dev "${if}" | sed 's/^.*inet // ; s/\/...*$//')
printf %s "${result}"
tty -s && printf "\n"
}

Resources