Hello again
this library has been written to help accessing Unix\ Linux servers with private key. it needs ganymed-ssh2-build210.jar to work
package lib;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class SSHLib
{
static String sshPrivateKey;
static Connection conn = null;
static Session session = null;
static String sshUsername;
String output ="";
public SSHLib() throws IOException
{
init();
}
private void init() throws IOException{
char privateKeyChar[] = sshPrivateKey.toCharArray();
conn = new Connection(ftpHostname,22);
conn.connect();
boolean isAuthenticated = conn.authenticateWithPublicKey(sshUsername, privateKeyChar, null);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
session = conn.openSession();
}
public String excuteCMD(String cmd) throws IOException
{
session = conn.openSession();
session.execCommand(cmd);
InputStream stdout = new StreamGobbler(session.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true)
{
String line = br.readLine();
if (line == null)
break;
else
output+=line;
}
return output;
}
public Integer getExitStatus()
{
return session.getExitStatus();
}
public void disconnect()
{
session.close();
conn.close();
}
public String getSize(String ftpAccount , String path) throws IOException {
return excuteCMD("du -sh " + path + "|" + "awk '{print $1}'" );
}
public void deleteFolder(String path) throws IOException{
excuteCMD("rm -rf " + path);
}
public static void main(String[] args) throws IOException {
SSHLib ssh = new SSHLib();
Integer x = ssh.getExitStatus();
//System.out.println("exit status :" + x);
System.out.println(ssh.getSize(null, "/home/"));
ssh.disconnect();
}
}
No comments:
Post a Comment