怎么在java中执行sudo 命令
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,本篇内容介绍了"怎么在java中执行sudo 命令"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成! *
千家信息网最后更新 2026年11月02日怎么在java中执行sudo 命令
本篇内容介绍了"怎么在java中执行sudo 命令"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
* 1. 修改/etc/sudoers文件,在其中添加一行: * username ALL=(ALL) NOPASSWD:ALL * 其中,"username"是需要运行这个程序的用户名。
/bin/bash -c echo password |sudo -S command
Shell(JSCH)工具类:
package cn.bywin.cbvsp.common.utils;import cn.bywin.cbvsp.DAL.BO.po.MyUserInfo;import com.jcraft.jsch.*;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;/** * @author linbin * @create 2019-03-19 10:06 */public class Shell { //远程主机的ip地址 private String ip; //远程主机登录用户名 private String username; //远程主机的登录密码 private String password; //设置ssh连接的远程端口 public static final int DEFAULT_SSH_PORT = 22; //保存输出内容的容器 private ArrayList stdout; public Shell() { } /** * 初始化登录信息 * * @param ip * @param username * @param password */ public Shell(final String ip, final String username, final String password) { this.ip = ip; this.username = username; this.password = password; stdout = new ArrayList(); } /** * 执行shell命令 * * @param command * @return */ //原本是 execute(final String command) public int execute(String command) { int returnCode = 0; JSch jsch = new JSch(); MyUserInfo userInfo = new MyUserInfo(); try { //创建session并且打开连接,因为创建session之后要主动打开连接 Session session = jsch.getSession(username, ip, DEFAULT_SSH_PORT); session.setPassword(password); session.setUserInfo(userInfo); session.connect(); //打开通道,设置通道类型,和执行的命令 Channel channel = session.openChannel("exec"); ChannelExec channelExec = (ChannelExec) channel; channelExec.setCommand(command); channelExec.setInputStream(null); BufferedReader input = new BufferedReader(new InputStreamReader (channelExec.getInputStream())); channelExec.connect(); System.out.println("The remote command is :" + command); //接收远程服务器执行命令的结果 String line; while ((line = input.readLine()) != null) { stdout.add(line); } input.close(); // 得到returnCode if (channelExec.isClosed()) { returnCode = channelExec.getExitStatus(); // 关闭通道 channelExec.disconnect(); //关闭session session.disconnect(); } } catch (JSchException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return returnCode; } public static ChannelExec getCommandResult(Session session, String command) throws JSchException, IOException { ChannelExec channelExec = (ChannelExec) session.openChannel("exec"); channelExec.setCommand(command); channelExec.setInputStream(null); channelExec.setErrStream(System.err); channelExec.connect(); /*if (null != session) { session.disconnect(); }*/ return channelExec; } /** * get stdout * * @return */ public ArrayList getStandardOutput() { return stdout; }} 执行代码:
public static void main(String[] args) { Shell shell = new Shell("192.168.161.152","root","linbinbhs7"); shell.execute("/bin/bash -c echo linbinbhs7 |sudo -S ls"); ArrayList graphicsCardNames = shell.getStandardOutput(); graphicsCardNames.forEach(e->{ System.out.println(e); }); } 删除指定时间的文件
public class TestShell { public static void main(String[] args) { Shell shell = new Shell("192.168.96.136","developer","de183!!!"); shell.execute("/bin/bash -c echo de183!!! |sudo -S find /home/developer/temp -type f -mmin +1 -exec rm -rfv {} \\; > /home/developer/temp/nohup.out;"); ArrayList graphicsCardNames = shell.getStandardOutput(); graphicsCardNames.forEach(System.out::println); }} "怎么在java中执行sudo 命令"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
命令
主机
内容
通道
登录
文件
更多
用户
用户名
知识
输出
实用
主动
学有所成
接下来
一行
代码
信息
原本
困境
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
耶加雪菲
互联网科技活动banner
互联网科技有限公司经营范围
互联网科技会
科技互联网公司装修
互联网科技小贷
互联网科技革命
凡科互联网科技
狮子座互联网科技培训
互联网 科技馆
团飞科技与伯堂互联网服务平台
汽车互联网创新科技园
互联网科技创新服务
花神(南京)互联网科技有限公司
崇明区互联网教育科技售后服务
中国互联网十大科技股排名
烟台灵鸽互联网科技有限公司
杭州披云加互联网科技有限公司
江苏互联网科技小贷
南京一网一红互联网科技有限公司
科技平台互联网
广西互联网科技公司
互联网经济与金融科技
中国互联网公司排名锤子科技
保险科技服务互联网营销体系
小米互联网科技园入驻
启辰互联网科技
互联网科技数字货币
全球科技与互联网公司市值变化
河南商务圈互联网科技有限公司