A
Alex
Hello,
I have several Sun boxes that get their ip via DHCP. Once in a while,
when the ip's change, then I have to manually edit the /etc/hosts
files on all the boxes to correlate with the new IP. This is kind of
a hassle, so I wanted to use Perl to try and create a script that can
do this automatically. I am new to Perl, and scripting for that
matter, so I was hoping for some assistance. Here is what I have so
far for a machine named Morphine:
#!/usr/bin/perl -w
$ip = `ifconfig hme0 | grep inet | cut -f 2 -d " "`;
$ip_hostfile = `less /etc/hosts | grep Morphine | cut -f 1`;
print "The current ip is: $ip\n";
print "The ip in /etc/hosts is: $ip_hostfile\n";
if ($ip eq $ip_hostfile)
{
print "The ip's are the same. No changes made.\n";
}
else
{
print "The ip's are different, writing new ip to
/etc/hosts\n";
open(INPUT, "/etc/hosts");
open(OUTPUT, ">/etc/newhosts");
while (<INPUT>)
{
s/$ip_hostfile/$ip/g;
print OUTPUT;
}
close(INPUT);
close(OUTPUT);
}
Obviously, I haven't gotten to the part where I actually replace the
newhosts file with the /etc/hosts file, because the replacement of the
$ip_hostfile with the $ip is not working. It creates the new file,
and populates it with the contents of the INPUT file, but the new ip
doesn't get placed in the newhosts file. The ip that needs to be
changed doesn't get changed, it stays the same value as in the INPUT
file. However, what I have found is that in this line:
s/$ip_hostfile/$ip/g, if I replace the variables with actual ip's,
then the script works fine...the values are switched like they are
supposed to. So the problem seems to be that the variables are not
working in that line for some reason. The variables are initializing
correctly, bacause the print statements correctly show the ip's. So,
any help would be much appreciated.
Thanks,
Alex
I have several Sun boxes that get their ip via DHCP. Once in a while,
when the ip's change, then I have to manually edit the /etc/hosts
files on all the boxes to correlate with the new IP. This is kind of
a hassle, so I wanted to use Perl to try and create a script that can
do this automatically. I am new to Perl, and scripting for that
matter, so I was hoping for some assistance. Here is what I have so
far for a machine named Morphine:
#!/usr/bin/perl -w
$ip = `ifconfig hme0 | grep inet | cut -f 2 -d " "`;
$ip_hostfile = `less /etc/hosts | grep Morphine | cut -f 1`;
print "The current ip is: $ip\n";
print "The ip in /etc/hosts is: $ip_hostfile\n";
if ($ip eq $ip_hostfile)
{
print "The ip's are the same. No changes made.\n";
}
else
{
print "The ip's are different, writing new ip to
/etc/hosts\n";
open(INPUT, "/etc/hosts");
open(OUTPUT, ">/etc/newhosts");
while (<INPUT>)
{
s/$ip_hostfile/$ip/g;
print OUTPUT;
}
close(INPUT);
close(OUTPUT);
}
Obviously, I haven't gotten to the part where I actually replace the
newhosts file with the /etc/hosts file, because the replacement of the
$ip_hostfile with the $ip is not working. It creates the new file,
and populates it with the contents of the INPUT file, but the new ip
doesn't get placed in the newhosts file. The ip that needs to be
changed doesn't get changed, it stays the same value as in the INPUT
file. However, what I have found is that in this line:
s/$ip_hostfile/$ip/g, if I replace the variables with actual ip's,
then the script works fine...the values are switched like they are
supposed to. So the problem seems to be that the variables are not
working in that line for some reason. The variables are initializing
correctly, bacause the print statements correctly show the ip's. So,
any help would be much appreciated.
Thanks,
Alex