Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
First, is this some kind of education assignment?
Second, what have you tried or considered so far?
Third, what are your tools? Is your use constrained to a certain shell, interpreter, or environment?
Since you have a file all you need to do is:
(1) Insert dig google.com @ at the beginning of each line,
(2) Replace all occurrences of the colon ( character with a space followed by a -p followed by a space
It's a script I'm writing for myself.
I'm using bash.
I tried to pul all addresses (substring that end with ":" character) in an array, but I don't how to extract the PORT and use it in the command.
Since you have a file all you need to do is:
(1) Insert dig google.com @ at the beginning of each line,
(2) Replace all occurrences of the colon ( character with a space followed by a -p followed by a space
You now have a script file.
Make it executable and execute the script.
OK
^This will pretty much do it. But the ports can be an issue. How would you decide which port to use for which line (ip address) in the script?
Best,
HMW
Edit: Forget this part: How would you decide which port to use for which line (ip address) in the script? I didn't read the first post properly!!!
Assuming your source file name is source.txt
and you want the results in my.script,
you might try something like this:
Code:
for foo in `cat source.txt` ; do
IP=`echo ${foo}|cut -d: -f1`
PORT=`echo ${foo}|cut -d: -f2`
echo "dig google @${IP} -p ${PORT}"
done > my.script
Naturally, there are almost unlimited ways to do this depending upon what tools you want to use.
Bash string manipulation could easily pull apart the strings into IP and PORT without calling cut, but cut is clear and easy to understand, and performance is not really at issue with so small a file. The sed and awk utilities COULD be used, and perl would make it easy, but bash has everything you really need.
Look up a while / read loop and what setting IFS might do for you. With just a little searching I am sure you could this trivially.
I had to try this approach, and I agree that using a while loop and setting IFS appropriately is probably the smoothest way to go. Three lines of code and you're done!
My output (with echo instead of an actual "dig" at Google!):
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.