1:首先添加maven 依赖
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.48</version>
</dependency>
使用密码 方式连接 linux
public static String exec(String host, String user, String psw, int port,
String command) {
String result = ""
Session session = null
ChannelExec openChannel = null
try {
JSch jsch = new JSch()
session = jsch.getSession(user, host, port)
session.setPassword(psw.getBytes())
java.util.Properties config = new java.util.Properties()
config.put("StrictHostKeyChecking", "no")
session.setConfig(config)
session.connect()
openChannel = (ChannelExec) session.openChannel("exec")
openChannel.setCommand(command)
int exitStatus = openChannel.getExitStatus()
System.out.println(exitStatus)
openChannel.connect()
InputStream in = openChannel.getInputStream()
BufferedReader reader = new BufferedReader(
new InputStreamReader(in))
String buf = null
while ((buf = reader.readLine()) != null) {
result += new String(buf.getBytes("gbk"), "UTF-8")
+ "<br>\r\n"
}
} catch (JSchException | IOException e) {
e.printStackTrace()
result += e.getMessage()
} finally {
if (openChannel != null &&!openChannel.isClosed()) {
openChannel.disconnect()
}
if (session != null &&session.isConnected()) {
session.disconnect()
}
}
return result
}
String exec = exec("192.168.80.101", "root", "111", 22,"sleep 2ls")
使用 秘钥方式 连接linux
public static String exec1(String ip, String user, int port,
String privateKey, String passphrase, String command) {
String result = ""
Session session = null
ChannelExec openChannel = null
try {
JSch jsch = new JSch()
jsch.addIdentity(privateKey)
session = jsch.getSession(user, ip, port)
java.util.Properties config = new java.util.Properties()
config.put("StrictHostKeyChecking", "no")
session.setConfig(config)
session.connect()
openChannel = (ChannelExec) session.openChannel("exec")
openChannel.setCommand(command)
int exitStatus = openChannel.getExitStatus()
System.out.println(exitStatus)
openChannel.connect()
InputStream in = openChannel.getInputStream()
BufferedReader reader = new BufferedReader(
new InputStreamReader(in))
String buf = null
while ((buf = reader.readLine()) != null) {
result += new String(buf.getBytes("gbk"), "UTF-8")
+ "<br>\r\n"
}
} catch (JSchException | IOException e) {
e.printStackTrace()
result += e.getMessage()
} finally {
if (openChannel != null &&!openChannel.isClosed()) {
openChannel.disconnect()
}
if (session != null &&session.isConnected()) {
session.disconnect()
}
}
return result
}
String result=exec1("192.168.80.101", "root", 22,"C:\\Users\\ebnew\\Desktop\\office-key(1)", "", "sleep 2ls")
package jschimport java.io.File
import java.io.FileInputStream
import java.util.Properties
import com.jcraft.jsch.Channel
import com.jcraft.jsch.ChannelSftp
import com.jcraft.jsch.JSch
import com.jcraft.jsch.JSchException
import com.jcraft.jsch.Session
public class Test {
protected String host//sftp服务器ip
protected String username//用户名
protected String password//密码
protected String privateKey//密钥文件路径
protected String passphrase//密钥口令
protected int port = 22//默认的sftp端口号是22
/**
* 获取连接
* @return channel
*/
public ChannelSftp connectSFTP() {
JSch jsch = new JSch()
Channel channel = null
try {
if (privateKey != null && !"".equals(privateKey)) {
//使用密钥验证方式,密钥可以使有口令的密钥,也可以是没有口令的密钥
if (passphrase != null && "".equals(passphrase)) {
jsch.addIdentity(privateKey, passphrase)
} else {
jsch.addIdentity(privateKey)
}
}
Session session = jsch.getSession(username, host, port)
if (password != null && !"".equals(password)) {
session.setPassword(password)
}
Properties sshConfig = new Properties()
sshConfig.put("StrictHostKeyChecking", "no")// do not verify host key
session.setConfig(sshConfig)
// session.setTimeout(timeout)
session.setServerAliveInterval(92000)
session.connect()
//参数sftp指明要打开的连接是sftp连接
channel = session.openChannel("sftp")
channel.connect()
} catch (JSchException e) {
e.printStackTrace()
}
return (ChannelSftp) channel
}
/**
* 上传文件
*
* @param directory
* 上传的目录
* @param uploadFile
* 要上传的文件
* @param sftp
*/
public void upload(String directory, String uploadFile, ChannelSftp sftp) {
try {
sftp.cd(directory)
File file = new File(uploadFile)
sftp.put(new FileInputStream(file), file.getName())
} catch (Exception e) {
e.printStackTrace()
}
}
/**
* 下载文件
*
* @param directory
* 下载目录
* @param downloadFile
* 下载的文件
* @param saveFile
* 存在本地的路径
* @param sftp
*/
public void download(String directory, String downloadFile,
String saveFile, ChannelSftp sftp) {
try {
sftp.cd(directory)
sftp.get(downloadFile,saveFile)
} catch (Exception e) {
e.printStackTrace()
}
}
/**
* 删除文件
*
* @param directory
* 要删除文件所在目录
* @param deleteFile
* 要删除的文件
* @param sftp
*/
public void delete(String directory, String deleteFile, ChannelSftp sftp) {
try {
sftp.cd(directory)
sftp.rm(deleteFile)
} catch (Exception e) {
e.printStackTrace()
}
}
public void disconnected(ChannelSftp sftp){
if (sftp != null) {
try {
sftp.getSession().disconnect()
} catch (JSchException e) {
e.printStackTrace()
}
sftp.disconnect()
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)