Skip to main content

Java functions to run SSH commands from java


     public static void SSH(String Host,String username,String passwor,String cmd,String cmd1) {
 
byte[] tmp = new byte[1024];
String host =Host;
String user =username;
String password =passwor;
String  command = cmd;
                String ou=null;
try {
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
// Create a JSch session to connect to the server
Session session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
// Establish the connection
session.connect();
System.out.println("Connected...");

ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(command);
channel.setErrStream(System.err);
                        channel.connect();
                       
                        channel.setCommand(cmd1);
channel.setErrStream(System.err);
channel.connect();

InputStream in = channel.getInputStream();
                       
channel.connect();

while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0) {
break;
}
                                     
      System.out.print(new String(tmp, 0, i));
                                       
                                       ou=new String(tmp); 
                                   
}
if (channel.isClosed()) {
System.out.println("Exit Status: "
+ channel.getExitStatus());
break;
}
Thread.sleep(1000);
}
channel.disconnect();
session.disconnect();
System.out.println("DONE!!!");
} catch (Exception e) {
e.printStackTrace();
}
               
}

Comments

Popular posts from this blog

Encryption and Decryption functions for Java

    public static String encrypt(String key, String initVector, String value) {         try {             IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));             SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");             Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");             cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);             byte[] encrypted = cipher.doFinal(value.getBytes());             System.out.println("encrypted string: "                     + Base64.encodeBase64String(encrypted));             return Base64.encodeBase64String(encrypted);         } catch (Exception ex) {   ...

Disabling DEP(data Excecution prevention) for third party softwares in windows

1.go to system properties. 2.go to Advanced system settings . 3. go to Advance tab ,performance settings,Data execution prevention tab make sure u have selected 1st option as highlighted in screenshot.after making changes you must restart system

java function to write into sql database from java

public static void runsql(String Q)   {     try     {       // create a mysql database connection           String myUrl = "jdbc:mysql://localhost:3306/firewall?zeroDateTimeBehavior=convertToNull";       Class.forName("com.mysql.jdbc.Driver");       Connection conn = DriverManager.getConnection(myUrl,"root", "password");       if (conn != null) {     System.out.println("Connected"); }       Statement st = conn.createStatement();             st.executeUpdate(Q);       conn.close();     }     catch (Exception e)     {       System.err.println("Got an exception!");       System.err.println(e.getMessage());     }   }