[2edec46] | 1 | #!/usr/bin/perl |
---|
| 2 | |
---|
| 3 | package gateway_lib; |
---|
| 4 | |
---|
[2b35261] | 5 | # Package stuff to keep the caller's namespace clean, but allow additions if |
---|
| 6 | # they need it. |
---|
[2edec46] | 7 | require Exporter; |
---|
| 8 | @ISA=qw(Exporter); |
---|
| 9 | @EXPORT_OK=qw(set_sshd_params import_key wait_for_DNS deter_tunnelip |
---|
| 10 | configure_outgoing_iface add_route bind_tap_to_iface iface_to_addr |
---|
[97edf0d] | 11 | dest_to_iface addr_to_iface read_config config_filename); |
---|
[8d4e4fb] | 12 | |
---|
[2edec46] | 13 | |
---|
| 14 | use strict; |
---|
| 15 | |
---|
| 16 | use IO::File; |
---|
| 17 | use IO::Pipe; |
---|
[1899afd] | 18 | use IO::Socket; |
---|
[2edec46] | 19 | |
---|
| 20 | use File::Temp; |
---|
| 21 | use File::Copy; |
---|
[85462fb] | 22 | use Net::hostent; |
---|
[2edec46] | 23 | |
---|
[2b35261] | 24 | # Standard locations of these commands (FreeBSD) |
---|
[2edec46] | 25 | my $IFCONFIG = "/sbin/ifconfig"; |
---|
| 26 | my $ROUTE = "/sbin/route"; |
---|
| 27 | my $TMCC = "/usr/local/etc/emulab/tmcc"; |
---|
| 28 | my $FINDIF = "/usr/local/etc/emulab/findif"; |
---|
[8d4e4fb] | 29 | my $TMCC = "/usr/local/etc/emulab/tmcc"; |
---|
[2edec46] | 30 | |
---|
[f8fa72b] | 31 | # Linux choices |
---|
| 32 | my $BRCTL = "/usr/sbin/brctl"; |
---|
| 33 | my $IPROUTE = "/sbin/ip route "; |
---|
| 34 | |
---|
[8f654de] | 35 | $BRCTL = '/sbin/brctl' unless -x $BRCTL ; |
---|
| 36 | |
---|
[2b35261] | 37 | # Takes an ssh config file and a reference to a hash of keys whose values must |
---|
| 38 | # be set a specific way. Replaces existing entries with the set values. |
---|
[2edec46] | 39 | sub set_sshd_params { |
---|
| 40 | my($keys, $file) = @_; |
---|
| 41 | my $f; |
---|
| 42 | my $t; |
---|
| 43 | |
---|
| 44 | $file = "/etc/ssh/sshd_config" unless $file; |
---|
| 45 | $f = new IO::File("$file") || die "Can't read $f: $!\n"; |
---|
| 46 | $t = new File::Temp() || die "Can't open tempfile: $!\n"; |
---|
| 47 | |
---|
[5e71d34] | 48 | # If the parameter is in the file (even in a comment) update the line to |
---|
| 49 | # have the new parameter. |
---|
| 50 | my %updated; |
---|
[2edec46] | 51 | while (<$f>) { |
---|
| 52 | foreach my $k (keys %{$keys}) { |
---|
[5e71d34] | 53 | if (s/^\s*#?\s*$k.*/$k $keys->{$k}/) { $updated{$k}++; } |
---|
[2edec46] | 54 | } |
---|
| 55 | print $t $_; |
---|
| 56 | } |
---|
[5e71d34] | 57 | # Any parameters not found above are output directly. |
---|
| 58 | foreach my $k (keys %{$keys}) { |
---|
| 59 | print $t "$k " . $keys->{$k} . "\n" unless $updated{$k}; |
---|
| 60 | } |
---|
[2edec46] | 61 | $f->close(); |
---|
| 62 | $t->close(); |
---|
| 63 | copy("$file", "$file.hold"); |
---|
| 64 | copy($t->filename, $file); |
---|
| 65 | # When $t goes out of scope, the tmpfile is deleted |
---|
| 66 | } |
---|
| 67 | |
---|
[2b35261] | 68 | # Append the given keyfile to the given authorised key file. |
---|
[2edec46] | 69 | sub import_key { |
---|
| 70 | my($keyfile, $authkeys) = @_; |
---|
| 71 | |
---|
| 72 | my $kf = new IO::File($keyfile) || die "Can't open $keyfile:$!\n"; |
---|
| 73 | my $ak = new IO::File(">>$authkeys") || die "Can't open $authkeys: $!\n"; |
---|
| 74 | |
---|
| 75 | while (<$kf>) { |
---|
| 76 | print $ak $_; |
---|
| 77 | } |
---|
| 78 | $kf->close(); |
---|
| 79 | $ak->close(); |
---|
| 80 | } |
---|
| 81 | |
---|
[2b35261] | 82 | # Keep trying to look up the given hostname until successful. If timeout is |
---|
| 83 | # given, die after waiting that long. If sleep is given, wait that many |
---|
| 84 | # seconds between attempts (defaults to 5). |
---|
[2edec46] | 85 | sub wait_for_DNS { |
---|
[2b35261] | 86 | my($name, $timeout, $sleep) = @_; |
---|
[2edec46] | 87 | my $start = time(); |
---|
[2b35261] | 88 | $sleep = 5 unless $sleep; |
---|
[85462fb] | 89 | my $rv; |
---|
[2edec46] | 90 | |
---|
[87b1a06] | 91 | # If the name is an IP address, skip this. |
---|
| 92 | return if $name =~ /^[\d\.]+$/; |
---|
| 93 | |
---|
[85462fb] | 94 | while (!$rv) { |
---|
| 95 | $rv = gethostbyname($name); |
---|
[2edec46] | 96 | die "Timeout waiting for DNS to get $name\n" |
---|
| 97 | if ($timeout && time() - $start > $timeout); |
---|
[85462fb] | 98 | sleep($sleep) unless $rv; |
---|
[2edec46] | 99 | } |
---|
| 100 | } |
---|
| 101 | |
---|
[2b35261] | 102 | # Get the external access parameters (interface, address, netmask, mac address, |
---|
| 103 | # and next hop router) from tmcd in Emulabs that support the DETER tunnelip |
---|
| 104 | # extension. |
---|
[2edec46] | 105 | sub deter_tunnelip { |
---|
| 106 | # To parse tmcc |
---|
| 107 | my $tmcc = new IO::Pipe || die "Can't create tmcc pipe: $!\n"; |
---|
| 108 | my $interface; # Interface with external address |
---|
| 109 | my $ip; # IP address of external interface |
---|
| 110 | my $mac; # MAC address |
---|
| 111 | my $netmask; # Netmask |
---|
| 112 | my $router; # Router for the internet |
---|
| 113 | |
---|
| 114 | |
---|
[2b35261] | 115 | # Parse out the info about tunnelips. Format is usually one line of |
---|
| 116 | # ATTR=VALUE. Multiple lines are possible. |
---|
[2edec46] | 117 | $tmcc->reader("$TMCC tunnelip"); |
---|
| 118 | while (<$tmcc>) { |
---|
| 119 | chomp; |
---|
| 120 | /TUNNELIP=([\d\.]*)/ && do { $ip = $1; }; |
---|
| 121 | /TUNNELMASK=([\d\.]*)/ && do { $netmask = $1; }; |
---|
| 122 | /TUNNELMAC=([[:xdigit:]]*)/ && do { $mac = $1; }; |
---|
| 123 | /TUNNELROUTER=([\d\.]*)/ && do { $router = $1; }; |
---|
| 124 | } |
---|
| 125 | $tmcc->close(); |
---|
| 126 | |
---|
| 127 | die "No MAC information for tunnel.\n" unless $mac; |
---|
| 128 | |
---|
| 129 | # Use the emulab findif command to get the right interface to configure |
---|
| 130 | $interface = `$FINDIF $mac`; |
---|
| 131 | chomp $interface; |
---|
| 132 | die "Can't get interface for mac address $mac: $?" if $? || !$interface; |
---|
| 133 | |
---|
| 134 | return ($interface, $ip, $netmask, $mac, $router); |
---|
| 135 | } |
---|
| 136 | |
---|
[2b35261] | 137 | |
---|
| 138 | # Configure the given interface with the given IP address and netmask. |
---|
[2edec46] | 139 | sub configure_outgoing_iface { |
---|
[2b35261] | 140 | my ($interface, $ip, $netmask) = @_; |
---|
[2edec46] | 141 | |
---|
| 142 | my @ifconfig = ($IFCONFIG, $interface, $ip); |
---|
| 143 | push(@ifconfig, 'netmask', $netmask) if $netmask; |
---|
| 144 | |
---|
| 145 | system(@ifconfig); |
---|
| 146 | die join(" ", @ifconfig) . " failed: $!\n" if $?; |
---|
| 147 | } |
---|
| 148 | |
---|
[2b35261] | 149 | # Add a route to the destination through the router. If wait is given, do not |
---|
| 150 | # attempt to add the route until DNS has the hostname in it. If timeout is |
---|
| 151 | # given, only wait that many seconds for DNS to acquire it. |
---|
[2edec46] | 152 | sub add_route { |
---|
| 153 | my($routedest, $router, $wait, $timeout) = @_; |
---|
| 154 | my @cmd; |
---|
| 155 | # Linux and FreeBSD use slightly different route syntax, so get the OS |
---|
| 156 | my $os = `uname`; |
---|
| 157 | chomp $os; |
---|
| 158 | |
---|
| 159 | $timeout = 0 unless $timeout; |
---|
| 160 | die "add_host_route needs a router and a destination\n" |
---|
| 161 | unless $router && $routedest; |
---|
| 162 | |
---|
| 163 | wait_for_DNS($routedest, $timeout) if $wait; |
---|
| 164 | |
---|
| 165 | if ( $os =~ /^Linux/ ) { |
---|
| 166 | @cmd = ($ROUTE, 'add', $routedest, 'gw', $router); |
---|
| 167 | } |
---|
| 168 | elsif ( $os =~ /^FreeBSD/ ) { |
---|
| 169 | @cmd = ($ROUTE, 'add', $routedest, $router); |
---|
| 170 | } |
---|
| 171 | else { |
---|
| 172 | die "Unknown OS: $os\n"; |
---|
| 173 | } |
---|
| 174 | system(@cmd); |
---|
| 175 | warn join(" ", @cmd) . " failed: $?\n" if $?; |
---|
| 176 | return $?; |
---|
| 177 | } |
---|
| 178 | |
---|
[2b35261] | 179 | # Connect the tap($tapno) interface to $iface at the link level. Remove any IP |
---|
| 180 | # addresses assigned to interface to avoid confusing the routing system. We're |
---|
| 181 | # very tolerant of errors as the bridge and other interfaces may already exist |
---|
| 182 | # when this is called. |
---|
[2edec46] | 183 | sub bind_tap_to_iface { |
---|
| 184 | my($tapno, $iface) = @_; |
---|
| 185 | my $bridge = "bridge$tapno"; |
---|
| 186 | my $tap = "tap$tapno"; |
---|
| 187 | my @addr = iface_to_addr($iface); |
---|
| 188 | |
---|
| 189 | # Wait for the tap |
---|
| 190 | system("$IFCONFIG $tap > /dev/null 2>/dev/null"); |
---|
| 191 | while ($?) { |
---|
| 192 | system("$IFCONFIG $tap > /dev/null 2>/dev/null"); |
---|
| 193 | } |
---|
| 194 | |
---|
[f8fa72b] | 195 | my $uname = `uname`; |
---|
| 196 | chomp $uname; |
---|
| 197 | |
---|
| 198 | if ($uname =~ /FreeBSD/) { |
---|
| 199 | system("ifconfig $bridge create"); |
---|
| 200 | warn "Cannot create bridge: $?\n" if $?; |
---|
| 201 | foreach my $a (@addr) { |
---|
| 202 | system("ifconfig $iface delete $a"); |
---|
| 203 | warn "Cannot delete address $a: $?\n" if $?; |
---|
| 204 | } |
---|
| 205 | system("ifconfig $bridge addm $iface up"); |
---|
| 206 | warn "Cannot add intefrace $iface to bridge: $?\n" if $?; |
---|
| 207 | system("ifconfig $bridge addm $tap"); |
---|
| 208 | warn "Cannot add intefrace $tap to bridge: $?\n" if $?; |
---|
| 209 | return $?; |
---|
| 210 | } |
---|
| 211 | elsif ($uname =~ /Linux/) { |
---|
| 212 | system("$IFCONFIG $iface 0.0.0.0 down"); |
---|
| 213 | system("$BRCTL addbr $bridge"); |
---|
| 214 | warn "Cannot create bridge: $?\n" if $?; |
---|
| 215 | system("$BRCTL addif $bridge $tap"); |
---|
| 216 | system("$BRCTL addif $bridge $iface"); |
---|
| 217 | system("$BRCTL stp $bridge off"); |
---|
| 218 | system("$IFCONFIG $bridge up"); |
---|
| 219 | system("$IFCONFIG $tap up"); |
---|
| 220 | system("$IFCONFIG $iface up"); |
---|
| 221 | return $?; |
---|
[2edec46] | 222 | } |
---|
| 223 | } |
---|
| 224 | |
---|
[2b35261] | 225 | # Return the IP addresses accociated with this interface (as a list) |
---|
[2edec46] | 226 | sub iface_to_addr { |
---|
| 227 | my($iface) = @_; |
---|
| 228 | my $ipipe = new IO::Pipe() || die "Can't create pipe for ifconfig: $!\n"; |
---|
| 229 | my @addr; |
---|
[f8fa72b] | 230 | my $uname = `uname`; |
---|
| 231 | my $ipre = "inet\\s+([0-9\\.]+)"; |
---|
| 232 | chomp $uname; |
---|
| 233 | |
---|
| 234 | if ($uname =~ /Linux/) { |
---|
| 235 | $ipre = "inet\\s+addr:\\s*([0-9\\.]+)"; |
---|
| 236 | } |
---|
[2edec46] | 237 | |
---|
| 238 | $ipipe->reader("$IFCONFIG $iface"); |
---|
| 239 | while(<$ipipe>) { |
---|
[f8fa72b] | 240 | /$ipre/ && push(@addr, $1); |
---|
[2edec46] | 241 | } |
---|
| 242 | $ipipe->close(); |
---|
| 243 | return @addr; |
---|
| 244 | } |
---|
| 245 | |
---|
[2b35261] | 246 | # Return the interface that packets to this host (IP or DNS) would be sent on. |
---|
[2edec46] | 247 | sub dest_to_iface { |
---|
| 248 | my($dest) =@_; |
---|
| 249 | my $rpipe = new IO::Pipe() || die "Can't create pipe for route: $!\n"; |
---|
[f8fa72b] | 250 | my $uname = `uname`; |
---|
| 251 | chomp $uname; |
---|
| 252 | my $route; |
---|
| 253 | my $dev_re; |
---|
| 254 | |
---|
| 255 | if ($uname =~ /Linux/) { |
---|
| 256 | $route = $IPROUTE; |
---|
| 257 | $dev_re = "\\s+dev\\s+(\\S+)"; |
---|
| 258 | } |
---|
| 259 | elsif ($uname =~ /FreeBSD/) { |
---|
| 260 | $route = $ROUTE; |
---|
| 261 | $dev_re = 'interface:\\s*([[:alnum:]]+)'; |
---|
| 262 | } |
---|
| 263 | |
---|
[2edec46] | 264 | |
---|
[f8fa72b] | 265 | print "$route get $dest\n"; |
---|
| 266 | $rpipe->reader("$route get $dest"); |
---|
[2edec46] | 267 | while (<$rpipe>) { |
---|
[f8fa72b] | 268 | /$dev_re/ && do { |
---|
[2edec46] | 269 | my $iface = $1; |
---|
| 270 | $rpipe->close(); |
---|
[f8fa72b] | 271 | print "matched: $iface\n"; |
---|
[2edec46] | 272 | return $iface; |
---|
| 273 | }; |
---|
| 274 | } |
---|
| 275 | $rpipe->close(); |
---|
| 276 | |
---|
| 277 | die "No route to $dest\n"; |
---|
| 278 | } |
---|
| 279 | |
---|
[2b35261] | 280 | # Return the interface bound to this IP address. If there are more than one, |
---|
| 281 | # the first one returned by ifconfig is the one returned. |
---|
[2edec46] | 282 | sub addr_to_iface { |
---|
| 283 | my($addr) = @_; |
---|
| 284 | my $ipipe = new IO::Pipe() || die "Can't create pipe for ifconfig: $!\n"; |
---|
| 285 | my $iface; |
---|
| 286 | |
---|
| 287 | $ipipe->reader("$IFCONFIG"); |
---|
| 288 | while(<$ipipe>) { |
---|
[8f654de] | 289 | /^([[:alnum:]]+):?/ && do { |
---|
[2edec46] | 290 | $iface = $1; |
---|
| 291 | next; |
---|
| 292 | }; |
---|
[8f654de] | 293 | /inet(\s+addr:)?\s*([0-9\.]+)/ && do { |
---|
| 294 | if ($2 eq $addr) { |
---|
[2edec46] | 295 | $ipipe->close(); |
---|
| 296 | return $iface; |
---|
| 297 | } |
---|
| 298 | }; |
---|
| 299 | } |
---|
| 300 | $ipipe->close(); |
---|
| 301 | die "Cannot match $addr to an interface\n"; |
---|
| 302 | } |
---|
| 303 | |
---|
[8d4e4fb] | 304 | sub read_config { |
---|
| 305 | my($file, $map) = @_; |
---|
| 306 | my %param; |
---|
| 307 | my %keywords; |
---|
| 308 | my $f; |
---|
| 309 | |
---|
| 310 | foreach my $k (keys %{$map}) { |
---|
| 311 | if ( $k =~ /(\S+)=[si]/ ) { |
---|
| 312 | $keywords{$1} = $map->{$k}; |
---|
| 313 | $param{$1}++; |
---|
| 314 | } |
---|
| 315 | else { |
---|
| 316 | $keywords{$k} = $map->{$k}; |
---|
| 317 | } |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | $f = new IO::File($file) || die "Can't open $file: $!\n"; |
---|
| 321 | while (<$f>) { |
---|
| 322 | /^\s*([^:]+):\s*(.*)/ && do { |
---|
| 323 | my $keyw = $1; |
---|
| 324 | my $val = $2; |
---|
| 325 | |
---|
| 326 | $keyw =~ tr [A-Z] [a-z]; |
---|
| 327 | |
---|
| 328 | if ($keywords{$keyw} ) { |
---|
| 329 | if ( $param{$keyw} ) { |
---|
| 330 | if ( ref($keywords{$keyw}) eq 'SCALAR') { |
---|
| 331 | ${$keywords{$keyw}} = $val; |
---|
| 332 | } |
---|
| 333 | elsif( ref($keywords{$keyw}) eq 'ARRAY') { |
---|
| 334 | push(@{$keywords{$keyw}}, $val); |
---|
| 335 | } |
---|
| 336 | else { |
---|
| 337 | die "Unknown variable type for $keyw\n"; |
---|
| 338 | } |
---|
| 339 | } |
---|
| 340 | else { ${$keywords{$keyw}}++; } |
---|
| 341 | } |
---|
| 342 | next; |
---|
| 343 | }; |
---|
| 344 | } |
---|
| 345 | $f->close(); |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | |
---|
[f8fa72b] | 349 | sub config_filename { |
---|
| 350 | # Find the configuration file in the usual places, if there is one in |
---|
| 351 | # /usr/local/federation/etc, use it, otherwise look in the emulab standard |
---|
| 352 | # filesystems which depends on what experiment and project we're in. |
---|
[8d4e4fb] | 353 | my $pid; |
---|
| 354 | my $eid; |
---|
| 355 | my $filename; |
---|
[f8fa72b] | 356 | my $fed_dir = "/usr/local/federation/etc/"; |
---|
| 357 | my $hn = `hostname`; |
---|
| 358 | chomp $hn; |
---|
| 359 | $hn =~ s/\..*//; |
---|
| 360 | |
---|
| 361 | return "$fed_dir/$hn.gw.conf" if -r "$fed_dir/$hn.gw.conf"; |
---|
| 362 | |
---|
[8d4e4fb] | 363 | my $tmcd = new IO::Pipe() || die "Can't create pipe: $!\n"; |
---|
| 364 | |
---|
| 365 | $tmcd->reader("$TMCC status"); |
---|
| 366 | |
---|
| 367 | while (<$tmcd>) { |
---|
| 368 | chomp; |
---|
| 369 | /ALLOCATED=([^\/]+)\/(\S+)/ && do { |
---|
| 370 | $pid = $1; |
---|
| 371 | $eid = $2; |
---|
| 372 | }; |
---|
| 373 | } |
---|
| 374 | $tmcd->close(); |
---|
| 375 | $filename = "/proj/$pid/exp/$eid/tmp/$hn.gw.conf" |
---|
| 376 | if $pid and $eid; |
---|
| 377 | |
---|
| 378 | return $filename; |
---|
| 379 | } |
---|
| 380 | |
---|
[f8fa72b] | 381 | |
---|
[9b3627e] | 382 | sub client_conf_filename { |
---|
| 383 | # Find the configuration file in the usual places, if there is one in |
---|
| 384 | # /usr/local/federation/etc, use it, otherwise look in the emulab standard |
---|
| 385 | # filesystems which depends on what experiment and project we're in. |
---|
| 386 | my $pid; |
---|
| 387 | my $eid; |
---|
| 388 | my $filename; |
---|
| 389 | my $fed_dir = "/usr/local/federation/etc/"; |
---|
| 390 | |
---|
| 391 | return "$fed_dir/client.conf" if -r "$fed_dir/client.conf"; |
---|
| 392 | |
---|
| 393 | my $tmcd = new IO::Pipe() || die "Can't create pipe: $!\n"; |
---|
| 394 | |
---|
| 395 | $tmcd->reader("$TMCC status"); |
---|
| 396 | |
---|
| 397 | while (<$tmcd>) { |
---|
| 398 | chomp; |
---|
| 399 | /ALLOCATED=([^\/]+)\/(\S+)/ && do { |
---|
| 400 | $pid = $1; |
---|
| 401 | $eid = $2; |
---|
| 402 | }; |
---|
| 403 | } |
---|
| 404 | $tmcd->close(); |
---|
| 405 | $filename = "/proj/$pid/exp/$eid/tmp/client.conf" |
---|
| 406 | if $pid and $eid; |
---|
| 407 | |
---|
| 408 | return $filename; |
---|
| 409 | } |
---|
| 410 | |
---|
[1899afd] | 411 | sub wait_for_port { |
---|
| 412 | my($addr, $port, $timeout, $sleep) = @_; |
---|
| 413 | my $start = time(); |
---|
| 414 | $sleep = 5 unless $sleep; |
---|
| 415 | die "Need both address and port\n" unless $addr && $port; |
---|
| 416 | |
---|
| 417 | my $s; |
---|
| 418 | |
---|
| 419 | while (!$s) { |
---|
[9b3627e] | 420 | # We've seen some arp pollution, so be proactive about clearing the |
---|
| 421 | # cache if we're waiting to get out. |
---|
| 422 | system("arp -d -a"); |
---|
[1899afd] | 423 | if (!($s = new IO::Socket(Domain => &AF_INET, PeerAddr => $addr, |
---|
| 424 | PeerPort => $port))) { |
---|
| 425 | if ($timeout and time() - $start > $timeout) { |
---|
| 426 | return undef; |
---|
| 427 | } |
---|
| 428 | else { sleep($sleep); } |
---|
| 429 | } |
---|
| 430 | } |
---|
| 431 | $s->close(); |
---|
| 432 | return 1; |
---|
| 433 | |
---|
| 434 | } |
---|
| 435 | |
---|
[2c16731] | 436 | # Fling a few ping packets at the peer in the hopes that it opens doors through |
---|
| 437 | # NATs and other filters. Practically speaking this can make a big difference. |
---|
| 438 | sub ping_peer { |
---|
| 439 | my($peer) = @_; |
---|
| 440 | system("ping -c 5 $peer"); |
---|
| 441 | } |
---|
| 442 | |
---|
[c3a3fe3] | 443 | sub testcmd_repeat { |
---|
| 444 | my($cmd, $timeout, $sleep) = @_; |
---|
| 445 | my $start = time(); |
---|
| 446 | $sleep = 5 unless $sleep; |
---|
| 447 | |
---|
| 448 | while (1) { |
---|
| 449 | system("$cmd"); |
---|
| 450 | if ($?) { |
---|
| 451 | if ($timeout and time() - $start > $timeout) { |
---|
| 452 | return undef; |
---|
| 453 | } |
---|
| 454 | else { sleep($sleep); } |
---|
| 455 | } |
---|
| 456 | else { return 1; } |
---|
| 457 | } |
---|
| 458 | } |
---|
[1899afd] | 459 | |
---|
[85462fb] | 460 | sub get_ip { |
---|
| 461 | my($name) = @_; |
---|
| 462 | my $gwip; |
---|
| 463 | if ( my $hent = gethostbyname($name) ) { |
---|
| 464 | $gwip = inet_ntoa($hent->addr_list->[0]); |
---|
| 465 | } |
---|
| 466 | return $gwip; |
---|
| 467 | } |
---|
| 468 | |
---|
[2edec46] | 469 | 1; |
---|