Posts tagged with linux - page 3

Linux command awk extract anything

Awk is pretty use full when you are writing automatic scripts to do your jobs. Some simple examples of how we can use awk are given below

ps | awk '{print $1}'

It will print first column of ps command.  If you want to print second column also write

ps | awk '{print $1 $2}'

Output seems to be pretty crowded? Add a simple space by

ps | awk '{print $1 " " $2}'

Want to limit it display only second line?

ps | awk ' NR==2{print $1 " " $2}'

Want to exclede first row? You can do it by

ps |awk 'NR !=1{print  $1 " " $2}'

Similarly you can print rows greater than a value by

ps |awk 'NR >1{print  $1 " " $2}'

You can extract virtually anything using awk... Feel free to explore every options. Type man awk or awk --help to explore other options. Feel free to comment also... :)

This site has some good examlpes.


Pass output of one command to argument of other

Well Linux is famous for combining things in power full ways in which anything is possible from a command line. The real fun of Linux came when you start focusing on terminal to do everything...

It's possible to pipeline commands in terminal. Also it's possible to pass output of something as an argument of other.

For example you can ping to your own external ip by

ping $(curl -s  curlmyip.com)

curl curlmyip.com returns you ip address. This address is passed to  the telnet as an argument. Whatever you put between $( and ) will execute as if it is a bash command.


How to know external IP address from Linux terminal?

ifconfig displays only local ip address of your system. If your system is directly connected to internet then it will be your external ip address. It may not be the case always. Because you will be behind a router or a modem.
You can know the external IP address by going to www.whatismyip.com or www.ipecho.net. If you want to know it from a terminal type

curl curlmyip.com

What curl do is it fetches the content of the link to the terminal. There are also several other websites that offers this same service. Some of them are listed below.

curl ipecho.net/plain

curl myexternalip.com/raw

curl curlmyip.com


curl ifconfig.me/ip


curl icanhazip.com


curl ip.appspot.com



How to see open ports inLinux?

Type this netstat command in terminal.

netstat -tulpn

Or you can use this one.

netstat -l

Try netstat --help to go through other options also...

Testing NAT Port Forwarding and reverese NAT

For a long time I have been trying to configure port forwarding using NAT. But every attempt was futile...  Finally I found that my port forwarding works just fine. But it just appears to be not working because my router doesn't support reverse NAT. In this post I will show you guys how to test whether you configure your NAT correctly or not. (Read a detailed write up by shane. Link)

First make a port forwarding rule in your router say 1600. Download this little tool called simple internet tools. Install it and open port listener and select your internal IP(192.168.x.x). Then type your port number and press start listening.

Go to this site and check whether it says port is opened. If your connection is successful you will notice it in the port listener application.

Then go to your browser and type your internet ip address. (You can find it from www.whatismyip.com) . Tnen type your port number after a colon. (116.211.22.12:1600). Your browser shows a webpage not available window. Check your port listener. If you find a connection successful message it means your router supports reverse NAT. Otherwise reverse NAT is not available.

You can also check it by an nc command like this.

nc 116.211.22.12 1600

If you didn't want to test the connection using the above site you can do it by nc from a remote ssh account. Login to your ssh account and try to connect it with nc.