IP Address LookUp Bash Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting IP Address LookUp Bash Script
# 1  
Old 08-16-2014
IP Address LookUp Bash Script

I am new to bash scripting. I want write a script that reads from the first argument file and run nslookup, then prints out each nslookup. Something like below:

File name = ip
Code:
8.8.8.8
8.8.4.4

Bash shell script: nslookup.sh
Code:
#!/bin/bash
for i in $1
  do
      nslookup $i
  done

I run the command ./nslookup.sh ip, but doesn't work.

Please let me know what I am doing wrong.

Thank you in advance.

Last edited by rbatte1; 08-16-2014 at 05:19 PM.. Reason: Added CODE and ICODE tags
# 2  
Old 08-16-2014
Please use code tags as required by forum rules!

You can't do it like that. $1 will hold the string "ip" if script is called as indicated, and the for loop will execute exactly one single time, $i holding "ip", resulting in nslookup ip which will fail, (almost) certainly.

Try instead in your script:
Code:
while read IP
  do nslookup $IP
  done < $1

# 3  
Old 08-16-2014
Thank you for the reply. I will try that will while loop.

If I were to use for loop script, what do you have to read the content of "ip" file?

-BB
# 4  
Old 08-16-2014
If you invoke your script as:
Code:
./nslookup.sh ip

$1 inside your script will expand to ip. If you replace the code in your script with the code RudiC suggested, the redirection on the last line will cause the read to grab one IP address from the file ip each time through the loop until all lines have been processed.
# 5  
Old 08-17-2014
Basically, I know how to call a file within a loop script. However, that can be cumbersome if you have multiple IP address groups (or other information) in multiple files. I am trying to avoid modifying the shell script every time I have to lookup different files. I would like to learn if I can do this by giving it an argument , so I can avoid modifying the script each time.

Is there a way to do this by giving it an argument when you run the script?
# 6  
Old 08-17-2014
I repeat, if you put the code RudiC gave you in your script, and invoke it with:
Code:
./nslookup.sh ip

your script will lookup all of the IP addresses listed in the file named ip.
If you invoke it with:
Code:
./nslookup.sh SomeOtherFile

your script will lookup all of the IP addresses listed in the file named SomeOtherFile.

Please explain how this is different from what you have said you want to do???
# 7  
Old 08-17-2014
My apologies. I thought the "ip" portion was a file name. It works just as you said. Thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to write a value to a physical memory address in bash script?

How would I write a value to a physical memory address? I was able to read a physical memory address (for example, 0x400) using this line: dd if=/dev/mem count=4 bs=1 skip=$(( 0x400 )) But I get an error: dd: 'standard input': cannot skip to specified offset when I try to write using... (1 Reply)
Discussion started by: rabrandt
1 Replies

2. Shell Programming and Scripting

Array V-Lookup using UNIX bash

Hey everyone, I am trying to extract column values from a column in a tab-delimited text file and overlay them in a 2nd tab-delimited text file using a V-lookup type script in Unix bash. These are the 1st few rows of the 1st input file IN1: rsid chromosome position allele1 ... (10 Replies)
Discussion started by: Geneanalyst
10 Replies

3. Shell Programming and Scripting

Bash script who maps IP with MAC address

Hy every body, Unfortunately and without success, i want to write a bash script who maps a known IP addess to a known MAC address using iptables and for the FORWARD chain. Within the DHCP server, i have assigned a fixed IP address to all clients based on their MAC addresses of their... (11 Replies)
Discussion started by: hermouche
11 Replies

4. Shell Programming and Scripting

Bash lookup matching digits for secong file

In the bash below the user selects the file to be used. The digits of each file are unique and used to automatically locate the next file to be used in the process. The problem I can not seem to fix is that the full path needs to be referenced in the second portion and it is not currently. Is... (7 Replies)
Discussion started by: cmccabe
7 Replies

5. Shell Programming and Scripting

Bash script to replace text file from a lookup file

Hi. I need assistance with the replacing of text into a specific file via a bash script. My bash script, once run, currently provides a menu of computer names to choose.The script copies onto my system various files, depending what computer was selected in the menu.This is working OK. Now, I... (1 Reply)
Discussion started by: jonesn2000
1 Replies

6. Shell Programming and Scripting

Help on lookup script

Hi All, I've had a look around the forum but cannot find any answers for what I want to do. I have 2 files : FILE 1 =============== ter049107 ter049048 2013-04-09 08:15:16 ter049056 ter049083 ter049112 2013-04-09 10:35:10 2013-04-09 10:29:47 2013-04-09 07:44:05 FILE 2 - note... (4 Replies)
Discussion started by: prashantv
4 Replies

7. IP Networking

lookup ip address, subnet mask, gateway, and dns at same time

Is there a command that can lookup ip address, subnet mask, gateway, and dns all at the same. I know ifconfig can lookup ip address and subnet mask. I know route -n can lookup gateway. Not sure about a dns command. So I hope there is a way to lookup ip address, subnet mask, gateway, and dns all at... (2 Replies)
Discussion started by: cokedude
2 Replies

8. Shell Programming and Scripting

trim last octate of ip address using bash script

Hi, i need to replace the last octate in ipaddress with 0 using bash shell for one of my applicatiom. googling i found the below link where they do the same thing but use long2 ip which i dont see in linux. trim ip address octet - Stack Overflow Plz can soemone guide how do i do this... (5 Replies)
Discussion started by: akshatha
5 Replies

9. UNIX for Advanced & Expert Users

Clueless about how to lookup and reverse lookup IP addresses under a file!!.pls help

Write a quick shell snippet to find all of the IPV4 IP addresses in any and all of the files under /var/lib/output/*, ignoring whatever else may be in those files. Perform a reverse lookup on each, and format the output neatly, like "IP=192.168.0.1, ... (0 Replies)
Discussion started by: choco4202002
0 Replies

10. Shell Programming and Scripting

lookup script

hello I am a student taking an intro to UNIX class. I have an assignment I am having trouble completing. The assignment is as follows. i have a file called .addr_book that has various names and phone numbers in it. I need to write a script called lookup that will run like this I... (2 Replies)
Discussion started by: tampaJim
2 Replies
Login or Register to Ask a Question