//import org.apache.log4j.BasicConfigurator;
//import org.apache.log4j.PatternLayout;
//import org.apache.log4j.RollingFileAppender;
package com.simoncat.net;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import com.sshtools.j2ssh.io.IOStreamConnector;
import com.sshtools.j2ssh.io.IOStreamConnectorState;
import com.sshtools.j2ssh.connection.*;
import com.sshtools.j2ssh.SshClient;
import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
import com.sshtools.j2ssh.session.SessionChannelClient;
import com.sshtools.j2ssh.configuration.SshConnectionProperties;
import com.sshtools.j2ssh.transport.ConsoleKnownHostsKeyVerification;
//import com.sshtools.common.hosts.ConsoleHostKeyVerification;
import com.sshtools.j2ssh.transport.IgnoreHostKeyVerification;
public class MySSHClient {
SshClient ssh = null;
SshConnectionProperties properties = null;
SessionChannelClient session = null;
public MySSHClient(String hostName,int port, String userName, String passwd ){
try{
// Make a client connection
ssh = new SshClient();
properties = new SshConnectionProperties();
properties.setHost(hostName);
if(port != 0)
properties.setPort(port);
// Connect to the host
//ssh.connect(properties, new ConsoleKnownHostsKeyVerification());
ssh.connect(properties,new IgnoreHostKeyVerification());
// Create a password authentication instance
PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
pwd.setUsername(userName);
pwd.setPassword(passwd);
// Try the authentication
int result = ssh.authenticate(pwd);
// Evaluate the result
if (result==AuthenticationProtocolState.COMPLETE) {
System.out.println("Connection Authenticated");
}
}
catch(Exception e){
System.out.println("Exception : " + e.getMessage());
}
}//end of method.
public String execCmd(String cmd){
String theOutput = "";
try
{
// The connection is authenticated we can now do some real work!
session = ssh.openSessionChannel();
if ( session.executeCommand(cmd)){
IOStreamConnector output = new IOStreamConnector();
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
output.connect(session.getInputStream(), bos );
session.getState().waitForState(ChannelState.CHANNEL_CLOSED);
theOutput = bos.toString();
//System.out.println();
}
//else
//throw Exception("Failed to execute command : " + cmd);
//System.out.println("Failed to execute command : " + cmd);
}
catch(Exception e){
System.out.println("Exception : " + e.getMessage());
}
return theOutput;
}
public static void main(String arg[]){
//../webxmlxslt_src/libs/j2ssh
//MySSHClient sshc = new MySSHClient("192.168.1.95",0,"emecas","linux");
//System.out.println( sshc.execCmd("ls -l") );
//System.out.println( sshc.execCmd("df -k") );
//System.out.println( sshc.execCmd("who") );
//System.out.println( sshc.execCmd("reboot now") );
//System.out.println( sshc.execCmd("sudo -i") );
//System.out.println( sshc.execCmd("linux") );
//System.out.println( sshc.execCmd("ls") );
MySSHClient sshc = new MySSHClient("reportes.umng.edu.co",1947,"root","nemesis1");
System.out.println( sshc.execCmd("reboot now") );
}
}