[SortofNewbie] Addusers with script on crontab

Discussion in 'Perl Misc' started by Henk, Jun 24, 2004.

  1. Henk

    Henk Guest

    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...
    Henk, Jun 24, 2004
    #1
    1. Advertising

  2. Henk

    Henk Guest

    "Gunnar Hjalmarsson" <> wrote in message
    news:...
    > Henk wrote:
    > > 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 ;)

    >
    > Unfortunately I don't have such a script.
    >
    > Where can I sign up for an account on your server?
    >
    > --
    > Gunnar Hjalmarsson
    > Email: http://www.gunnar.cc/cgi-bin/contact.pl


    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 ;)
    Henk, Jun 24, 2004
    #2
    1. Advertising

  3. Henk

    Henk Guest

    "Henk" <> wrote in message
    news:40db1a8f$1_10@Output.100ProofNews.com...
    >
    > "Gunnar Hjalmarsson" <> wrote in message
    > news:...
    > > Henk wrote:
    > > > 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 ;)

    > >
    > > Unfortunately I don't have such a script.
    > >
    > > Where can I sign up for an account on your server?
    > >
    > > --
    > > Gunnar Hjalmarsson
    > > Email: http://www.gunnar.cc/cgi-bin/contact.pl

    >
    > 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(@) {
    }
    Henk, Jun 24, 2004
    #3
  4. Henk <> wrote:

    > Btw this is what i made up so far ;)



    You forgot these 2 lines:

    use warnings;
    use strict;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jun 25, 2004
    #4
  5. Henk

    Joe Smith Guest

    Henk wrote:
    > 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
    Joe Smith, Jun 26, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rbt
    Replies:
    3
    Views:
    362
  2. Replies:
    2
    Views:
    572
  3. Robert Bossy
    Replies:
    0
    Views:
    389
    Robert Bossy
    Mar 21, 2008
  4. Astley Le Jasper

    Running a Python script from crontab

    Astley Le Jasper, Dec 2, 2008, in forum: Python
    Replies:
    19
    Views:
    13,618
    Philip Semanchuk
    Dec 5, 2008
  5. Matt Cluver

    Perl Script Not Running From Crontab.

    Matt Cluver, Apr 15, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    146
Loading...

Share This Page