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")
首先确保Linux开启sshd服务,并支持远程SSH连接。java程序使用jsch框架登录Linux,执行命令。protected void creation() throws Exception {
JSch jsch = new JSch()
session = jsch.getSession(userName, host, port)
session.setPassword(password)
Properties config = new Properties()
config.put("StrictHostKeyChecking", "no")
session.setConfig(config)
session.setTimeout(CONNECT_TIMEOUT)
session.setConfig("PreferredAuthentications", "password,keyboard-interactive")
session.setServerAliveInterval(1000 * 60 * 2)
session.connect()
}
public String sendCommand(String command) throws Exception {
if(!isConnected())
throw new JSchException("Session is not connected, command exec faild.")
final ChannelExec exec = (ChannelExec)session.openChannel("exec")
ByteArrayOutputStream out = new ByteArrayOutputStream()
exec.setCommand(command)
exec.setOutputStream(out)
exec.setExtOutputStream(out)
exec.connect()
final Thread thread = new Thread() {
public void run() {
while(!exec.isEOF()) {
try { Thread.sleep(500L)} catch(Exception e) {}
}
}
}
thread.setDaemon(true)
thread.start()
thread.join(EXEC_TIMEOUT)
thread.interrupt()
if(thread.isAlive()) {
throw new JSchException("Exec Time Out Error")
} else {
try {
exec.disconnect()
out.close()
} catch (Exception e) {
}
byte[] lens = out.toByteArray()
String result = new String(lens, charset)
if(result.startsWith("bash") &&result.indexOf("command not found") != -1)
return ""
return result
}
}
Java和Linux的理想关系是,一方面有一种可以在所有 *** 作系统上运行的语言,另一方面 *** 作系统可以根据需要进行各种计算。这种关系本身在过去,现在或者将来都可以实现,但现在目前Java并没有在Linux界唤起多大的兴趣,原因主要在于Java和Linux群体和技术在原则性和专业性上存在差异造成的。原则性的差异涉及开放源码和免费软件这两个术语。Java既不开放也不免费,而Linux坚持这两个原则,至少在理论上是这样。此外,Java倡导者也必须明白,在Linux界存在着一个明显的分歧,大部分Linux人士都非常注重“free”这个词,虽然有些人是从经济上来了解这个词,但无论怎样他们都坚持开放源码的原则。尽管Linux有向各个阶层推广的趋势,但目前来说自由软件的积极倡导者主要来自世界各大学的学生和研究人员。他们对Linux的发展是非常关键的,为开发Linux和 *** 作系统软件输送了大批的人才。
开放源码和Linux的商业价值取决于那些用Linux为市场开发产品的开发人员,以及喜欢根据自己需要用Linux对 *** 作系统进行自定义的人们。如果必要,这些人会出钱购买Linux,因为它是一种开放的源码。他们对Linux的发展也是至关重要的,否则,Linux将只是一堆计算机课程和深奥的研究课题。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)