java应用程序远程登录linux并执行其命令(ssh jar包)
2014-05-19 18:59
806 查看
http://www.ganymed.ethz.ch/ssh2/在这个网址下载一个调用ssh和scp命令的jar包。
然后,就可以写程序了。将上面的jar包导入MyEclipse,下面是一个类的实例代码。
package hh;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class Basic
{
public static void main(String[] args)
{
String hostname = "222.177.211.9";//要登陆目标主机
String username = "boss";//登陆用的用户名
String password = "qwer@1234" //登陆用到的密码
try
{
/* Create a connection instance */
Connection conn = new Connection(hostname);
/* Now connect */
conn.connect();
/* Authenticate */
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
/* Create a session */
Session sess = conn.openSession();
//sess.execCommand("uname -a && date && uptime && who");
sess.execCommand("ps aux ");
System.out.println("Here is some information about the remote host:");
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true)
{
String line = br.readLine();
if (line == null)
break;
System.out.println(line);
}
/* Show exit status, if available (otherwise "null") */
System.out.println("ExitCode: " + sess.getExitStatus());
/* Close this session */
sess.close();
/* Close the connection */
conn.close();
}
catch (IOException e)
{
e.printStackTrace(System.err); System.exit(2);
}
}
}
在控制台我们将看到输出结果。
然后,就可以写程序了。将上面的jar包导入MyEclipse,下面是一个类的实例代码。
package hh;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class Basic
{
public static void main(String[] args)
{
String hostname = "222.177.211.9";//要登陆目标主机
String username = "boss";//登陆用的用户名
String password = "qwer@1234" //登陆用到的密码
try
{
/* Create a connection instance */
Connection conn = new Connection(hostname);
/* Now connect */
conn.connect();
/* Authenticate */
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
/* Create a session */
Session sess = conn.openSession();
//sess.execCommand("uname -a && date && uptime && who");
sess.execCommand("ps aux ");
System.out.println("Here is some information about the remote host:");
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true)
{
String line = br.readLine();
if (line == null)
break;
System.out.println(line);
}
/* Show exit status, if available (otherwise "null") */
System.out.println("ExitCode: " + sess.getExitStatus());
/* Close this session */
sess.close();
/* Close the connection */
conn.close();
}
catch (IOException e)
{
e.printStackTrace(System.err); System.exit(2);
}
}
}
在控制台我们将看到输出结果。
相关文章推荐
- java通过代码登录远程linux服务器并执行linux命令
- java如何远程登录linux并执行其命令
- java通过ssh2远程连接计算机并执行linux命令的方法(转)
- linux远程自动登录以及执行命令
- Java远程连接Linux系统并执行Linux命令
- java远程执行linux命令
- SSH登录到远程linux机器并执行命令
- 在Windows下用plink工具批量实现自动远程登录Linux并执行关机命令
- JAVA利用SSH2登录LINUX并执行命令
- sshxcute --java远程执行linux/unix命令的工具类
- 利用java实现可远程执行linux命令的小工具
- java远程执行linux命令
- windows 下 java程序jsch连接远程linux服务器执行shell命令
- Java 远程执行 Linux 的命令
- java 使用jsch 远程链接linux执行命令
- Ganymed SSH-2 java执行远程linux机器命令工具
- Linux命令之远程登录与执行远程主机命令
- java 使用jsch 远程链接linux执行命令
- Python 使用paramiko实现远程登录Linux主机并执行命令和操作
- Python远程登录Linux操作系统,执行命令、创建目录、上传及下载文件