#!/usr/bin/perl -w
#
use warnings;
#program version
$VERSION="0.1";
print "AUTOSAMBA\nWritten for the JULEX project by Thomas Karpiniec.\n";
print "Email: tkarpiniec\@gmail.com\n\n";
print "Attempting to find a free netbios name... ";
$nonamefound = 1;
open (NMBLOOKUP,"/usr/bin/nmblookup julex |"); #Test for availability of "julex" first.
@grab = <NMBLOOKUP>; #read in the results
close(NMBLOOKUP);
foreach $line(@grab) {
if ($line =~ /name_query/) { $nonamefound = 0; } #If it has "name_query" anywhere, it's free
}
#Now if it wasn't free, loop up numbers until you find one that is!
$number = 0;
while ($nonamefound == 1) {
$number = $number + 1;
open (NMBLOOKUP,"/usr/bin/nmblookup julex$number |");
@grab = <NMBLOOKUP>;
close(NMBLOOKUP);
foreach $line(@grab) {
if ($line =~ /name_query/) { $nonamefound = 0; }
}
}
if ($number == 0) { $compname = "julex"; } #If it's free, use the computer name "julex"
else { $compname = "julex$number"; } #Otherwise, julex1, julex2, whatever comes first and is free
print "done.\n";
#Having decided upon the computer name, let's set up the global parameters
push(@sambafile, "[global]\n");
push(@sambafile, "workgroup = HOME\n");
push(@sambafile, "security = SHARE\n");
push(@sambafile, "hosts allow = localhost, local-machine-name, 192.168.0., 192.168.1., 10.0.0.\n");
push(@sambafile, "hosts deny = all\n");
push(@sambafile, "netbios name = $compname\n");
push(@sambafile, "guest only = yes\n");
#Now that the global parameters are set up, let's read in the /mnt ls listing
print "Checking /mnt for shares... ";
open (LSMNT,"/bin/ls /mnt |"); #Run:$ ls /mnt
@ls = <LSMNT>; #read in the results
foreach $line(@ls) { #separate them all out since they'll most likely be on one line
@sharelines = split(/ /, $line);
foreach $_(@sharelines) {
s/\n//g;
push(@shares, $_);
}
}
print "done.\n";
#now that we have all our share names in @shares we can add the share listings
#to our output file :D
foreach $sharename(@shares) {
push(@sambafile,"[$sharename]\npath = /mnt/$sharename\ncomment = Julex share of /mnt/$sharename\nread only = no\nguest ok = yes\nwriteable = yes\n\n");
}
#ok now our file's set up, output to /tmp/smb.conf and start the daemons!
print "Writing smb.conf to /tmp... ";
open(SMBFILE,">/tmp/smb.conf");
print SMBFILE @sambafile;
close(SMBFILE);
print "done.\n";
print "Starting services (smbd & nmbd)... ";
system("/usr/sbin/smbd -s /tmp/smb.conf");
system("/usr/sbin/nmbd -s /tmp/smb.conf");
print "done.\n";
#Now tell the user what's just been set up
print "\nComputer name: $compname\nWorkgroup: HOME\n\nSHARES\n======\n";
foreach $share(@shares) {
print "$share\n";
}
__END__
=head1 NAME
autosamba - An automatic configuration tool for samba in Julex, live-on-cd Linux distribution.
=head1 SYNOPSIS
Run this script from a console and it will automatically share all entries in /mnt with read/write access.
=head1 AUTHOR
Thomas Karpiniec
=cut