Creating multiple FTP accounts in cPanel

March 17, 2013 2 comments

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
Categories: Web Development

A message to myself

February 27, 2013 2 comments

عندما ماتت أمي كنت أواجه أي مشكلة بسؤال

“ماذا بعد وفاة أمي؟”

ماذا بعد وفاة أمي .. فأي كارثة أكبر من ذلك؟

ماذا بعد وفاة أمي .. فأي شيء بعد ذلك يهون

ومنذ أيام دفنت والدي بيدي .. وبعدها وجدت إجابة لهذا السؤال وقررت أن أواجه الدنيا كلها بتلك الإجابة

ماذا بعد وفاة أمي؟ وفاة أبي .. وماذا بعد وفاة أبي؟ وفاتي

“وفاتي” تلك هي الإجابة

نعم .. إنها الدنيا هل نسيت؟

إن شيئاً لن يبقى .. تلك الحقيقة البسيطة التي نتجاهلها

إن شيئاً لن يعمر في الأرض مهما طال به الزمن

سأكون يوماً في التراب .. سأعود يوماً للتراب ..

وسأحيا يوماً للحساب

Categories: Life

الفيس بوك والسياسة والإنتصار المزيف

December 24, 2012 1 comment

في وسط الصراع السياسي دلوقتي في بلدنا الفيس بوك بقى بالنسبة لكتير مننا شيء مهم جداً وواخد معظم وقتنا

 

أنا نفسي في الوضع ده .. وفكرت كتير في إني لازم أحط حد للوقت اللي بيضيع كتير عليه بس مأخدتش خطوة فعلية حتى الآن

 

المهم إني فكرت من كام يوم في السبب .. ولقيت إني وكتير من الناس واقعين في مشكلة بجد

 

مشكلة أكبر من مجرد وقت بيضيع وبس

 

لما حللت الموضوع لقيت إن الواحد مننا في وقت ما قبل كدة أخد فكرة عن فصيل سياسي معين إنهم مش كويسين وإقتنع بيها

 

وبناءاً على ده فهو مشترك في صفحات كتير بتهاجم الفصيل ده وعنده أصدقاء كتير بنفس الفكرة

 

كل مايدخل الفيس بوك ويشوف سقطة جديدة أو حاجة تدين الفصيل ده .. يتبسط .. وبسرعة يشارك بالحاجة دي

 

ده لأن الحاجة دي بتثبت إنه صح .. الفصيل ده وحش زي ماهو مقتنع وبيقول للناس .. بيشارك بالحاجة دي ولسان حاله بيقول – مش قلتلكم؟ وبيقول لنفسه – أنا صح

 

.. ووقتها بيحس بإنتصار .. إنتصار لأنه صح ولأن الباقي غلط .. لكنه من وجهة نظري إنتصار مزيف

 

ليه إنتصار مزيف؟

 

لأن كل اللي بتعمله ده من ورا شاشة .. وفاكر نفسك كدة صاحب موقف ورأي سياسي ومناضل من أجل الوطن .. وانت مابتشاركش في اي حاجة على أرض الواقع

 

عامل زي مشجع الكورة المتعصب اللي بيكره فريق معين بينافس فريقه .. وخسارة الفريق اللي بيكرهه بتبسطه جداً وتخليه مبسوط طول اليوم وغالباً بيقعد يغيظ في إصحابه وجيرانه اللي بيشجعوا الفريق ده ويحس وقتها بإنتصار مزيف

 

عارف إمتى يبقى مش مزيف؟ لو انت بتلعب مع الفريق اللي انت متعصبله وفرحت لما كسبت أو حتى فرحت لما الفريق المنافس خسر لأنك اتقدمت عليه في الترتيب أو حتى غلبته في ماتش

 

لو عايز تبقى منتصر بجد قدامك حاجة من إتنين

 

يا إما تبقى مشارك سياسياً بجد .. تنزل على أرض الواقع .. تنضم لحزب أو تفضل مستقل .. وتساهم في صنع التحول الديمقراطي لبلدك

 

أو .. لو مش هاتعرف تعمل ده لأي سبب

 

شوف انت بتعمل ايه .. بتشتغل في إيه؟ وفيد بلدك في مجال شغلك

 

إعمل عمل خيري ..

 

إعمل نشاط مفيد في بيتك .. في مدرستك .. في كليتك .. في مكان عملك .. مع جيرانك

 

شجع اللي حواليك على التغيير .. إبدأ بنفسك والقريبين منك .. بلدنا عايزانا إحنا نتغير عشان تتغير وتبقى أحسن

 

لو عملت كدة .. ساعتها بس .. هايبقى إنتصارك مش مزيف .. هايبقى إنتصار بجد

ومش هاتبقى لوحدك كسبان مهما كان انتمائك .. بلدنا كلها هاتبقى كسبانة

Categories: Life, Thoughts Tags:

Mom, I’ve got married

July 14, 2012 1 comment

Mother, I’ve got married!

Grandma told me about her dream of you few days before I get married and how happy you were preparing for my marriage.

This means that you are really feeling what we are doing and share us our happiness.

You must be also knowing that I didn’t forget you in that event and wished if you were beside me in everything.

Now let me tell you about her, my wife:

She has a great personality and mind. Her way of thinking is just special.

She memorized the Qur’an.

She is beautiful and she is a lady, she deserves having the title “lady”.

She has a lovely smile.

She has goals in her life and she is enthusiast to achieve them.

She knows how to be beside me.

I know that you are so happy now and that you would be giving me tons of advises to take care of her 🙂

Categories: Life

مهما كان صغيراً

أحياناً ما نتراجع عن فعل خيراً ما ظناً منا أنه صغيراً وربما يكون غير مجدي أو ليس بالمنفعة الكبيرة

 وأحياناً أخرى نقع في ذنباً ما غير مكترثين ظناً منا أنه صغيراً وليس بالأمر الذي يجب أن نقلق منه

وقديماً قال المتنبي لاتحقرنّ صغيرة إن الجبال من الحصى

 فإن ذنباً أو معروفاً صغيراً قد يكون ما يفصل بين دخولنا الجنة أو النار يوم لا ينفع الندم شيئاً

وإن ذنباً صغيراً قد يكون باب لذنبٍ أكبر فأكبر ومعروفاً صغيراً قد يكون عند الله أكبر وأكبر

فلا تحقرن ذنباً

حكي عن بعض العارفين انه كان يمشي في الوحل جامعاً ثيابه محتزرا عن زلقة رجليه ومع ذلك فقد زلقت رجله وسقط واتسخت ثيابه فقام يمشي وسط الوحل يبكي ويقول : هذا مثل العبد لا يزال يتوقى الذنوب ويجانبها حتى يقع في ذنب او ذنبين فعندها يخوض في الذنوب جميعا

ولا تحقرن معروفاً

يقول رسولنا الكريم صلى الله عليه وسلم : ( لا تحقرن من المعروف شيئا ، ولو أن تلقى أخاك بوجه طلق ) صحيح مسلم

 حتى الكلمة

يقول رسول الله صلى الله عليه وسلم: ( إِنَّ أَحَدَكُمْ لَيَتَكَلَّمُ بِالْكَلِمَةِ مِنْ رِضْوَانِ اللَّهِ ، مَا يَظُنُّ أَنْ تَبَلُغَ مَا بَلَغَتْ ، فَيَكْتُبُ اللهُ بِهِ رِضْوَانَهُ إِلَى يَوْمِ يَلَقْاهُ ، وَإِنَّ أَحَدَكُمْ لَيَتَكَلَّمُ الْكَلِمَةَ مِنْ سَخَطِ اللَّهِ ، مَا يَظُنُّ أَنْ تَبْلُغَ الَّذِي بَلَغَتْ ، فَيَكْتُبُ اللهُ بِهَا سَخَطَهُ إِلَى يَوْمِ يَلْقَاهُ ) صحيح الترمذي


Categories: Life

What a teacher can make [Video]

This is a very emotional and touching video about how much difference a teacher can make to a student’s life:

Yet another impressive movie is that one based on “Three Letters from Teddy” story .. Watch it here: http://www.makeadifferencemovie.com/.

Categories: Life Tags: , ,

XOR – The Interesting Gate

April 25, 2011 2 comments

Note: The interesting features in XOR and XNOR are somehow the same but with small difference, I’ll speak in details here about XOR and will provide another article for XNOR later.

XOR Symbol Red

Among other logic gates, XOR and XNOR are  interesting gates having some unique features.

Multi-input XOR

All 2-input logic gates have the same meaning when they have more than 2 inputs. For example an AND gate is a gate that outputs 1 when all its inputs are 1, an OR gate outputs 1 when any of the inputs is 1, a NAND gate outputs 0 when all its inputs are 1, a NOR gate outputs 1 when all its inputs are 0.

A 2-input XOR gate outputs 1 when there’s exactly a single 1 at the inputs which means it’s exclusively there and that’s from where the name XOR (Exclusive OR) comes. We can alternatively say that it outputs 1 when the 2 inputs are different.

A multi-input XOR gate however doesn’t necessarily have the same meaning as the 2-input XOR above. There’re two different interpretations for a multi-input XOR and let’s check that on a 3-input XOR as an example:

3-input XOR

Read the full post…

Abosolute vs. Relative Path

March 12, 2010 2 comments

When we create a web-page at which there are some links (URLs) to images or other pages, links are written either in an absolute or relative way (commonly known as absolute and relative paths).

Suppose that we’ve a website whose domain name is http://www.shoubraeng.com and there’s a web-page that’s in the directory http://www.shoubraeng.com/pages/project/index.html.

Let’s also assume that there’s a subdirectory of the project directory called images.

Here’s a description of the hierarchy (directories are written in bold):

|___ pages

____|____ picture1.jpg

____|____ project

_________|_____ picture2.jpg

_________|_____ index.html

_________|_____ images

_______________|_____ picture3.jpg

The index.html page is the web-page of interest. We’re trying to make a background picture to it from the available 3 pictures. picture1.jpg is in the pages directory, picture2.jpg is in the project directory and picture3.jpg is in the images directory.

Now let’s see the different ways to link to any of these pictures.

Absolute Path

The easy way is to use the absolute/full path of the image that looks like:

<body background=”http://www.shoubraeng.com/pages/picture1.jpg“> or

<body background=”http://http://www.shoubraeng.com/pages/project/images/picture3.jpg“>

You might have guessed that it’s called absolute/full because it contains the very specific path to the image.

One more note about absolute paths is that if the file is on the same domain of your web-page then you may not write the domain name but just start with a slash (/) like:

<body background=”/pages/picture1.jpg“>

Relative Path

It’s called relative because it’s described relative to the path of your web-page regardless of its position.

1- If you are linking to picture2.jpg that’s in the same directory of your index.html web-page then – using relative path – you may just write its name as follows:

<body background=”picture2.jpg“>

Writing only the name of the image in the path tells the browser that it’s in the same directory of the web-page that is being loaded now.

2- If you are linking to picture3.jpg that’s in a subdirectory of your index.html webp-page, it will then look like:

<body background=”images/picture3.jpg“>

It should also be noticeable that starting with a directory name (with no / at the beginning) tells the browser to find the image in the subdirectory located in the directory of the web-page that’s being loaded now.

3- If you are linking to picture1.jpg that’s up on directory from the web-page it will then look like:

<body background=”../picture1.jpg“>

The double dots (..) here tells the browser to go up on directory to find the image.

Which is better … absolute or relative path?

Each is preferred at certain conditions.

Absolute path is better when what you link to is used in multiple pages across your website so that it doesn’t matter where your page is. Suppose that there’s a banner image that you use for all pages in your website … is it good to replicate the image in every folder of your website to use a relative path of it in every web-page there? Of course not.

Relative path is better when what you link to is very specific to the web-page and is located in the same directory containing that web-page or a subdirectory of it. Suppose a situation where you are writing an article web-page that contains many related pictures inside it. You’ve made a directory containing your article and a subdirectory containing all the article-related images. Now you are going to link to these images using either absolute or relative paths and you wonder which is better! Ask yourself the following question and it should reply you …. what if you later moved the article to a different domain? If were’re linking to images in an absolute way then you will have to change all paths again to the new domain but if you were using relative paths then you will not have to change anything.

This is because what is relative here is relative there!

Ahmed Abdullah Hussein

Microcontroller

August 30, 2009 5 comments

This is my first experience with Microcontrollers (µC).

Before I know about µC, I’ve already had a lot of experience about computer architecture and the story of running a software on a PC. I’ve then attended a summer training introducing Microcontrollers with a hands-on practice. Let’s get started…

microcontroller_ATMega32

What is a Microcontroller?

Microcontroller is actually a computer but a small one. It’s simply a computer on a chip. It has all the 3 main elements of any stored-program computer system. It has a processor, a storage memory and some I/O facilities.

Read more…

What is a Software Technology?

August 6, 2009 2 comments

What is a technology?

Here’s how Wikipedia defines technology:

Technology is a broad concept that deals with an animal species’ usage and knowledge of tools and crafts, and how it affects an animal species’ ability to control and adapt to its environment.

Designer thinking

Arrr … What animals!!!

But that is not what I was searching for. I was searching for that word “technology” that I always see in my study about Computer Software. I heard for example about Java technology, .NET technology, JDBC technology and many others.

Today I could make a very close definition of what is a technology when it’s related to software.

So the question now should be: What is a software technology?

I think that a software technology is simply a set of software libraries or classes (from an OO view) written in some programming languages to facilitate the ability to manipulate something. Having such technology should provide abstraction or hide the very specific details of dealing with this thing to make it easier for the developer.

I’ll say that Java, the programming language., is a software technology indeed.

Another example that I’ve read about it today and gave me the intuition about this topic is JDBC or Java DataBase Connectivity. It’s mentioned that JDBC is a technology that enables database access and manipulation of different database drivers like those for MySql, Oracle, MSSQL, … while JDBC is simply some software classes written in java!

Designer workstation after

Got it!!!

One more thing … Don’t you think that there’s some similarity between a software technology the way I defined it and a general technology definition as Wikipedia mentioned?

Wikipedia said it’s a Concept or set of concepts and I’ve said it’s a a set of Classes.  Do you know what’s a class in software? it is actually an abstraction of a Concept. There’s even more similarity between the two definitions but I’ll leave it to you as a homework 😉

– Ahmed Abdullah Hussein

Categories: Software Develeopment