你可以使用JSch
JSch全称是“Java Secure Channel”
是SSH2的一个纯Java实现。它允许你连接到一个sshd 服务器,使用端口转发,X11转发,文件传输等等。同时也是支持执行命令;
以下是大概运行的代码,只是提供大致思路,可以去查官方API和demo
import com.jcraft.jsch.ChannelExecimport com.jcraft.jsch.ChannelSftp
import com.jcraft.jsch.ChannelSftp.LsEntry
import com.jcraft.jsch.JSch
import com.jcraft.jsch.JSchException
import com.jcraft.jsch.Session
import com.jcraft.jsch.SftpATTRS
import com.jcraft.jsch.SftpException
.......
try{
Session session = new 指培颤JSch().getSession(user, ip, port)
session.setPassword(pwd)
session.setConfig("StrictHostKeyChecking", "no")
session.setConfig("userauth.gssapi-with-mic", "no")
session.connect()
ChannelExec exec = (ChannelExec) session.openChannel("exec")
exec.setCommand("ifconfig")//这里是你要执唯败行的命令,部分命令中敏不支持,具体自己执行下
ByteArrayOutputStream bao = new ByteArrayOutputStream()
exec.setOutputStream(bao)
ByteArrayOutputStream baerr = new ByteArrayOutputStream()
exec.setErrStream(baerr)
exec.connect()
while (!exec.isEOF())
String errmsg = new String(baerr.toByteArray(), "utf-8")
if (StringUtils.notNull(errmsg)) {
throw new RuntimeException(errmsg)
} else {
System.out.println(new String(bao.toByteArray(), "utf-8"))
}
}catch(Exception e){
e.printStackTrace()
}finally{
//关闭session等 *** 作
}
1) 在ECLIPSE下面EXPORT成为RUNNABLE JAR FILE。2)拷贝1)中生成的JAR包,到LINUX中去。
3)随便搞个什么名字的.SH文件,岩迟里简改面输入粗咐李java -jar XXX.jar
package com.cn3158.base.utilsimport java.io.BufferedReader
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.io.OutputStream
import java.util.ArrayList
import java.util.List
import org.apache.log4j.Logger
public class ProcessExecutor {
private static Logger logger = Logger.getLogger(ProcessExecutor.class)
public static boolean isSubProcessOutput = true
private static ProcessExecutor instance = new ProcessExecutor()
private ProcessExecutor() {
super()
}
public static ProcessExecutor getInstance()
{
return instance
}
public Integer executeProcess(List<String>commands) {
Integer result = null
ProcessBuilder pb = new ProcessBuilder(commands)
// 开启错胡返御误信息的流世销到标准输出流,在某种情况下由于错误输出流中的数据没有被读取,进程就不会结束
pb.redirectErrorStream(true)
try {
//启动进程
Process process = pb.start()
//logger.info("begin to start process "+process.hashCode()+" ...")
//System.out.println("begin to start process "+process.hashCode()+" ...")
//开启子线程裤岩,读取进程输出信息
Thread t = new SubprocessReader(process,isSubProcessOutput)
t.start()
//等待进程执行完成
int status = process.waitFor()
//logger.info("finish to execute process "+process.hashCode()+",exit status("+status+") ...")
//System.out.println("finish to execute process "+process.hashCode()+",exit status("+status+") ...")
return status
} catch (Exception e) {
logger.error(e)
}
return result
}
/**
* 读取进程输出内容
* @author Administrator
*
*/
class SubprocessReader extends Thread {
private Process subprocess
private boolean isOutput
SubprocessReader(Process p, boolean isOutput) {
this.subprocess = p
this.isOutput = isOutput
}
public void run() {
BufferedReader bufferedReader = null
try {
bufferedReader = new BufferedReader(
new InputStreamReader(subprocess.getInputStream(),"gbk"))
} catch (Exception e1) {
e1.printStackTrace()
}
String line = null
try {
while ((line = bufferedReader.readLine()) != null) {
if (isOutput) {
logger.info("process "+subprocess.hashCode()+":"+line)
//System.out.println("process "+subprocess.hashCode()+":"+line)
}
}
} catch (Exception e) {
logger.error("read subProcess error.", e)
} finally {
if (null != subprocess) {
InputStream is = subprocess.getInputStream()
InputStream es = subprocess.getErrorStream()
OutputStream os = subprocess.getOutputStream()
try {
bufferedReader.close()
is.close()
is = null
es.close()
es = null
os.close()
os = null
} catch (IOException e) {
}
subprocess = null
}
}
}
}
public static void main(String[] args)throws Exception {
List<String>commands = new ArrayList<String>()
//commands.add("convert")
//commands.add("-page")
//commands.add("952x3683")
//commands.add("pattern:gray100")
//commands.add("-page")
//commands.add("+555+316")
//commands.add("d:/wxwtest/common/plot2.png")
//commands.add("-background")
//commands.add("white")
//commands.add("-compose")
//commands.add("multiply")
//commands.add("-flatten")
//commands.add("d:/wxwtest/temp/empty.png")
//commands.add("convert")
//commands.add("d:\\wxwtest\\common\\plot.png")
//commands.add("-fill")
//commands.add("white")
//commands.add("-colorize")
//commands.add("50%")
//commands.add("d:/wxwtest/temp/temp.png")
commands.add("CutyCapt")
commands.add("-–url=http://www.3158.cn")
commands.add("-–out=d:/wxwtest/3158.jpg")
ProcessExecutor pe = ProcessExecutor.getInstance()
pe.executeProcess(commands)
//Runtime.getRuntime().exec("CutyCapt -–url=http://www.3158.cn -–out=d:/wxwtest/3158.jpg")
}
}
这是java执行子进程的实例代码,希望对你有用
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)