skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Monday, October 15, 2012

JAVA GUI to Execute UNIX Commands

Posted by Raju Gupta at 2:00 PM – 0 comments
 
This is a Java based tool which is used to execute UNIX Commands. Its a user friendly tool

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import java.awt.BorderLayout;

public class JAVAGUIUNIXBOX implements ActionListener {

 JFrame frame;
 Container container;
 JLabel storeNo, service, console;
 JTextField storeNumber;
 JButton start, stop, status, clear, lastTx, LoginUsers, preTask, postTask;
 JTextArea output1;
 JScrollPane scroll, scroll1;
 JComboBox services;
 JList serviceList;

 JPanel panel, panel1, panel2, panel3;

 public void Main() {

  frame = new JFrame("JAVAGUIUNIXBOX ");

  storeNo = new JLabel("Store No");
  storeNumber = new JTextField();

  String subject[] = { "All", "Auto Transfer", "Telnet", "Clear RX",
    "IVRD", "EPSQUEUE", "DRXD" };

  start = new JButton("Start");
  stop = new JButton("Stop");
  status = new JButton("Status");
  service = new JLabel("Select Service");
  services = new JComboBox();
  serviceList = new JList(subject);
  clear = new JButton("Clear");
  console = new JLabel("Console");
  output1 = new JTextArea();

  // services.addItem("");

  frame.setLayout(null);
  // frame. showBorder(new TitledBorder("Title"));

  scroll = new JScrollPane(output1);
  scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  // scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

  Border compound = null;

  compound = BorderFactory.createTitledBorder(compound, "",
    TitledBorder.TOP, TitledBorder.BELOW_BOTTOM);

  scroll1 = new JScrollPane(serviceList);
  scroll1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

  storeNo.setBounds(30, 30, 50, 20);

  storeNumber.setBounds(100, 30, 60, 20);
  service.setBounds(180, 30, 85, 20);
  scroll1.setBounds(270, 17, 100, 80);
  start.setBounds(380, 18, 75, 20);
  stop.setBounds(380, 46, 75, 20);
  status.setBounds(380, 74, 75, 20);

  console.setBounds(30, 145, 75, 20);
  scroll.setBounds(27, 120, 430, 155);
  clear.setBounds(380, 280, 75, 20);

  panel = new JPanel();
  panel1 = new JPanel();

  panel3 = new JPanel();

  panel.setBorder(BorderFactory.createTitledBorder(""));
  panel1.setBorder(BorderFactory.createTitledBorder("Console"));

  panel3.setBorder(BorderFactory.createTitledBorder(""));

  // simplePanel.add(new JLabel(""), JLabel.CENTER);

  panel.setBounds(170, 5, 300, 95);
  panel1.setBounds(10, 100, 460, 206);

  panel3.setBounds(10, 5, 155, 95);

  frame.add(storeNo);
  frame.add(storeNumber);
  frame.add(service);
  // frame.add(serviceList);
  frame.add(start);
  frame.add(stop);
  frame.add(status);

  frame.add(clear);

  output1.setEditable(false);
  output1.setLineWrap(true);
  output1.setVisible(true);
  frame.add(scroll);
  frame.add(scroll1);

  frame.add(panel);
  frame.add(panel1);

  frame.add(panel3);

  start.addActionListener(this);
  stop.addActionListener(this);
  status.addActionListener(this);
  clear.addActionListener(this);

  frame.setSize(500, 350);
  frame.setLocation(300, 200);
  frame.setVisible(true);

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 }

 public static void main(String[] args) {

  new JAVAGUIUNIXBOX().Main();

 }

 JSch jsch = new JSch();
 Session session = null;

 @Override
 public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub
  // System.out.println("Inside n"+e.getActionCommand());

  if (e.getActionCommand().equals("Clear")) {

   output1.setText("");
   storeNumber.setText("");
  } else {

   // stNo - Server Name
   String stNo = storeNumber.getText().trim();
   String newSNO = stNo;

   boolean integerFlag = integerCheck(stNo);

   if (integerFlag == true) {

    if (serverStatus(newSNO) == true) {

     // logWriter.writeLog("Initializing the JSCH Object"+newSNO);

     Channel channel = null;
     try {
      channel = session.openChannel("exec");
     } catch (JSchException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
     }

     System.out.println("Initialize");

     String telnetStart = "sudo /usr/local/operations/scripts/./telnet start";
     String telnetStop = "sudo /usr/local/operations/scripts/./telnet stop";
     String telnetStatus = "sudo /usr/local/operations/scripts/./telnet status";

     String autoStart = "sh /usr/pdx/bycom/servers/AUTOTRANSFER.sh start";
     String autoStop = "sh /usr/pdx/bycom/servers/AUTOTRANSFER.sh stop";
     String autoStatus = "sh /usr/pdx/bycom/servers/AUTOTRANSFER.sh status";

     String clearRxStart = "sh /etc/init.d/clearrx start";
     String clearRxStop = "sh /etc/init.d/clearrx stop";
     String clearRxStatus = "sh /etc/init.d/clearrx status";

     String ivrdStart = "sh /usr/pdx/bycom/servers/IVRD.sh start";
     String ivrdStop = "sh /usr/pdx/bycom/servers/IVRD.sh stop";
     String ivrdStatus = "sh /usr/pdx/bycom/servers/IVRD.sh status";

     String epsStart = "sh /usr/pdx/bycom/servers/EPSQUEUE.sh start";
     String epsStop = "sh /usr/pdx/bycom/servers/EPSQUEUE.sh stop";
     String epsStatus = "sh /usr/pdx/bycom/servers/EPSQUEUE.sh status";

     String drxdStart = "sh /usr/pdx/bycom/servers/DRXD_RX.sh start";
     String drxdStop = "sh /usr/pdx/bycom/servers/DRXD_RX.sh stop";
     String drxdStatus = "sh /usr/pdx/bycom/servers/DRXD_RX.sh status";

     Object o[] = serviceList.getSelectedValues();

     if (o.length == 0) {
      JOptionPane
        .showMessageDialog(null,
          "Please Select Atleast One Service",
          "Error", 1);
     } else {
      if (e.getActionCommand().equals("Start")) {

       try {
        channel = session.openChannel("exec");
       } catch (JSchException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
       }

       int count = 0;
       int i = 0;
       ;

       System.out.println(o.length);

       if (o.length == 1
         && o[i].toString().equalsIgnoreCase("All")) {
        i = 0;

        ((ChannelExec) channel).setCommand(telnetStart
          + ";" + autoStart + ";" + clearRxStart
          + ";" + ivrdStart + ";" + epsStart
          + ";" + drxdStart);
        count++;

       }

       else {

        System.out.println("Main");
        String manyServices = "", service = "";
        for (int j = 0; j < o.length; j++) {

         if (o[j].toString().equalsIgnoreCase(
           "Telnet")) {
          service = telnetStart;
         }
         if (o[j].toString().equalsIgnoreCase(
           "Auto Transfer")) {
          service = autoStart;
         }
         if (o[j].toString().equalsIgnoreCase(
           "Clear Rx")) {
          service = clearRxStart;
         }
         if (o[j].toString()
           .equalsIgnoreCase("IVRD")) {
          service = ivrdStart;
         }
         if (o[j].toString().equalsIgnoreCase(
           "EPSQUEUE")) {
          service = epsStart;
         }
         if (o[j].toString()
           .equalsIgnoreCase("DRXD")) {
          service = drxdStart;
         }
         manyServices += service + ";";

        }
        ((ChannelExec) channel)
          .setCommand(manyServices);
        System.out.println(manyServices);

       }

       // ;ps -ef | grep mediture
       channel.setInputStream(null);

       ((ChannelExec) channel).setErrStream(System.err);

       try {
        channel.connect();
       } catch (JSchException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
       }

       InputStream in = null;
       try {
        in = channel.getInputStream();
       } catch (IOException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
       }

       byte[] tmp = new byte[1024];
       String output = null;
       while (true) {
        try {
         while (in.available() > 0) {
          int i1 = in.read(tmp, 0, 1024);
          if (i1 < 0)
           break;
          output = new String(tmp, 0, i1);
          // System.out.print(new String(tmp, 0,
          // i));
         }
        } catch (IOException e1) {
         // TODO Auto-generated catch block
         e1.printStackTrace();
        }
        if (channel.isClosed()) {
         // System.out.println("exit-status: "+channel.getExitStatus());
         break;
        }
        try {
         Thread.sleep(4000);
        } catch (Exception ee) {
        }
       }

       output1.setText(output);
       channel.disconnect();

      }

      if (e.getActionCommand().equals("Stop")) {

       try {
        channel = session.openChannel("exec");
       } catch (JSchException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
       }
       // Object o[]=serviceList.getSelectedValues();
       int count = 0;
       int i = 0;
       ;

       System.out.println(o.length);

       if (o.length == 1
         && o[i].toString().equalsIgnoreCase("All")) {
        i = 0;

        ((ChannelExec) channel).setCommand(telnetStop
          + ";" + autoStop + ";" + clearRxStop
          + ";" + ivrdStop + ";" + epsStop + ";"
          + drxdStop);
        count++;

       }

       else {

        System.out.println("Main");
        String manyServices = "", service = "";
        for (int j = 0; j < o.length; j++) {

         if (o[j].toString().equalsIgnoreCase(
           "Telnet")) {
          service = telnetStop;
         }
         if (o[j].toString().equalsIgnoreCase(
           "Auto Transfer")) {
          service = autoStop;
         }
         if (o[j].toString().equalsIgnoreCase(
           "Clear Rx")) {
          service = clearRxStop;
         }
         if (o[j].toString()
           .equalsIgnoreCase("IVRD")) {
          service = ivrdStop;
         }
         if (o[j].toString().equalsIgnoreCase(
           "EPSQUEUE")) {
          service = epsStop;
         }
         if (o[j].toString()
           .equalsIgnoreCase("DRXD")) {
          service = drxdStop;
         }
         manyServices += service + ";";

        }
        ((ChannelExec) channel)
          .setCommand(manyServices);
        System.out.println(manyServices);

       }

       // ;ps -ef | grep mediture
       channel.setInputStream(null);

       ((ChannelExec) channel).setErrStream(System.err);

       try {
        channel.connect();
       } catch (JSchException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
       }

       InputStream in = null;
       try {
        in = channel.getInputStream();
       } catch (IOException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
       }

       byte[] tmp = new byte[1024];
       String output = null;
       while (true) {
        try {
         while (in.available() > 0) {
          int i1 = in.read(tmp, 0, 1024);
          if (i1 < 0)
           break;
          output = new String(tmp, 0, i1);
          // System.out.print(new String(tmp, 0,
          // i));
         }
        } catch (IOException e1) {
         // TODO Auto-generated catch block
         e1.printStackTrace();
        }
        if (channel.isClosed()) {
         // System.out.println("exit-status: "+channel.getExitStatus());
         break;
        }
        try {
         Thread.sleep(4000);
        } catch (Exception ee) {
        }
       }

       output1.setText(output);
       channel.disconnect();

      }

      if (e.getActionCommand().equals("Status")) {

       try {
        channel = session.openChannel("exec");
       } catch (JSchException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
       }
       // Object o[]=serviceList.getSelectedValues();
       int count = 0;
       int i = 0;
       ;

       System.out.println(o.length);

       if (o.length == 1
         && o[i].toString().equalsIgnoreCase("All")) {
        i = 0;

        ((ChannelExec) channel).setCommand(telnetStatus
          + ";" + autoStatus + ";"
          + clearRxStatus + ";" + ivrdStatus
          + ";" + epsStatus + ";" + drxdStatus);
        count++;

       }

       else {

        System.out.println("Main");
        String manyServices = "", service = "";
        for (int j = 0; j < o.length; j++) {

         if (o[j].toString().equalsIgnoreCase(
           "Telnet")) {
          service = telnetStatus;
         }
         if (o[j].toString().equalsIgnoreCase(
           "Auto Transfer")) {
          service = autoStatus;
         }
         if (o[j].toString().equalsIgnoreCase(
           "Clear Rx")) {
          service = clearRxStatus;
         }
         if (o[j].toString()
           .equalsIgnoreCase("IVRD")) {
          service = ivrdStatus;
         }
         if (o[j].toString().equalsIgnoreCase(
           "EPSQUEUE")) {
          service = epsStatus;
         }
         if (o[j].toString()
           .equalsIgnoreCase("DRXD")) {
          service = drxdStatus;
         }
         manyServices += service + ";";

        }
        ((ChannelExec) channel)
          .setCommand(manyServices);
        System.out.println(manyServices);

       }

       // ;ps -ef | grep mediture
       channel.setInputStream(null);

       ((ChannelExec) channel).setErrStream(System.err);

       try {
        channel.connect();
       } catch (JSchException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
       }

       InputStream in = null;
       try {
        in = channel.getInputStream();
       } catch (IOException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
       }

       byte[] tmp = new byte[1024];
       String output = null;
       while (true) {
        try {
         while (in.available() > 0) {
          int i1 = in.read(tmp, 0, 1024);
          if (i1 < 0)
           break;
          output = new String(tmp, 0, i1);
          // System.out.print(new String(tmp, 0,
          // i));
         }
        } catch (IOException e1) {
         // TODO Auto-generated catch block
         e1.printStackTrace();
        }
        if (channel.isClosed()) {
         // System.out.println("exit-status: "+channel.getExitStatus());
         break;
        }
        try {
         Thread.sleep(4000);
        } catch (Exception ee) {
        }
       }

       output1.setText(output);
       channel.disconnect();

      }

     }

    }

    else {
     JOptionPane.showMessageDialog(null, "Server offline",
       "Error", 1);
     output1.setText("");
     storeNumber.setText("");
    }

   } else {
    JOptionPane.showMessageDialog(null, "Invalid Store Number",
      "Error", 1);
    output1.setText("");
    storeNumber.setText("");
   }

  }
 }

 public boolean integerCheck(String stNo) {

  boolean check;

  String num = "[\p{Digit}&&[1234567890]]+";

  if (stNo.length() == 4) {

   if (stNo.matches(num)) {

    check = true;
   } else {

    check = false;
   }

  } else {

   check = false;

  }

  return check;
 }

 public boolean serverStatus(String newSNO) {

  String userName = "userName";
  String password = "password1 ";
  String password1 = "password2";
  // logWriter.writeLog("Creating the session object"+userName+" "+newSNO+" "+password);
  System.out.println("newSNO" + newSNO);
  try {
   session = jsch.getSession(userName, newSNO, 22);
  } catch (JSchException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
  // logWriter.writeLog("Setting the Password");
  session.setPassword(password);

  // jsch.addIdentity("id_rsa");
  // System.out.println("identity added ");

  java.util.Properties config = new java.util.Properties();
  config.put("StrictHostKeyChecking", "no");
  // logWriter.writeLog("Setting config key");
  session.setConfig(config);

  boolean status;

  try {
   session.connect();
   status = true;
  } catch (JSchException e1) {
   // TODO Auto-generated catch block

   status = false;
   // session.disconnect();
   e1.printStackTrace();
  }

  if (status == false) {

   try {
    session = jsch.getSession(userName, newSNO, 22);
   } catch (JSchException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
   // logWriter.writeLog("Setting the Password");
   session.setPassword(password1);

   // jsch.addIdentity("id_rsa");
   // System.out.println("identity added ");

   java.util.Properties config1 = new java.util.Properties();
   config.put("StrictHostKeyChecking", "no");
   // logWriter.writeLog("Setting config key");
   session.setConfig(config1);

   try {
    session.connect();
    status = true;
   } catch (JSchException e1) {
    // TODO Auto-generated catch block

    status = false;
    // session.disconnect();
    e1.printStackTrace();
   }

  }

  return status;
 }
}

Labels: Java Command Example

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger