首页 / 知识
教你用页面操作liunx命令
2023-04-11 16:19:00

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;
}
}
|
最新内容
相关内容
机器学习用java还是python?
机器学习用java还是python?,分析,环境,数据,培训,发展,机器,结果,控制台,生态环境,有用,机器学习用python更合适。机器学习不需要面向对象,不python怎么清除命令窗中的命令
python怎么清除命令窗中的命令,代码,培训,命令,冗余,以上,以下,方法,更多,内容,python清空命令行的方法:有时我们在命令行上运行一些代码时,觉学python需要java吗?
学python需要java吗?,代码,时间,人工智能,数据,分析,美的,手机,对比,培训,效率,学Python不一定要学Java,先学好Python没,入门了,之后学习其他语python怎么创建mysql数据库连接
python怎么创建mysql数据库连接,项目,数据,培训,参数,结果,数据库,方法,语句,数据库连接,接口,python数据库接口支持非常多的数据库,你可以选怎么使用python命令行参数
怎么使用python命令行参数,代码,灵活,异常,培训,参数,模块,选项,列表,方法,以上,Python提供了getopt模块来获取命令行参数。$pythontest.pyar转java还是python?
转java还是python?,人工智能,数据,行业,培训,生产,产业,技术,平台,风险,生态,从目前IT行业的人才需求上来看,如果是应用级开发,那么掌握Java语python怎样连接mysql数据库
python怎样连接mysql数据库,培训,数据,流程,项目,一致,系统,名字,数据库,参数,结果,python数据库接口支持非常多的数据库,你可以选择适合你项python连接数据库
python连接数据库,培训,数据,项目,一致,流程,数据库,参数,结果,接口,语句,python连接数据库推荐使用MySQLdb。Python数据库接口支持非常多的python连接oracle乱码怎么办
python连接oracle乱码怎么办,培训,一致,密码,乱码,脚本,字符集,源文件,中文,解决方案,以上,在使用Python开发项目过程中,有时需使用脚本调用orpython怎么执行命令
python怎么执行命令,代码,环境,培训,系统,工作,命令,路径,操作系统,程序,空格,Python的命令提示符是在操作系统中,提示进行命令输入的一种工作pythonjava语法区别有哪些
pythonjava语法区别有哪些,名称,培训,数字,数据,能存,标准,数据类型,语法,字符串,分号,1、变量java中定义变量,inta=0;而python中为a=0;由此可python如何连接mysql数据库
python如何连接mysql数据库,培训,数据,项目,一致,流程,数据库,参数,结果,接口,语句,python数据库接口支持非常多的数据库,你可以选择适合你项