下面是一个简易的 Java 应用示例,该应用使用 JSch 库来实现远程上传文件以及使用脚本启停远程服务的功能。这个示例包括以下几个部分:
- 建立与远程服务器的 SSH 连接。
- 上传文件到远程服务器。
- 执行远程脚本以启动或停止服务。
首先,确保你的项目中包含了 JSch 库。如果你使用 Maven,请在 pom.xml
文件中添加以下依赖:
xml
com.jcraft
jsch
0.1.55
以下是一个简单的 Java 应用:
java
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class RemoteControlApp {
private String host;
private String user;
private String password;
private JSch jsch;
public RemoteControlApp(String host, String user, String password) {
this.host = host;
this.user = user;
this.password = password;
this.jsch = new JSch();
}
public void uploadFile(String localFilePath, String remoteDirectory) {
Session session = null;
ChannelSftp channelSftp = null;
try {
session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
File file = new File(localFilePath);
String remoteFileName = file.getName();
channelSftp.cd(remoteDirectory);
channelSftp.put(new FileInputStream(file), remoteFileName);
System.out.println("File uploaded successfully to: " + remoteDirectory + remoteFileName);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (channelSftp != null) {
channelSftp.exit();
}
if (session != null) {
session.disconnect();
}
}
}
public void executeRemoteScript(String scriptPath, String action) {
Session session = null;
Channel channel = null;
try {
session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(scriptPath + " " + action);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
channel.connect();
// Wait for the script to complete
while (!channel.isClosed()) {
Thread.sleep(1000);
}
System.out.println("Script executed successfully with action: " + action);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (channel != null) {
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
}
}
public static void main(String[] args) {
RemoteControlApp remoteControlApp = new RemoteControlApp("remote.host.example.com", "username", "password");
// Example usage
String localFilePath = "/path/to/local/file.zip";
String remoteDirectory = "/remote/directory/";
remoteControlApp.uploadFile(localFilePath, remoteDirectory);
String scriptPath = "/path/to/remote/script.sh";
String action = "start"; // or "stop"
remoteControlApp.executeRemoteScript(scriptPath, action);
}
}
在这个应用中,uploadFile
方法用于上传文件到远程服务器,而 executeRemoteScript
方法用于执行远程脚本,其中 action
参数可以是 “start” 或 “stop”,取决于你想要执行的操作。
确保你的远程脚本(script.sh
)能够接收一个参数(例如 “start” 或 “stop”),并根据该参数执行相应的操作。以下是一个简单的脚本示例:
bash
#!/bin/bash
script.sh
ACTION=$1
SERVICE_NAME=“your_service_name”
case $ACTION in
start)
systemctl start $SERVICE_NAME
;;
stop)
systemctl stop $SERVICE_NAME
;;
*)
echo “Usage: $0 {start|stop}”
exit 1
;;
esac
请确保远程脚本具有执行权限,并且路径正确。在实际部署时,你需要替换主机名、用户名、密码、本地文件路径、远程目录和远程脚本路径等参数。此外,考虑到安全性和稳定性,建议使用密钥认证而不是密码认证。