本文目錄一覽:
- 1、java如何執行遠程服務器上的.sh文件
- 2、JAVA_JSCH如何遠程操作SFTP服務器上的文件?
- 3、如何用Java實現SSH遠程連接?
- 4、Apache SSHD實現SFTP服務端,客戶端訪問進行遠程文件上傳下載
- 5、jsch實現java sftp上傳,在非root用戶下出現permission dined異常,
java如何執行遠程服務器上的.sh文件
你可以使用JSch
JSch全稱是「Java Secure Channel」
是SSH2的一個純Java實現。它允許你連接到一個sshd 服務器,使用端口轉發,X11轉發,文件傳輸等等。同時也是支持執行命令;
以下是大概運行的代碼,只是提供大致思路,可以去查官方API和demo
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelS;
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等操作
}
JAVA_JSCH如何遠程操作SFTP服務器上的文件?
使用SSH協議進行FTP傳輸的協議叫SFTP
換言之你的SSH協議一定啟用了,那麼使用基本linux命令在遠端執行即可。
我個人而言,JSCH一般是這樣用的:SFTP用於單純的文件上傳,之後直接使用基礎ssh協議執行遠端linux命令(比如說,移動文件或是重啟服務器等等)
至於API的具體使用方式,稍微搜索一下很容易找到,比如這個:
如何用Java實現SSH遠程連接?
這還要思路。。。
表單提交到後台,觸發方法, 然後調jsch的方法,獲取返回信息。 然後return到頁面。
搞定!
Apache SSHD實現SFTP服務端,客戶端訪問進行遠程文件上傳下載
package jsch;
import 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 {
s;
File file = new File(uploadFile);
s(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 {
s;
s;
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 刪除文件
*
* @param directory
* 要刪除文件所在目錄
* @param deleteFile
* 要刪除的文件
* @param sftp
*/
public void delete(String directory, String deleteFile, ChannelSftp sftp) {
try {
s;
s;
} catch (Exception e) {
e.printStackTrace();
}
}
public void disconnected(ChannelSftp sftp){
if (sftp != null) {
try {
s;
} catch (JSchException e) {
e.printStackTrace();
}
s;
}
}
}
jsch實現java sftp上傳,在非root用戶下出現permission dined異常,
這個正常,應該是你當前上傳用戶在上傳位置沒有權限造成的,權限應該在服務器端修改
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/160972.html