Linuxhost
コマンドは次を返します。
hostA.domain.com has address xx.xxx.xxx.xx
$ipaddr
IPアドレスをどのように取得して変数に入れますか?
open(FILE, "hostlist.txt") or die("Unable to open file");
@hostnames = <FILE>;
close(FILE);
foreach $hostname (@hostnames)
{
$lookup = qx(host $hostname);
$ipaddr = grep ip_address $lookup; <---- need help here
print $ipaddr;
}
ベストアンサー1
StackOverflowではなくUnixおよびLinuxサイトにこのコンテンツを投稿しました。
cat hostlist.txt | xargs resolveip -s
ただし、これはIPアドレスのみを返します。
一部のホスト名には複数のIPアドレスが接続されています。
$ host www.google.com
www.google.com is an alias for www.l.google.com.
www.l.google.com has address 74.125.227.18
www.l.google.com has address 74.125.227.17
www.l.google.com has address 74.125.227.16
www.l.google.com has address 74.125.227.20
www.l.google.com has address 74.125.227.19
IPリストのみを取得するには、次のいずれかの方法を使用してください。
host <hostname> | grep "has address" | awk '{print $4}'
Perlを引き続き使用するには、resolvipを使用します。
$ipaddr = qx(resolveip -s $hostname);
または、シェルコマンドを実行せずにすべてのIPを取得します。
use Socket;
@ipaddrs = map { inet_ntoa($_) } (gethostbyname($hostname))[4,];