0

I have this line in my perl script :

"$retrieveIps --fields FileserverIPS WHERE Fileserver LIKE '%NET%' | tr \" 
\" \"\n\" | xargs -n1 host";

This part of the line above :

$retrieveIps --fields FileserverIPS WHERE Fileserver LIKE '%NET%'

Should give me this output :

NodeName1.comp.com NodeName2.comp.com

And I want to avoid using shell commands in my script as much as possible. I was wondering if there is any way to do the 'tr' , 'xargs' and 'host' command in perl? I tried to look on the internet but couldn't find anything helpful for me.

This is the required output :

10.0.0.1
10.0.0.2

1 Answer 1

3

You can use the gethostbyname function to get the IP address and Socket::inet_ntoa to unpack it:

use Socket;
my @hostnames
    = `$retrieveIps --fields FileserverIPS WHERE Fileserver LIKE '%NET%' `;
print inet_ntoa(scalar gethostbyname $_), "\n" for @hostnames;

inet_ntoa is easy to implement (but I'm not sure whether it'd work in Perl 4):

sub inet_ntoa {
    my ($ip) = @_;
    return join '.', map ord, split //, $ip
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for this solution , is there any other way to do that? , Since I am not able to use Socket in my company.
@user3819295: Socket is part of Perl core since 5.0. Are you still running Perl 4?!
Unfortunately yes , part of our environment still running Perl 4. I will accept this answer since it is answering my question. However, if you could come up with another solution it would be great. Thanks anyway!
Perl 4 is older than 23 years!
it Works!! , amazing , thank you so much for your time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.