We help IT Professionals succeed at work.

Help with bash script to get continual input

Tech_20
Tech_20 asked
on
118 Views
Last Modified: 2017-03-23
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
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2015

Commented:
#!/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
CERTIFIED EXPERT
Most Valuable Expert 2015

Commented:
Though unless you're configured to use multiple DNS servers, the answer back should always be the same.
CERTIFIED EXPERT
Most Valuable Expert 2015

Commented:
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

Author

Commented:
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?
CERTIFIED EXPERT
Most Valuable Expert 2015
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
That works! Thanks, Jan!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions