Ananth's Blog - page 12

Posts

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 set dns address in android phones?

Ever wondered how setup dns address in android phones. You can do it if your phone is rooted.
You may have a terminal installed with busybox if your phone is rooted. Otherwise install a terminal and gain root permission by typing

su

Then list all the device dns entries using this command

getprop | grep dns

Then set the dns entries by the command

setprop net.rmnet0.dns1  8.8.8.8

Here rmnet0.dns1 is given as an example. This may vary depending on which dns entry you have to choose. 8.8.8.8 is the googles dns server address. Replace it with your choice.

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...