Perl Hostname Generator for linux

2 replies [Last post]
EverestX
EverestX's picture
Offline
SX Crew
Joined: 2009/05/15

Here's a host name generator I whipped together in perl for backtrack boxes.

I'll be honest, I'm not a very saavy coder, so there may very well be a better way to do this with the code. Any tips would be great, but in the mean time, this works fairly well.

#!/usr/bin/perl
########################################
#Perl Random Hostname Generator
#
#  Files Modified:
# /etc/hostname
# /proc/sys/kernel/hostname
#
# A SoldierX utility by EverestX
########################################


print "****Hostname Generator****\n";
print "    A SoldierX Utility\n\n";



if ($#ARGV !=) {

#This Section generates a 5 char alphanumeric value
       $host = rand_host();



      sub rand_host {

      my @chars = ('a'..'z','0'..'9');

      my $len = 4;

       $host = '';

      for (..$len) {

      $host .= $chars[int rand @chars];

      }



# This Section generates the 1st Letter


     $letter = rand_letter();

    sub rand_letter{

    my @chars = ( 'A'..'Z');

    my $length = ;

     $letter = '';

    for (..$length){
   
    $letter .= $chars[int rand @chars];

}

#Combine the 1st letter & other 5 digits.

$hostname = $letter.$host;

print " Random hostname set to: $hostname \n";


#Change the host files

open (FILE1, '>/etc/hostname');
 print FILE1 "$hostname";
  close (FILE1);


open (FILE2, '>/proc/sys/kernel/hostname');
 print FILE2 "$hostname";
  close (FILE2);

}

}
}