Home > Web Development > Creating multiple FTP accounts in cPanel

Creating multiple FTP accounts in cPanel

I’m giving the Computer Applications course this term. It’s basically about Web Development basics (HTML, CSS, JavaScript,..).

I wanted to get students familiar with creating online webpages instead of doing it all on their local machines.

I decided to give every student an FTP account on my website to do some of their projects and assignments there.

After I could find a suitable host with unlimited FTP accounts (HostGator), I had a problem that I had to create 133 FTP accounts manually from the cPanel … So tiring!

In short, I could do this programatically through the cPanel API.

A php webpage from which I make a request/call to the cPanel webpage responsible for adding FTP accounts (doaddftp.html):

XXX.php


<!--?php
$cpanel_user = 'username';
// your cPanel password $cpanel_pass = 'password';
// your cPanel skin $cpanel_skin = 'x3';
// your cPanel domain $cpanel_host = 'mydomain.com';
// ftp username $ftp_user = $_POST['ftpuser'];
// ftp password $ftp_pass = $_POST['ftppass'];
// ftp quota $ftp_quota = '20';

// create the ftp account
$request = "http://{$cpanel_user}:{$cpanel_pass}@{$cpanel_host}:2082";
$request .= "/frontend/{$cpanel_skin}/ftp/doaddftp.html";
$request .= "?login={$ftp_user}";
$request .= "&password={$ftp_pass}";
$request .= "&homedir=public_html/projects/{$ftp_user}";
$request .= "&quota={$ftp_quota}";
$result = file_get_contents($request);
echo $result;
?-->

The XXX.php accepts the FTP username and password through POST method.

I’ve then created a java program that requests the XXX.php webpage 133 times each with a unique username and password for each student.


import java.io.*;
import java.net.*;
import java.util.Scanner;

/**
 *
 * @author Mindhunter - Ahmed Abdullah Hussein
 * mindhunter74@yahoo.com
 */
public class ftpAccountCreator {

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(new File("inputForAccounts.txt"));
        int numOfStudents = 133;
        for (int i = 1; i <= numOfStudents; i++) {
            String ftpuser = sc.next(), ftppass = sc.next();
            String params = "ftpuser=" + ftpuser + "&ftppass=" + ftppass;
            String response = excutePost("http://www.mydomain.com/XXX.php", params);
            if (response.contains("Account created successfully")) {
                System.out.println("Student " + i + " created successfully " + ftpuser + " " + ftppass);
            } else {
                System.out.println("Failure!!");
                System.exit(0);
            }
            Thread.sleep(3000);
        }
    }

    public static String excutePost(String targetURL, String urlParameters) {
        URL url;
        HttpURLConnection connection = null;
        try {
            //Create connection
            url = new URL(targetURL);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");

            connection.setRequestProperty("Content-Length", ""
                    + Integer.toString(urlParameters.getBytes().length));
            connection.setRequestProperty("Content-Language", "en-US");

            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            //Send request
            DataOutputStream wr = new DataOutputStream(
                    connection.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();

            //Get Response
            InputStream is = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String line;
            StringBuffer response = new StringBuffer();
            while ((line = rd.readLine()) != null) {
                response.append(line);
                response.append('\r');
            }
            rd.close();
            return response.toString();

        } catch (Exception e) {

            e.printStackTrace();
            return null;

        } finally {

            if (connection != null) {
                connection.disconnect();
            }
        }
    }
    // To check if any names are repeated before creating accounts (one time call)
    public boolean noNamesRepeated() throws Exception {
        Scanner sc = new Scanner(new File("inputForAccounts.txt"));
        String[] names = new String[133];
        for (int i = 0; i < names.length; i++) {
            names[i] = sc.next();
            sc.next();
        }
        for (int i = 0; i < names.length; i++) {
            String string = names[i];
            for (int j = 0; j < names.length; j++) {
                if (i != j && names[i].equals(names[j])) {
                    System.out.println(names[i]);
                    return false;
                }
            }
        }
        return true;
    }
    // To check if any passwords are repeated before creating accounts (one time call)
    public boolean noNPasswordsRepeated() throws Exception {
        Scanner sc = new Scanner(new File("inputForAccounts.txt"));
        String[] names = new String[133];
        for (int i = 0; i < names.length; i++) {
            sc.next();
            names[i] = sc.next();
        }
        for (int i = 0; i < names.length; i++) {
            String string = names[i];
            for (int j = 0; j < names.length; j++) {
                if (i != j && names[i].equals(names[j])) {
                    System.out.println(names[i]);
                    return false;
                }
            }
        }
        return true;
    }
}

Note that the java program reads accounts information from the file inputForAccounts.txt which is formatted as follows:
username1 pass1
username2 pass2

It took about only 6 minutes to create the 133 FTP accounts with the program and I could make it less!

Sources:

  1. http://stackoverflow.com/questions/8888287/create-ftp-account-in-cpanel-without-domain
  2. http://www.xyzws.com/Javafaq/how-to-use-httpurlconnection-post-data-to-web-server/139
Advertisements
Categories: Web Development
  1. Helen
    October 27, 2014 at 3:37 PM

    Please could I have more information on this. What did you save the java file as, where did you place all the files and how did you run the java application? Did you compile it in any particular way? Your help would be most appreciated. Thank you in advance.

  2. October 29, 2014 at 3:34 AM

    Hello Helen,

    I ran the Java application – normally – on my PC to read the file containing all username/password pairs that I’ve prepared first.
    The application automatically connects to the internet and calls the XXX.php webpage that I put in the path “http://www.mydomain.com/XXX.php”.

    To run this java file, you may download and use JDK with Netbeans bundle (http://www.oracle.com/us/technologies/java/jdk-7-netbeans-download-432126.html).

    Let me know if you need any further help.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: