1.第一种
pom文件
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>build210</version>
</dependency>
java
脚本名为advertising_generate.sh,替换ip,port,username和password
public static String processStdout(InputStream in, String charset) {
InputStream stdout = new StreamGobbler(in);
StringBuffer buffer = new StringBuffer();
;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(stdout, charset));
String line = null;
while ((line = br.readLine()) != null) {
buffer.append(line + "\n");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer.toString();
}
public static void main(String[] args) throws IOException, JSchException {
Connection connection = null;
try {
connection = new Connection(ip, port);
//连接
connection.connect();
//身份验证
boolean isAuthenticated = connection.authenticateWithPassword(username, password);
if (isAuthenticated) {
System.out.println("连接成功");
Session session = null;
try {
session = connection.openSession();
//把命令写入脚本,在执行该脚本
//session.execCommand("sh /root/aaa.sh" + "a b");
session.execCommand("sh /home/nginx/advertising_generate.sh "+"202312281519 https://jm-live-video-dev.oss-cn-shanghai.aliyuncs.com/goods/101164/c14619cb-4fd8-4d45-8f97-428f042bf81344.jpg");
String result = processStdout(session.getStdout(), DEFAULTCHART);
//如果为得到标准输出为空,说明脚本执行出错了
if (result == null) {
result = processStdout(session.getStderr(), DEFAULTCHART);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
connection.close();
}
} else {
throw new Exception("身份验证失败");
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
第二种:
pom文件
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>
java
public static String exeCommand(String host, int port, String user, String password, String command) throws JSchException, IOException {
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setConfig("StrictHostKeyChecking", "no");
// java.util.Properties config = new java.util.Properties();
// config.put("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
InputStream in = channelExec.getInputStream();
channelExec.setCommand(command);
channelExec.setErrStream(System.err);
channelExec.connect();
String out = IOUtils.toString(in, "UTF-8");
channelExec.disconnect();
session.disconnect();
return out;
}
}
main方法
public static void main(String[] args) throws IOException, JSchException {
String host = ip;
int port = port;
String user = user ;
String password = password ;
String command = "sh /home/nginx/advertising_generate.sh \"+\"202312281519 https://zpdt-dev.oss-cn-hangzhou.aliyuncs.com/goods/1/99ccbc1e-f20e-4290-a774-e03274e651ad.jpg";
String res = exeCommand(host,port,user,password,command);
System.out.println(res);
}
第三种
pom
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>
java
public static void main(String[] args) throws IOException, JSchException {
Session session = JschUtil.openSession( ip, port, username, password);
String cmd="sh /home/nginx/advertising_generate.sh 202312281519"+"https://zpdt-dev.oss-cn-hangzhou.aliyuncs.com/goods/1/99ccbc1e-f20e-4290-a774-e03274e651ad.jpg";
String result = JschUtil.exec(session,cmd, CharsetUtil.CHARSET_UTF_8);
System.out.println(result);
}