# how to get ip



## graudeejs (Aug 29, 2009)

How get get ip address in sh or perl, If I know server adress?

For example I want to get IP for example.com
How to do that in sh or perl? (perl preferred)


----------



## DutchDaemon (Aug 29, 2009)

dig(1)?


----------



## graudeejs (Aug 29, 2009)

```
# arp freebsd.org
freebsd.org (69.147.83.40) -- no entry
```
works better (for what I want)

perhaps, there's tool/cmd to get IP only?


----------



## anemos (Aug 29, 2009)

```
#!/usr/bin/perl -w

use strict;
use Socket;

my $raw_ip;
my $dotted_ip;

$raw_ip = gethostbyname("freebsd.org");

if (defined $raw_ip) {
        $dotted_ip = inet_ntoa($raw_ip);
        print $dotted_ip;
} else {
        print 'Not found';
}
```


```
$ ./getip.pl
$ 69.147.83.40
```


----------



## Alt (Aug 29, 2009)

I would do like this





> > dig +noall +answer ya.ru | awk '{ print $5}'
> 213.180.204.8
> 77.88.21.8
> 93.158.134.8



Or if you need only 1 ip, in shell


> IP=`dig +noall +answer ya.ru | awk '{ print $5; nextfile; }'`


----------



## Voltar (Aug 29, 2009)

Alt said:
			
		

> I would do like this
> 
> Or if you need only 1 ip, in shell



You can remove the need for awk by using "+short", i.e.




```
vulcan# dig +short freebsd.org
69.147.83.40
vulcan# dig +short google.com
74.125.67.100
74.125.127.100
74.125.45.100
vulcan#
```


----------

