adding users/domains in bulk

nobaloney

NoBaloney Internet Svcs - In Memoriam †
Joined
Jun 16, 2003
Messages
25,086
Location
California
We've got a client who's just asked us to setup 136 users, each with one domain, on their owned server.

I believe there have been posts on how to automate this but even Googling, I couldn't find anything.

Has anyone done this? I don't want to reinvent the wheel if possible.

Thanks.

Jeff
 
Hi Jeff,

You could do this as a CSV file import (with PHP):

Code:
<?php

include('httpsocket.php');

$server_ip="11.22.33.44";
$server_login="admin";
$server_pass="yourpass";
$server_ssl="N";

$sock = new HTTPSocket;
if ($server_ssl == "Y"){
	$sock->connect("ssl://".$server_ip, 2222);
}else{
	$sock->connect($server_ip, 2222);
}

$sock->set_login($server_login,$server_pass);

$arrtest = array();

if(($handle = fopen("import.csv", "r" )) !== FALSE){

	while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
	
		$num = count($data);
		
		$arrtest = array(
		   'action' => 'create',
		   'username' => $data[0],
		   'email' => $data[1],
		   'passwd' => $data[2],
		   'passwd2' => $data[2],
		   'domain' => $data[3],
		   'package' => $data[4],
		   'ip' => $server_ip,
		   'notify' => 'no',
		   'add' => 'Submit'
		);

		// print_r($arrtest);
		
		$sock->query('/CMD_API_ACCOUNT_USER', $arrtest );  
		
		$result = $sock->fetch_parsed_body();
		
		if($result['error']!="0"){
			echo "<b>Error creating username ".$data[0]." on server $server_ip<br>\n";
			echo $result['text']."<br>\n";
			echo $result['details']."<br>\n";
		}else{
			echo "User ".$data[0]." created on server $server_ip<br>\n";
		}
	
	}
	fclose($handle);
	exit0;
}

?>

set your CSV up like this:

username, email, passwd, domain, package

Note:

You should do more checking on this script - data input is "trusting" the CSV has correct data.

You get the httpsocket.php file from here:

http://files.directadmin.com/services/all/httpsocket/httpsocket.php


There's a stack of other options you can throw into the array:

http://www.directadmin.com/api.html#create


I've just tested this code and it works. Hope this helps you.
 
Last edited:
Jeff do you have the list of users and domains in a file? Or can your client do that for you?
 
You should do more checking on this script - data input is "trusting" the CSV has correct data.
I can make sure the CSV file has the correct data. I'm not a PHP kind of guy; care to contact me by email for further discussion?

Jeff
 
Jeff do you have the list of users and domains in a file? Or can your client do that for you?

I can create whatever I need. The client's list was horrible:

Domain names only, some in lowercase, some in uppercase, some in mixedcase, some singlespaced, some doublespaced, some even triplespaced, and with indents of various lengths :(.

But I've cleaned it up and now have a good list of domains, all lowercase.

I was hoping for a script which would take the first six characters of the domain name and then add a numeric series starting with 0001 and incrementing, to assure somewhat meaningful automated non-repeating usernames.

Do you have some ideas for me?

Jeff
 
Hopefully this is readable. Perl code. Make sure LWP is installed. I will edit as necessary.

Put the domains in the domains.txt file.

The code will start with the number 001 and also use the first 5 digits of the domain plus the number for the username. Only the first 5 because DA by default only allows 8 character usernames.

Supply the variable information at the top.

Code:
$count = "001";
$dapackage = ""; #Package name. You have to create a package before adding users.
$daemail = ""; #Email for the new user. Don't forget to escape the @ sign like \@
$daip = ""; #Ip of the server
$daadminusername = ""; #The admin username
$daadminpassword = ""; #The admin password

open(FILE, ">passwordlist.txt");

use HTTP::Request::Common qw(GET);
use LWP::UserAgent;
use HTTP::Cookies; # Not sure if that is needed or not but doesn't hurt. I used to use it.
$ua = new LWP::UserAgent;


@domains = `cat domains.txt`;
chomp(@domains);

foreach $domains(@domains){

        $count = sprintf( "%03d", $count);

        $domain = substr($domains, 0, 5);

        $dausername = "$domain"."$count";

        &adduser;

        print FILE "$dausername\t$domains\t$dapassword\n";

        $count++;
}



sub adduser{

$dapassword=&generate_random_string(8);

$cmd = "CMD_API_ACCOUNT_USER?action=create\&package=$dapackage\&add=Submit\&notify=no\&username=$dausername\&email=$daemail\&passwd=$dapassword\&passwd2=$dapassword\&ip=$daip\   &domain=$domains";

$url = "http://$daadminusername\:$daadminpassword\@$daip\:2222/$cmd";

my $req = GET $url;

@results = $ua->request($req)->as_string;


print "@results\n";

}

sub generate_random_string
{
        $random_string = ""; #Clear out previous password

        $length_of_randomstring=shift;

        @chars=('a'..'z','A'..'Z','0'..'9'); # lowercase, capital, and numbers
        #$random_string;
        foreach (1..$length_of_randomstring)
        {
                $random_string.=$chars[rand @chars];
        }
        return $random_string;
}
 
Last edited:
Are you trying to spoil perl's reputation? It's supposed to be a write once, read never language, but your perl is actually readable :).

Your script is great! But it only uses only one password. What I need is for it to create random passwords (eight characters total, alpha/numeric, at least some of each, some caps, some lowercase) for each, and to save the list something like this:

username<tab>domain-name<tab>password

if possible.

Can do?

Thanks :D

Jeff
 
Hi, could anyone rewrite @ranz code, but to only add multiple domains from domains.txt or csv file, to one user?
 
Back
Top