Skip to content

Instantly share code, notes, and snippets.

@zentooo
Created December 26, 2012 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zentooo/4381194 to your computer and use it in GitHub Desktop.
Save zentooo/4381194 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use LWP::UserAgent;
use File::Slurp;
use POSIX qw//;
my $ua = LWP::UserAgent->new;
my $resolv_conf_localhost = <<EOF;
nameserver 127.0.0.1
search ap-northeast-1.compute.internal
EOF
my $resolv_conf_original = <<EOF;
nameserver 172.16.0.23
search ap-northeast-1.compute.internal
EOF
main: {
$ua->get("http://google.co.jp");
print("resolv.conf = localhost\n");
write_file("/etc/resolv.conf", $resolv_conf_localhost);
sleep(3);
print("resolve from parent\n");
$ua->get("http://google.co.jp");
sleep(3);
print("resolve from child\n");
fork_and_req();
sleep(3);
write_file("/etc/resolv.conf", $resolv_conf_original);
};
sub fork_and_req {
my $pid = fork();
if ( $pid == -1 ) {
printf STDERR "fork failed: %s", $!
}
elsif ( $pid == 0 ) {
$ua->get("http://google.co.jp");
POSIX::_exit(POSIX::EXIT_SUCCESS);
}
else {
wait();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment