Tuesday, May 30, 2017

adding bulk of users GUI interface using Java swing in windows

Now, continuing talking about providing GUI interface to systems operations.

We will dive more into Java and start using the jar file command-factory.jar I mentioned in my previous article to show how useful is to provide GUI interface whether it's for delegation, e.g. you want to delegate specific operation, give access to specific team to extract report(from system not database), enforce standards for the operations conventions/unifying passwords, integrate multiple services together..etc, the list is too long to be mentioned here.

To demonstrate the idea, I will use what I would like to call OAM Object-Administrational Mapping (I just invented that ^_^) which is equivelant to the ORM Object-Relational Mapping. So, I will use command-factory.jar to create small app as "helloworld" to demonstrate the idea of "extending administration capabilities using Java development".

Let us start, after googling for 10 minutes I found the commands to create and delete windows user(normal windows user not a domain user).

in windows cmd:

-net user USERNAME PASSWORD /add

e.g.

-net user testuser Pass_123 /add

to delete:

net user testuser /del

Keep in mind in windows this works in the external commands only(check windows internal vs. external commands), for internal commands it didn't work I guess you need to use "shell" flag somewhere, Windows Powershell or even write python script to do the job for you. Even though with the external commands, probably you will need to use the full executable path (just put the full path it will save you some head scratches).

in windows cmd:

where net

output:
C:\Windows\System32\net.exe

After becoming familiar with the command, go to your IDE netbeans, eclipse..etc. Here, I thought it is better to demonstrate with desktop app instead of web based as it is faster to be used, you know, web app needs application server.

We will follow MVC design pattern which will include Model(user class/entity), View (main console/java swing) and controller(java class to link model and view).

create Java desktop application and give it a name call it UserManager for example, and for the sake of testing we will create main class without GUI/swing. Once application is created add command-factory.jar to the project libraries.


Project includes:

-User.java

package entity;

public class User {
 
    private String name;
    private String password;

    public User() {
    }

    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" + "name=" + name + ", password=" + password + '}';
    }  
}

-Controller.java
package controller;

import UserSAO.UserSAO;
import entity.User;

public class Controller {

    private UserSAO userSAO;
    private User user;

    public Controller() {
        userSAO = new UserSAO();
    }

    public UserSAO getUserSAO() {
        return userSAO;
    }
}

-UserSAO.java

package UserSAO;

import commandfactory.CommandFactory;
import entity.User;
import java.util.ArrayList;
import java.util.List;

public class UserSAO {

    CommandFactory commandFactory;

    public CommandFactory getCommandFactory() {
        return commandFactory;
    }

    public void createUser(User user) {
        List<String> cmd = new ArrayList<>();
        cmd.add("C:\\Windows\\System32\\net.exe");
        cmd.add("user");
        cmd.add(user.getName());
        cmd.add(user.getPassword());
        cmd.add("/add");
        commandFactory = new CommandFactory();
        commandFactory.setCmdList(cmd);
        commandFactory.execCommand();
    }

    public void deleteUser(String name) {
        List<String> cmd = new ArrayList<>();
        cmd.add("C:\\Windows\\System32\\net.exe");
        cmd.add("user");
        cmd.add(name);
        cmd.add("/delete");
        commandFactory = new CommandFactory();
        commandFactory.setCmdList(cmd);
        commandFactory.execCommand();
    }
}

-Main.java

package usermanager;

import controller.Controller;
import entity.User;

public class Main {

    public static void main(String[] args) {

        User user = new User("testuser", "Pass_123");
        Controller controller = new Controller();
        controller.getUserSAO().createUser(user);
        if (controller.getUserSAO().getCommandFactory().getExitCode() == 0) {
            System.out.println(controller.getUserSAO().getCommandFactory().getStdOutPut());
        }
//        controller.getUserSAO().deleteUser("testuser");
//        if (controller.getUserSAO().getCommandFactory().getExitCode() == 0) {
//            System.out.println(controller.getUserSAO().getCommandFactory().getStdOutPut());
//        }
    }
}


The code above is to create a user with password (testuser/Pass_123). To delete a user, you can use the below..

-Main.java

package usermanager;

import controller.Controller;

public class Main {

    public static void main(String[] args) {

//        User user = new User("testuser", "Pass_123");
        Controller controller = new Controller();
//        controller.getUserSAO().createUser(user);
//        if (controller.getUserSAO().getCommandFactory().getExitCode() == 0) {
//            System.out.println(controller.getUserSAO().getCommandFactory().getStdOutPut());
//        }
        controller.getUserSAO().deleteUser("testuser");
        if (controller.getUserSAO().getCommandFactory().getExitCode() == 0) {
            System.out.println(controller.getUserSAO().getCommandFactory().getStdOutPut());
        }
    }
}

Now, I will replace the main class with java swing class and have it so basic to accept user names in TextArea component then create users based on that along with their passwords..

-create JFrame class call it MainFrame

-from swing palette just add by drag and drop two text areas and 2 labels and 1 button
-Declare as instance variable and initialize it in the constructor

Controller controller;
public MainFrame() {
controller = new Controller();
..omitted

it will look like the snapshot

In the actionevent of the Add command you will add the below code

  for (String s: userTextArea1.getText().split("\\n+")) {
            controller.getUserSAO().deleteUser(s);
            if (controller.getUserSAO().getCommandFactory().getExitCode() == 0)
            logArea.append(s+" user is deleted\n");
        }

on windows cmd:
-net user (to check users were created)

while in the action event of Delete command you will put the below code

for (String s: userTextArea1.getText().split("\\n+")) {
            controller.getUserSAO().deleteUser(s);
            if (controller.getUserSAO().getCommandFactory().getExitCode() == 0)
            logArea.append(s+" user is deleted\n");
        }


after creation
-net user (to check users)
after deletion
-net user



jar url https://github.com/samir82show/devops/blob/master/Usertest.zip
use java 8
Samir











Saturday, May 27, 2017

jar file to execute external system commands

Since I like to use command line in the system administration,I realized that having GUI interface is becoming highly required more and more, so, I decided to use Java in my applications whether they are desktop (swing) or web based.

In order to achieve this(execute external scripts/commands), java provides two ways, Runtime and ProcessBuilder.

I decided to use ProcessBuilder to make jar file which one you add it to your library will offer you executing external commands as well as getting the Standard Output, Standard Error and the exit status.

Download here.

Here is how to use it..
  • Add the jar command-factory.jar file to the project library
  • Declare and initialize the class e.g. 
    • CommandFactory processFactory = new CommandFactory();
  • Declare and initialize list of string e.g. 
    • List<String> cmd = new ArrayList<>();
    • cmd.add("python"); 
    • cmd.add("c:\\test.py");
  • Set the command to be executed e.g. 
    • processFactory.setCmdList(cmd);
  • Call execute method e.g. 
    • processFactory.execCommand();
  • You can check the exit status 
    • e.g. processFactory.getExitCode();
  • Standard output e.g. 
    • processFactory.getStdOutPut();
  • Standard error e.g. 
    • processFactory.getStdError();
  • Check the executed command e.g. 
    • processFactory.getCmdList();

Tuesday, December 20, 2016

Avamar automating image level backup

Good whatever time you are in,

In the Avamar as server backup solution, we take Guest level backup, Application (Oracle, SQL server, Exchange..etc.) backups or VM snapshot backup.

Now, for the guest level backup you can use "msiexec ..etc." command to install client and register it with avamar from the client side. But when it comes to VM snapshot backup, it has to be configured from Avamar side. This task is painful if you are migrating from old backup solution like networker to avamar and specially when number of VMs is more than 500.

So, I decided to write script to deploy the VMs accepting the required inputs, searching for the client in the vcenter, adding it and adding the client to the respective group.

The only type of VM clients you can't add is VM client with "spaces" inside the name.

Check and your feedback is appreciated.

Saturday, January 16, 2016

networker backup speed report

To assess the backup performance you need to know the duration and the backup rate, however this is not available in networker command line utilities, so I wrote a script that collect the data a client and date passed as arguments to the script then it will give you the backup details adding the duration of the backup in hours, seconds and the rate MB/s, all you have to do is to fill the backup server name inside the script "backup_server = BKP_SERVER_NAME".

Your feedback is appreciated.

Tuesday, December 29, 2015

VPLEX report

This script is written to report the VPLEX capacity distribution, as a prerequisites use python 3.3 or the minimum version supports "subprocess.check_output", install openpyxl module; then replace this with your credentials, ip and email details

USERNAME = 'UserName'
PASSWORD = 'PassWord'
VPLEX_IP = 'VPLEX_IP'
EMAIL_FROM = 'report@company.com'
EMAIL_TO = 'email@company.com'

your feedback is appreciated,

Wednesday, August 27, 2014

resetting admins passwords in UNIX/Linux using perl

Dears,

this is my first perl script to reset the admins/users to a default password non-interactively, it could be used with the previous shell script to harden new system and add the users as well as resetting their password to default password, you will need to add the encrypted password in ReqPass variable, then the "admins" in the AdminList array variable.

Friday, August 22, 2014

Dears,

I made effort on writing script for hardening our systems, hardening is including

  • disabling the root login
  • setting password complexity and aging
  • setting banner
  • creating the admin users and add them to same group
  • deploying putty public keys for key based login for each user (requires you have the private key in your system)
  • distributing the system keys to enable ssh rsa key based login between the Linux systems
  • stopping some services like iptables, selinux and starting services like vmware tools for the vmware based systems
required:
  1. you should have the public putty keys in the same directory with the script, and the should have the same name convention eg. "samir_public_key", and "samir" should be existing in the list of admins "admin_list".
  2. you should have the private and the public and the private linux keys for linux systems login, eg. "admin3_sys_public_key" and "samir_sys_private_key" respectively.
  3. you should have the "banner" template
  4. you should have "sudoers" template
the directory of the script will look like the same:

admin1_public_key       admin2_public_key       admin3_public_key       admin4_public_key       admin5_public_key       admin6_public_key       banner      sudoers
admin1_sys_private_key  admin2_sys_private_key  admin3_sys_private_key  admin4_sys_private_key  admin5_sys_private_key  admin6_sys_private_key  admin1_sys_public_key   admin2_sys_public_key   admin3_sys_public_key   admin4_sys_public_key   admin5_sys_public_key   admin6_sys_public_key   harden.sh

BEFORE you run the script be sure that you another user who can access the system, as if you execute it without having you private putty keys you will lock yourself out side the box through ssh login.
this is the SCRIPT.
please notify me if you think it needs any modifications