Dig Command Examples

Simple Dig Command

[root@server ~]# dig justgeek.net

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-9.P2.el7 <<>> justgeek.net
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 53092
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;justgeek.net.                  IN      A

;; ANSWER SECTION:
justgeek.net.           267     IN      A       104.28.12.34
justgeek.net.           267     IN      A       104.28.13.34
justgeek.net.           267     IN      A       172.67.130.28

;; Query time: 10 msec
;; SERVER: 108.61.10.10#53(108.61.10.10)
;; WHEN: Wed May 27 07:13:17 UTC 2020
;; MSG SIZE  rcvd: 89

How to check A record of the website using DIG command

[root@server ~]# dig redhat.com +noall +answer

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-9.P2.el7 <<>> redhat.com +noall +answer
;; global options: +cmd
redhat.com. 3547 IN A 209.132.183.105

Check MX record using dig command

[root@server ~]# dig redhat.com MX +noall +answer

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-9.P2.el7 <<>> redhat.com MX +noall +answer
;; global options: +cmd
redhat.com. 600 IN MX 10 us-smtp-inbound-2.mimecast.com.
redhat.com. 600 IN MX 10 us-smtp-inbound-1.mimecast.com.
[root@server ~]#

How to check NS record of the website using DIG command.

[root@server ~]# dig justgeek.net NS +noall +answer

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-9.P2.el7 <<>> justgeek.net NS +noall +answer
;; global options: +cmd
justgeek.net. 86400 IN NS kia.ns.cloudflare.com.
justgeek.net. 86400 IN NS jerome.ns.cloudflare.com.
[root@server ~]#

About nslookup

So here we wil learn about NSLOOKUP (Name Server Lookup) and we will see some of it’s examples.

So we will start with, What is nslookup

nslookup is the tool which lets you the enter the host-name or domain name and see it’s corresponding IP address. It also lets you to pass IP address and get corresponding hostname. (reverse DNS)

Installation, if your CentOS machine says nslookup command not found.

[root@server /]# nslookup google.com
-bash: nslookup: command not found

so here you will have to Install bind-utils to get nslookup command. Simply run yum install bind-utils to take advantage of nslookup command.

[root@server /]# yum install bind-utils

nslookup command examples.

nslookup command followed by domain/hostname will display IP address of the domain.

[root@server /]# nslookup unix.com
Server:         108.61.10.10
Address:        108.61.10.10#53
 
Non-authoritative answer:
Name:   unix.com
Address: 209.126.104.117

You can use -type option to get various dns details. For example to get MX records.

[root@server /]# nslookup -type=mx redhat.com
Server:         108.61.10.10
Address:        108.61.10.10#53

Non-authoritative answer:
redhat.com      mail exchanger = 10 us-smtp-inbound-2.mimecast.com.
redhat.com      mail exchanger = 10 us-smtp-inbound-1.mimecast.com.

To get nameserver of the domain.

[root@server /]# nslookup -type=ns google.com
Server:         108.61.10.10
Address:        108.61.10.10#53

Non-authoritative answer:
google.com      nameserver = ns3.google.com.
google.com      nameserver = ns2.google.com.
google.com      nameserver = ns1.google.com.
google.com      nameserver = ns4.google.com.

Reverse DNS

[root@server /]#  nslookup 209.132.183.181
181.183.132.209.in-addr.arpa    name = origin-www2.redhat.com.

Authoritative answers can be found from:

To get Information using specific DNS server.

[root@server /]# nslookup redhat.com ns1.redhat.com
Server:         ns1.redhat.com
Address:        209.132.186.218#53

Name:   redhat.com
Address: 209.132.183.105

we have used the ns1.redhat.com as the DNS server. Here you may notice that, we don’t get any “Non-authoritative answer:” header, since ns1.redhat.com has all the zone information of redhat.com

Shell Script to reboot multiple servers.

Here is simple bash script where you can add the server names in the command line and then it will go on rebooting every server which you have listed.

Script Usage:-

[root@server /]# ./server-reboot.sh appserver1 sqlserver1 dataserver1

Above command will reboot appserver1 sqlserver1 and dataserver1, if you have not setup ssh keys it will ask for password.

To use this script create a file called server-reboot.sh and following contents.

#!/bin/bash
for server in "${@}"
do
{
scriptName=$0
scriptPath=$(dirname $0)
script=$scriptPath/boot.txt
command=`base64 -w0 $script`
ssh -t $server "echo $command | base64 -d | sudo bash"
}
done

Create a another file named boot.txt in the same location where you have created server-reboot.sh.

for ((i=03; i>0; i--)); do sleep 1 ; printf "\rREBOOTING SERVER in $i Second. PRESS CTRL + C to cancel " ; done
shutdown -rf now

Save both the files, give execute permissions to server-reboot.sh and start rebooting.

It’s just not reboot script but you can add any commands which you want to run on multiple Linux servers in the file boot.txt

There are many other ways to do the same task but this works best for my environment. Add in comments what works best for you.

Cut Command examples in Linux.

What is “cut” command in Linux?

“Cut” Command is a utility for cutting sections from each line of the Files. It can be used to cut parts of a line by byte position, character and delimiter. It can also be used to cut data from file formats like CSV.

Examples of Cut Command.
You can specify byte position using the option -b, it will show only that letter, I am using the letter JustGeek here.
If you want to display only first letter i.e. J

[root@server ]# echo "JustGeek" | cut -b 1
J

If you want to display the letter J,S,G i.e. 1,3 and 5th Letter then you can specify using “,”

[root@server ]# echo "JustGeek" | cut -b 1,3,5
JsG
[root@server ]#

Cut based on a delimiter:-
To cut using a delimiter use the -d option. This is normally used in conjunction with the -f option to specify the field that should be cut.
Create a file called names.csv with content below

Jeffery,Smith,45,London
Anthony,Evans,33,California
Charles,Jones,28,Paris

The delimiter can be set to a comma with -d ‘,’. cut can then pull out the fields you want with the -f flag. Example

[root@server ]# cat names.csv | cut -d ',' -f 1
Jeffery
Anthony
Charles
[root@server ]#

You can comma “,” separate for multiple entries. Example.

[root@server ]# cat names.csv | cut -d ',' -f 1,2
Jeffery,Smith
Anthony,Evans
Charles,Jones

If you want display age then you can just specify position with -f. Example

[root@server ]# cat names.csv | cut -d ',' -f 3
45
33
28

And if you want to Display City, then:

[root@server ]# cat names.csv | cut -d ',' -f 4
London
California
Paris

If you want to Change Delimeter in display then you can use the option –output-delimiter as shown in example below.

[root@server ]# cat names.csv | cut -d ',' -f 1,2 --output-delimiter='---'
Jeffery---Smith
Anthony---Evans
Charles---Jones
[root@server ]#

How to Check “cut” command version

 [root@server ]# cut --version
 cut (GNU coreutils) 8.22
 Copyright (C) 2013 Free Software Foundation, Inc.
 License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html.
 This is free software: you are free to change and redistribute it.
 There is NO WARRANTY, to the extent permitted by law.
 Written by David M. Ihnat, David MacKenzie, and Jim Meyering.