[SortofNewbie] Addusers with script on crontab

H

Henk

Ok guy's i need some help i have to create allot of users on an new
webserver (also the must have ftp access vistualhost and so on) i've got an
script allready but there comes an new user (and here it comes) i don't know
how to check if the username already exists in linux and perl how can i do
this ? is there any one who allready has done thius please show me an
snippet of that code ;)

Many thnx...
 
H

Henk

Gunnar Hjalmarsson said:
Unfortunately I don't have such a script.

Where can I sign up for an account on your server?

That's a shame ;(
Sorry but u can't sign up for it (yet ;) ) it's for an private collage
network
btw.. sorry for the crappy english but i hope you all understand what i am
trying to write here ;)
 
H

Henk

Henk said:
That's a shame ;(
Sorry but u can't sign up for it (yet ;) ) it's for an private collage
network
btw.. sorry for the crappy english but i hope you all understand what i am
trying to write here ;)

Btw this is what i made up so far ;)
#!/usr/bin/perl

use HTTP::Request;
use HTTP::Response;
use LWP::UserAgent;

$PrintDebug = 1; # Print debugging information
$baseURL = "Url to asp file with data";
$ip = &get_ip();

sub Debug(@){
$strDebug = shift;
if ($PrintDebug == 1) { print("$strDebug\n"); }
}

sub get_ip {
my ($myip, @iparr) = (undef, []);
$_ = `ifconfig`;
@iparr = /inet addr:(\d+\.\d+\.\d+\.\d+)/ig;
$myip = $iparr[0];
return $myip;
}

# Aanspreken hosting.asp file via http request
sub ExecHttpReq(@)
{
my $URL = shift;
my $ua = new LWP::UserAgent;
$ua->agent( "Routit Backend");
die "No useragent" unless defined($ua);
$req = new HTTP::Request('GET',$URL);
return $ua->request($req);
}

$response = ExecHttpReq("$baseURL/hosting.asp");

if( !$response->is_success ){
print $response->status_line;
print " FAILED\n";
exit 1;
}

# splitten van hosting.asp file voor het gebruiken van de data
@lines = split /\n/, $response->content;
for( $i=0; $i<= $#lines; $i++ ){
($ID, $ProductCode, $DealerKlantID,
$Naam, $DealerID, $Bedrijf,
$Username, $Password, $Domainname,
$ASP, $PHP, $Stats,
$Tag, $server) = split /\t/, $lines[$i];
$status = 0; #OK, account created

# Kijken of de hosting niet voor windows is
chomp $PHP;
chomp $Tag;
if($PHP =~ /true/) {
$status = createuser($Username, $Password);
$status = createvirtualhost();
# updatestatus($ID, $status);
}
# Kijken of dat er stats nodig zijn
if($Stats=~ /true/) {
$status = awstats();
}
if($Tag =~ /2/) {
$status = deleteall();
}
if($Tag=~ /3/){
$status = deletestats();
}

# Sub maken van user
sub createuser(@) {
Debug ("Making User");
chomp $Username;
Debug("Setting Password");
chomp $Password;
system("adduser -g webusers -d /var/www/$Domainname $Username");
system("echo \"$Password\" | /usr/bin/passwd --stdin $Username");
Debug("User added...");
chmod (0750, "/var/www/$Domainname");
Debug("Setting permissions...");
return 0; # account create
}

# Sub hosting status
sub updatestatus(@) {
ExecHttpReq("$baseURL/hosting.asp?ID=$ID#Status=$status");
}

# Sub voor het maken van Virtualhosts
sub createvirtualhost(@) {
Debug("setting domainname");
Debug( "mkdir $Domainname");
print("mkdir() ", mkdir("/etc/httpd/conf/virtualhosts/$Bedrijf",
0750),"\n");
print "making apache virtualhost dir\n";
open(APACHECONF,
">/etc/httpd/conf/virtualhosts/$Bedrijf/$Domainname.conf");
$strCONF = "<VirtualHost $ip>\n";
$strCONF .= "\tDocumentRoot /var/www/$Domainname\n";
$strCONF .= "\tServerName $Domainname\n";
$strCONF .= "\tServerAlias www.$Domainname\n";
$strCONF .= "\tServerAdmin webmaster\@$Domainname\n";
$strCONF .= "\tErrorLog logs/$Domainname.error.log \n";
$strCONF .= "\tCustomLog /var/log/httpd/$Domainname.log combined\n";
$strCONF .= "</VirtualHost>\n";
print APACHECONF $strCONF;
print "helemaal klaar !\n";
close(APACHECONF);
return 0; # Virtual host file created
}

print "Restarting Apache deamon\n";
system("killall -HUP httpd");
print "Klaar.\n";

# Sub voor het maken van de AWStats
sub awstats(@) {
Debug ("Making the dir and setting the awstats settings");
print("mkdir() ", mkdir("/var/www/$Domainname/stats", 0750),"\n");
print("chmod() ", chmod(".R /var/www/$Domainname/stats/", 0750),"\n");
system("cp -R /sys/scripts/awstats/icon/ /var/www/$Domainname/stats/");
system("chown -R root:webusers /var/www/$Domainname/stats");

# Het openen en editten van de template van awstats..

$domain = "$Domainname";
open(TEMPLATE, "</sys/scripts/awstats/awstats.conf");
open(CONFIG, "> /etc/awstats/awstats.$Domainname.conf");
@TEMPLATE=<TEMPLATE>;
for (@TEMPLATE) {
s/<DOMAIN>/$Domainname/g;
}

print CONFIG "@TEMPLATE\n";
close TEMPLATE;
close CONFIG;

return 0; # awstats done !


}

sub deleteall(@) {
Debug ("Setting up for all delete");
print ("rm() ", rm("/var/www/$Domainname/stats"),"\n"); # lets see if this
works
print ("rm() ", rm("/var/www/$Domainname"),"\n"); # lets see if this works
print ("rm() ", rm("/etc/httpd/conf/virtualhosts/$Bedrijf"),"\n");
# print ("rm() ", rm("/etc/awstats/awstats.$Domainname.conf"),"\n);

}
#sub deletestats(@) {
}
 
J

Joe Smith

Henk said:
i don't know how to check if the username already exists in linux

linux% perl -le 'print join ":",getpwnam("jms")'
jms:x:103:103:::Joe Smith:/home/jms:/bin/tcsh
linux% perl -le 'print join ":",getpwnam("invalid")'

linux% perldoc -f getpwnam

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top