public class JschUtil extends Object
| Constructor and Description |
|---|
JschUtil() |
| Modifier and Type | Method and Description |
|---|---|
static boolean |
bindPort(com.jcraft.jsch.Session session,
String remoteHost,
int remotePort,
int localPort)
绑定端口到本地。
|
static boolean |
bindPort(com.jcraft.jsch.Session session,
String remoteHost,
int remotePort,
String localHost,
int localPort)
绑定端口到本地。
|
static boolean |
bindRemotePort(com.jcraft.jsch.Session session,
int bindPort,
String host,
int port)
绑定ssh服务端的serverPort端口, 到host主机的port端口上.
|
static void |
close(com.jcraft.jsch.Channel channel)
关闭会话通道
|
static void |
close(com.jcraft.jsch.Session session)
关闭SSH连接会话
|
static void |
close(String key)
关闭SSH连接会话
|
static void |
closeAll()
关闭所有SSH连接会话
|
static com.jcraft.jsch.Channel |
createChannel(com.jcraft.jsch.Session session,
ChannelType channelType)
创建Channel连接
|
static com.jcraft.jsch.Session |
createSession(com.jcraft.jsch.JSch jsch,
String sshHost,
int sshPort,
String sshUser)
创建一个SSH会话,重用已经使用的会话
|
static com.jcraft.jsch.Session |
createSession(String sshHost,
int sshPort,
String sshUser,
byte[] privateKey,
byte[] passphrase)
新建一个新的SSH会话,此方法并不打开会话(既不调用connect方法)
|
static com.jcraft.jsch.Session |
createSession(String sshHost,
int sshPort,
String sshUser,
String sshPass)
新建一个新的SSH会话,此方法并不打开会话(既不调用connect方法)
|
static com.jcraft.jsch.Session |
createSession(String sshHost,
int sshPort,
String sshUser,
String privateKeyPath,
byte[] passphrase)
新建一个新的SSH会话,此方法并不打开会话(既不调用connect方法)
|
static Sftp |
createSftp(com.jcraft.jsch.Session session)
创建Sftp
|
static Sftp |
createSftp(String sshHost,
int sshPort,
String sshUser,
String sshPass)
创建Sftp
|
static String |
exec(com.jcraft.jsch.Session session,
String cmd,
Charset charset)
执行Shell命令
|
static String |
exec(com.jcraft.jsch.Session session,
String cmd,
Charset charset,
OutputStream errStream)
执行Shell命令(使用EXEC方式)
此方法单次发送一个命令到服务端,不读取环境变量,执行结束后自动关闭channel,不会产生阻塞。
|
static String |
execByShell(com.jcraft.jsch.Session session,
String cmd,
Charset charset)
执行Shell命令
此方法单次发送一个命令到服务端,自动读取环境变量,执行结束后自动关闭channel,不会产生阻塞。
|
static int |
generateLocalPort()
生成一个本地端口,用于远程端口映射
|
static com.jcraft.jsch.Session |
getSession(String sshHost,
int sshPort,
String sshUser,
byte[] privateKey,
byte[] passphrase)
获得一个SSH会话,重用已经使用的会话
|
static com.jcraft.jsch.Session |
getSession(String sshHost,
int sshPort,
String sshUser,
String sshPass)
获得一个SSH会话,重用已经使用的会话
|
static com.jcraft.jsch.Session |
getSession(String sshHost,
int sshPort,
String sshUser,
String privateKeyPath,
byte[] passphrase)
获得一个SSH会话,重用已经使用的会话
|
static int |
openAndBindPortToLocal(Connector sshConn,
String remoteHost,
int remotePort)
打开SSH会话,并绑定远程端口到本地的一个随机端口
|
static com.jcraft.jsch.Channel |
openChannel(com.jcraft.jsch.Session session,
ChannelType channelType)
打开Channel连接
|
static com.jcraft.jsch.Channel |
openChannel(com.jcraft.jsch.Session session,
ChannelType channelType,
int timeout)
打开Channel连接
|
static com.jcraft.jsch.Session |
openSession(String sshHost,
int sshPort,
String sshUser,
byte[] privateKey,
byte[] passphrase)
打开一个新的SSH会话
|
static com.jcraft.jsch.Session |
openSession(String sshHost,
int sshPort,
String sshUser,
byte[] privateKey,
byte[] passphrase,
int timeOut)
打开一个新的SSH会话
|
static com.jcraft.jsch.Session |
openSession(String sshHost,
int sshPort,
String sshUser,
String sshPass)
打开一个新的SSH会话
|
static com.jcraft.jsch.Session |
openSession(String sshHost,
int sshPort,
String sshUser,
String privateKeyPath,
byte[] passphrase)
打开一个新的SSH会话
|
static com.jcraft.jsch.Session |
openSession(String sshHost,
int sshPort,
String sshUser,
String privateKeyPath,
byte[] passphrase,
int timeOut)
打开一个新的SSH会话
|
static com.jcraft.jsch.Session |
openSession(String sshHost,
int sshPort,
String sshUser,
String sshPass,
int timeout)
打开一个新的SSH会话
|
static com.jcraft.jsch.ChannelSftp |
openSftp(com.jcraft.jsch.Session session)
打开SFTP连接
|
static com.jcraft.jsch.ChannelSftp |
openSftp(com.jcraft.jsch.Session session,
int timeout)
打开SFTP连接
|
static com.jcraft.jsch.ChannelShell |
openShell(com.jcraft.jsch.Session session)
打开Shell连接
|
static boolean |
unBindPort(com.jcraft.jsch.Session session,
int localPort)
解除端口映射
|
public static final String SSH_NONE
public static int generateLocalPort()
public static com.jcraft.jsch.Session getSession(String sshHost, int sshPort, String sshUser, String sshPass)
sshHost - 主机sshPort - 端口sshUser - 用户名sshPass - 密码public static com.jcraft.jsch.Session getSession(String sshHost, int sshPort, String sshUser, String privateKeyPath, byte[] passphrase)
sshHost - 主机sshPort - 端口sshUser - 用户名privateKeyPath - 私钥路径passphrase - 私钥密码public static com.jcraft.jsch.Session getSession(String sshHost, int sshPort, String sshUser, byte[] privateKey, byte[] passphrase)
sshHost - 主机sshPort - 端口sshUser - 用户名privateKey - 私钥内容passphrase - 私钥密码public static com.jcraft.jsch.Session openSession(String sshHost, int sshPort, String sshUser, String sshPass)
sshHost - 主机sshPort - 端口sshUser - 用户名sshPass - 密码public static com.jcraft.jsch.Session openSession(String sshHost, int sshPort, String sshUser, String sshPass, int timeout)
sshHost - 主机sshPort - 端口sshUser - 用户名sshPass - 密码timeout - Socket连接超时时长,单位毫秒public static com.jcraft.jsch.Session openSession(String sshHost, int sshPort, String sshUser, String privateKeyPath, byte[] passphrase)
sshHost - 主机sshPort - 端口sshUser - 用户名privateKeyPath - 私钥的路径passphrase - 私钥文件的密码,可以为nullpublic static com.jcraft.jsch.Session openSession(String sshHost, int sshPort, String sshUser, byte[] privateKey, byte[] passphrase)
sshHost - 主机sshPort - 端口sshUser - 用户名privateKey - 私钥内容passphrase - 私钥文件的密码,可以为nullpublic static com.jcraft.jsch.Session openSession(String sshHost, int sshPort, String sshUser, byte[] privateKey, byte[] passphrase, int timeOut)
sshHost - 主机sshPort - 端口sshUser - 用户名privateKey - 私钥内容passphrase - 私钥文件的密码,可以为nulltimeOut - 超时时长public static com.jcraft.jsch.Session openSession(String sshHost, int sshPort, String sshUser, String privateKeyPath, byte[] passphrase, int timeOut)
sshHost - 主机sshPort - 端口sshUser - 用户名privateKeyPath - 私钥的路径passphrase - 私钥文件的密码,可以为nulltimeOut - 超时时间,单位毫秒public static com.jcraft.jsch.Session createSession(String sshHost, int sshPort, String sshUser, String sshPass)
sshHost - 主机sshPort - 端口sshUser - 用户名,如果为null,默认rootsshPass - 密码public static com.jcraft.jsch.Session createSession(String sshHost, int sshPort, String sshUser, String privateKeyPath, byte[] passphrase)
sshHost - 主机sshPort - 端口sshUser - 用户名,如果为null,默认rootprivateKeyPath - 私钥的路径passphrase - 私钥文件的密码,可以为nullpublic static com.jcraft.jsch.Session createSession(String sshHost, int sshPort, String sshUser, byte[] privateKey, byte[] passphrase)
sshHost - 主机sshPort - 端口sshUser - 用户名,如果为null,默认rootprivateKey - 私钥内容passphrase - 私钥文件的密码,可以为nullpublic static com.jcraft.jsch.Session createSession(com.jcraft.jsch.JSch jsch,
String sshHost,
int sshPort,
String sshUser)
jsch - JSchsshHost - 主机sshPort - 端口sshUser - 用户名,如果为null,默认rootSessionpublic static boolean bindPort(com.jcraft.jsch.Session session,
String remoteHost,
int remotePort,
int localPort)
throws JschRuntimeException
session - 需要绑定端口的SSH会话remoteHost - 远程主机remotePort - 远程端口localPort - 本地端口JschRuntimeException - 端口绑定失败异常public static boolean bindPort(com.jcraft.jsch.Session session,
String remoteHost,
int remotePort,
String localHost,
int localPort)
throws JschRuntimeException
session - 需要绑定端口的SSH会话remoteHost - 远程主机remotePort - 远程端口localHost - 本地主机localPort - 本地端口JschRuntimeException - 端口绑定失败异常public static boolean bindRemotePort(com.jcraft.jsch.Session session,
int bindPort,
String host,
int port)
throws JschRuntimeException
session - 与ssh服务端建立的会话bindPort - ssh服务端上要被绑定的端口host - 转发到的hostport - host上的端口JschRuntimeException - 端口绑定失败异常public static boolean unBindPort(com.jcraft.jsch.Session session,
int localPort)
session - 需要解除端口映射的SSH会话localPort - 需要解除的本地端口public static int openAndBindPortToLocal(Connector sshConn, String remoteHost, int remotePort) throws JschRuntimeException
sshConn - SSH连接信息对象remoteHost - 远程主机remotePort - 远程端口JschRuntimeException - 连接异常public static com.jcraft.jsch.ChannelSftp openSftp(com.jcraft.jsch.Session session)
session - Session会话ChannelSftppublic static com.jcraft.jsch.ChannelSftp openSftp(com.jcraft.jsch.Session session,
int timeout)
session - Session会话timeout - 连接超时时长,单位毫秒ChannelSftppublic static Sftp createSftp(String sshHost, int sshPort, String sshUser, String sshPass)
sshHost - 远程主机sshPort - 远程主机端口sshUser - 远程主机用户名sshPass - 远程主机密码Sftppublic static Sftp createSftp(com.jcraft.jsch.Session session)
session - SSH会话Sftppublic static com.jcraft.jsch.ChannelShell openShell(com.jcraft.jsch.Session session)
session - Session会话ChannelShellpublic static com.jcraft.jsch.Channel openChannel(com.jcraft.jsch.Session session,
ChannelType channelType)
session - Session会话channelType - 通道类型,可以是shell或sftp等,见ChannelTypeChannelpublic static com.jcraft.jsch.Channel openChannel(com.jcraft.jsch.Session session,
ChannelType channelType,
int timeout)
session - Session会话channelType - 通道类型,可以是shell或sftp等,见ChannelTypetimeout - 连接超时时长,单位毫秒Channelpublic static com.jcraft.jsch.Channel createChannel(com.jcraft.jsch.Session session,
ChannelType channelType)
session - Session会话channelType - 通道类型,可以是shell或sftp等,见ChannelTypeChannelpublic static String exec(com.jcraft.jsch.Session session, String cmd, Charset charset)
session - Session会话cmd - 命令charset - 发送和读取内容的编码ChannelExecpublic static String exec(com.jcraft.jsch.Session session, String cmd, Charset charset, OutputStream errStream)
此方法单次发送一个命令到服务端,不读取环境变量,执行结束后自动关闭channel,不会产生阻塞。
session - Session会话cmd - 命令charset - 发送和读取内容的编码errStream - 错误信息输出到的位置public static String execByShell(com.jcraft.jsch.Session session, String cmd, Charset charset)
此方法单次发送一个命令到服务端,自动读取环境变量,执行结束后自动关闭channel,不会产生阻塞。
session - Session会话cmd - 命令charset - 发送和读取内容的编码ChannelExecpublic static void close(com.jcraft.jsch.Session session)
session - SSH会话public static void close(com.jcraft.jsch.Channel channel)
channel - 会话通道public static void close(String key)
key - 主机,格式为user@host:portpublic static void closeAll()
Copyright © 2025. All rights reserved.