Java通过freeSSHd.exe软件远程调用计算机执行cmd命令(在下面两个方法里,我执行的是重启系统的命令)
freeSSHd软件下载地址: http://www.freesshd.com/?ctt=download
开始安装freeSSHd:
双击开始安装,安装过程中会提示两个选项
1.Private keys should be created. Should I do it now??
这是在询问现在是否创建私钥,选是。
2.Do you want to run FreeSSHd as a system service?
这是在询问是否开启freeSSHd服务,选是。
freeSSHd配置:
双击桌面上的freeSSHd或者从开始菜单栏伤的freeSSHd并不会打开软件,这是因为软件已经启动并隐藏在右下角处。
在桌面右下角右击freeSSHdService,选择settings进行配置。
1.首先配置Server status
找到Server status选项卡,SSH server is running. 默认是红叉,点击下方 Click here to start it.启动服务,红叉会变成绿勾
如果没有变成绿勾,并报错“the specified address is already in use”,这是因为服务在上面已经开启,需要关闭。win + r 输入services.msc 找到freeSShd的服务关闭,
关闭之后重启点击Click here to start it启动服务,这时候发现服务已经启动。
????????2.配置用户
找到Users选项卡,点击add添加一个用户
Lodin:用户名
authorization:选择 Password stored as SHA1 hash,
password:密码,和下一行的确认密码
注意:最下面三个全部要勾选.
3.配置端口
找到SSH选项卡
listen address:监听的地址(本机ip)
port:监听的端口,端口号默认22尽量不要修改
max number of connections:最大连接数自行配置,
两个方法使用的是同一个jar包:
<!--Java通过freeSSHd远程调用计算机执行cmd命令,引入的jar包-->
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>262</version>
</dependency>
方法1:
package com.ruoyi.web.controller.dl;
import java.io.IOException;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class SysProcessHelp {
public static void main(String[] args) {
// 服务器ip
String ipv4Address = "192.168.43.113";
// freeSshd设置的连接端口
int port = 22;
String freeSshdUserName = "yan";
String freeSshdUserPassword = "123";
// 在cmd中执行的命令
String command = "shutdown /r";
connectServer(ipv4Address, port, freeSshdUserName, freeSshdUserPassword, command);
}
public static void connectServer(String ipv4Address, int port, String freeSshdUserName, String freeSshdUserpassword, String command) {
Connection conn = new Connection(ipv4Address, port);
Session session = null;
try {
conn.connect();
// login
boolean isLogin = conn.authenticateWithPassword(freeSshdUserName, freeSshdUserpassword);
if (isLogin) {
System.out.println("登录成功");
} else {
System.out.println("登录失败");
}
Session openSession = conn.openSession();
openSession.execCommand(command);
InputStream is = new StreamGobbler(openSession.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(is, "GBK"));
while (true) {
String line = br.readLine();
if (line == null) {
break;
}
System.out.println(line);
}
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}
if (conn != null) {
conn.close();
}
}
}
}
方法2:
package com.ruoyi.web.controller.dl;
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 SSHWindows {
public static void main(String[] args) {
// TODO Auto-generated method stub
String hostname ="192.168.43.113";
String username="yan";
String password="123";
try{
//建立连接
Connection conn= new Connection(hostname);
// System.out.println("set up connections");
conn.connect();
//利用用户名和密码进行授权
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
if(isAuthenticated ==false)
{
// System.out.println("--------");
throw new IOException("Authorication failed");
}
//打开会话
Session sess = conn.openSession();
// System.out.println("cmd----");
//执行命令
sess.execCommand("shutdown /r");
// System.out.println("The execute command output is:");
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);
}
// System.out.println("退出代码 "+sess.getExitStatus());
sess.close();
conn.close();
// System.out.println("连接关闭");
}catch(IOException e)
{
System.out.println("无法访问远程机器");
}
}
}
|