Ganymede SSH: Executing multiple commands in a single session. The Easy Way

Download the Ganymede ssh jar from www.ganymed.ethz.ch/ssh2/



The below given program acts as if you are working with putty console. The text in bold are doing the input and output work. With the given format of the program, it is highly customizable.

The program goes like this:-



import java.awt.Color;import java.awt.FlowLayout;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.io.BufferedReader;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.util.Scanner;import javax.swing.BoxLayout;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPasswordField;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.SwingUtilities;

import ch.ethz.ssh2.Connection;import ch.ethz.ssh2.InteractiveCallback;import ch.ethz.ssh2.KnownHosts;import ch.ethz.ssh2.ServerHostKeyVerifier;import ch.ethz.ssh2.Session;import ch.ethz.ssh2.StreamGobbler;

 class MySessionConnector extends Thread { String hostname; String username;
InputStream in =null; OutputStream out =null; InputStream err = null;   public static void main(String[] args)   {   new MySessionConnector();   } public MySessionConnector() { this.hostname = "server"; this.username = "user"; String password = "password"; try { /* Create a connection instance */
Connection conn = new Connection(hostname); /* Now connect */ conn.connect(); boolean isAuthenticated = conn.authenticateWithPassword(username, password); if (isAuthenticated == false) throw new IOException("Authentication failed."); /* Create a session */
Session sess = conn.openSession(); sess.requestPTY("dumb", 90, 30, 0, 0, null); sess.startShell(); Scanner scan = new Scanner(System.in); String myinput = null; in = sess.getStdout(); out = sess.getStdin(); err = sess.getStderr(); String myCommand = null; //new RemoteErrorConsumer().start(); new RemoteConsumer().start(); char[] carr = null; while(!(myCommand = scan.nextLine()).equalsIgnoreCase("Exit")) { carr = myCommand.toCharArray() ; for(int i=0;i<carr.length;i++) { out.write(carr[i]); } out.write(13); } } catch(Exception e1) { e1.printStackTrace(); } }//Constructor Ends


class RemoteConsumer extends Thread { public void run() { try{ byte[] buff = new byte[8192]; int len = 0; int i=0; while (true) { len = in.read(buff); for(i=0;i<len;i++) { System.out.print((char)buff[i]); }    }
} catch(Exception e1) { e1.printStackTrace(); } }//run ends }//class RemoteConsumer Ends class RemoteErrorConsumer extends Thread {        public void run()        {         try{ byte[] buff = new byte[8192]; int len = 0; int i=0; while (true) { len = err.read(buff); for(i=0;i<len;i++) { System.out.print((char)buff[i]); }    }
} catch(Exception e1) { e1.printStackTrace(); }        } } }

1 comment: