Avatar of Tech_20
Tech_20
 asked on

Help with bash script to get continual input

I need a script to take input in the form of a server name, perform the nslookup command, output only the actual IP server address (not the DNS server) and print the output. I have part of the script but need help on the rest. Thanks.

______

#!/usr/bin/bash

echo “Enter a ServerName.\n”
read serverName
nsLook = $(echo nslookup $serverName | egrep -o “Non*" | # this is the part where I need help with getting the last IP address
print nsLook

echo “Do you have another?\n”
case:
    yes)
        next;;    # this is the part where I need help
    no)
        break;;
esac
* BashDNS* nslookup

Avatar of undefined
Last Comment
Tech_20

8/22/2022 - Mon
Jan Bacher

#!/bin/bash

function get_nameservers() {
      read -p "Enter a Server Name: " serverName
      nsLook=`nslookup $serverName`
      server_addy=`echo $nsLook | cut -d " " -f2`
      echo $server_addy
      get_nameservers
}

get_nameservers
Jan Bacher

Though unless you're configured to use multiple DNS servers, the answer back should always be the same.
Jan Bacher

I misread your question and have changed one of the lines:


#!/bin/bash

function get_nameservers() {
      read -p "Enter a Server Name: " serverName
      nsLook=`nslookup $serverName`
      server_addy=`echo $nsLook | cut -d ":" -f6`
      echo $server_addy
      get_nameservers
}

get_nameservers
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Tech_20

ASKER
Hello Jan,

Thanks for the quick response. This extracted only the nameserver at first. Then after trying it again (and double-checking the terminal session to confirm), it began to work. That buggy issue may have to do with the version of Terminal (OS X El Capitan) I am using but it's currently working.

I have another question regarding an event where there is no server found. Instead of a lengthy error in the form of all of the names of the files in the working directory, is there a possible if/then statement to use in that code block to return "N/A" if the variable $server_addy has no value?
ASKER CERTIFIED SOLUTION
Jan Bacher

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Tech_20

ASKER
That works! Thanks, Jan!