怎么在java中执行sudo 命令
发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,本篇内容介绍了"怎么在java中执行sudo 命令"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成! *
千家信息网最后更新 2025年12月03日怎么在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安全错误
数据库的锁怎样保障安全
发票摇奖小程序服务器异常
dbms数据库有啥
自动化专业数据库
互联网加科技育儿
怎么看数据库记住的密码
山东服务器维修哪家好云主机
深圳工业软件开发报价
网络安全宣传主持人主持稿
学习网站用什么服务器
全球网络安全领域排名
一卡通服务器需要几台
lol转区服务器排行榜
降临服务器
人民警察网络安全管理
软件开发包就业
许可证服务器的计算机名
prom时序数据库指标
搞软件开发的该怎么找客户
网络安全对大学生造成的影响
steam无法连接服务器
枣庄市网络安全执法典型案
网络安全四个坚持的具体内容
华为网络安全二级违规
软件公司it 网络安全
数据库导出表到2003
数据库的注册和登录密码
软件开发去哪里培训
steam无法连接服务器
3个网络安全方面的问题
网络安全平台培训会