java连接服务器
Maven
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>262</version>
</dependency>
java代码
public class LiunxUtil {
public static String execute(String ip,String username,String password, String cmd) {
try {
// 创建连接
Connection conn = new Connection(ip, 22);
// 启动连接
conn.connect();
// 验证用户密码
conn.authenticateWithPassword(username, password);
Session session = conn.openSession();
//执行命令
session.execCommand(cmd);
session = conn.openSession();
//执行命令
session.execCommand(cmd);
InputStream stdout = new StreamGobbler(session.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
StringBuffer buffer = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
buffer.append(line + "\n");
}
String result = buffer.toString();
System.out.println(result);
session.close();
conn.close();
//如果没有异常,返回结果为命令执行结果,如果有异常,返回null
return result;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
执行代码
public static void main(String[] args) {
String execute = LiunxUtil.execute(host,userName,password,"ls");
System.out.println(execute);
}
执行结果
centos服务器的文件上传和下载
工具包
public class FtpUtil {
private static final Logger loggerMonitor = LoggerFactory.getLogger("monitor");
/**
* FTPClient对象
**/
private static ChannelSftp ftpClient = null;
/**
*
*/
private static Session sshSession = null;
/**
* 连接服务器
*
* @param host
* @param port
* @param userName
* @param password
* @return
* @throws Exception
*/
public static ChannelSftp getConnect(String host, String port, String userName, String password)
throws Exception {
try {
JSch jsch = new JSch();
// 获取sshSession
sshSession = jsch.getSession(userName, host, Integer.parseInt(port));
// 添加s密码
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
// 开启sshSession链接
sshSession.connect();
// 获取sftp通道
ftpClient = (ChannelSftp) sshSession.openChannel("sftp");
// 开启
ftpClient.connect();
loggerMonitor.debug("success ……");
} catch (Exception e) {
e.printStackTrace();
throw new Exception("连接sftp服务器异常……");
}
return ftpClient;
}
/**
* 下载文件
*
* @param ftp_path 服务器文件路径
* @param save_path 下载保存路径
* @param oldFileName 服务器上文件名
* @param newFileName 保存后新文件名
* @throws Exception
*/
public static void download(String ftp_path, String save_path, String oldFileName, String newFileName)
throws Exception {
FileOutputStream fos = null;
try {
ftpClient.cd(ftp_path);
File file = new File(save_path);
if (!file.exists()) {
file.mkdirs();
}
String saveFile = save_path + newFileName;
File file1 = new File(saveFile);
fos = new FileOutputStream(file1);
ftpClient.get(oldFileName, fos);
} catch (Exception e) {
loggerMonitor.error("下载文件异常……", e.getMessage());
throw new Exception("download file error……");
} finally {
if (fos != null) {
try {
fos.close();
} catch (Exception e) {
e.printStackTrace();
throw new Exception("close stream error……");
}
}
}
}
/**
* 上传
*
* @param upload_path 上传文件路径
* @param ftp_path 服务器保存路径
* @param newFileName 新文件名
* @throws Exception
*/
public static void uploadFile(String upload_path, String ftp_path, String newFileName) throws Exception {
FileInputStream fis = null;
try {
fis = new FileInputStream(new File(upload_path));
ftpClient.cd(ftp_path);
ftpClient.put(fis, newFileName);
} catch (Exception e) {
e.printStackTrace();
throw new Exception("Upload file error.");
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
throw new Exception("close stream error.");
}
}
}
}
/**
* 关闭
*
* @throws Exception
*/
public static void close() throws Exception {
loggerMonitor.debug("close……");
try {
ftpClient.disconnect();
sshSession.disconnect();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception("close stream error.");
}
}
/**
* 遍历文件夹里的文件
* @file 文件夹路径
* @fileList 存储文件名的集合
*/
public static ArrayList<String> isDirectory(File file, ArrayList<String> fileList) {
if(file.exists()){
if (file.isFile()) {
int start = file.getAbsolutePath()。lastIndexOf("\\")+1;
int end = file.getAbsolutePath()。length();
fileList.add(file.getAbsolutePath()。substring(start,end));
}else{
File[] list = file.listFiles();
if (list.length == 0) {
System.out.println(file.getAbsolutePath() + " is null");
} else {
for (int i = 0; i < list.length; i++) {
isDirectory(list[i],fileList);//递归调用
}
}
}
}else{
System.out.println("文件不存在!");
}
return fileList;
}
}
|