Chinaunix首页 | 论坛 | 博客
  • 博客访问: 93508
  • 博文数量: 41
  • 博客积分: 1905
  • 博客等级: 上尉
  • 技术积分: 335
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-10 20:07
文章分类

全部博文(41)

文章存档

2011年(1)

2010年(18)

2009年(22)

我的朋友

分类: Java

2009-07-16 14:56:02

Java - sshtools: read output from a command

package abc;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import com.sshtools.j2ssh.SshClient;
import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
import com.sshtools.j2ssh.configuration.ConfigurationLoader;
import com.sshtools.j2ssh.configuration.SshConnectionProperties;
import com.sshtools.j2ssh.connection.ChannelInputStream;
import com.sshtools.j2ssh.connection.ChannelOutputStream;
import com.sshtools.j2ssh.connection.ChannelState;
import com.sshtools.j2ssh.io.IOStreamConnector;
import com.sshtools.j2ssh.session.SessionChannelClient;
public class Aaa {
 public static void main(String[] args)
 {
     try {
         // JDK > 1.4 ONLY
         /*Handler fh = new FileHandler("example.log");
         fh.setFormatter(new SimpleFormatter());
         Logger.getLogger("com.sshtools").setUseParentHandlers(false);
         Logger.getLogger("com.sshtools").addHandler(fh);
         Logger.getLogger("com.sshtools").setLevel(Level.ALL);*/
         // Configure J2SSH (This will attempt to install the bouncycastle provider
         // under jdk 1.3.1)
         ConfigurationLoader.initialize(false);
         BufferedReader reader =
             new BufferedReader(new InputStreamReader(System.in));
         System.out.print("Connect to host? ");
         String hostname = reader.readLine();
         // Make a client connection
         SshClient ssh = new SshClient();
         ssh.setSocketTimeout(30000);
         SshConnectionProperties properties = new SshConnectionProperties();
         properties.setHost(hostname);
         properties.setPrefPublicKey("ssh-dss");
         // Connect to the host
         ssh.connect(properties);
         // Create a password authentication instance
         PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
         // Get the users name
         System.out.print("Username? ");
         // Read the password
         String username = reader.readLine();
         pwd.setUsername(username);
         // Get the password
         System.out.print("Password? ");
         String password = reader.readLine();
         pwd.setPassword(password);
         // Try the authentication
         int result = ssh.authenticate(pwd);
         // Evaluate the result
         if (result == AuthenticationProtocolState.COMPLETE) {
          SessionChannelClient session = ssh.openSessionChannel();
          InputStream in = session.getInputStream();
          session.executeCommand("/home/amnetdo/filetest /home/amnetdo/agc");
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          StringBuffer buffer = new StringBuffer();
          String line;
          while ((line = br.readLine()) != null) {
          buffer.append(line);
          }
          String out;
          out = buffer.toString();
          System.out.println(out);
         }
       }
       catch (Exception e) {
         e.printStackTrace();
       }
     }
}
阅读(795) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~