Linux commands from Java

2020-02-11 04:51发布

Is it possbile to execute linux commands with java? I am trying to create a web servlet to allow ftp users to change their passwords without ssh login access. I would like to execute the next commands:

# adduser -s /sbin/nologin clientA -d /home/mainclient/clientA
# passwd clientA
# cd /home/mainclient; chgrp -R mainclient clientA
# cd /home/mainclient/clientA; chmod 770 .

标签: java linux
5条回答
Root(大扎)
2楼-- · 2020-02-11 05:35

The java RunTime object has exec methods to run commands in a separate process

查看更多
Animai°情兽
3楼-- · 2020-02-11 05:36

If you invoke those commands from Java, make sure to pack multiple commands to a single shell-script. This will make invocation much easier.

查看更多
老娘就宠你
4楼-- · 2020-02-11 05:38
趁早两清
5楼-- · 2020-02-11 05:42

Check out this.

However, doing what you are talking about is way outside spec, and I wouldnt reccommend it. To get it to work you are going to either run your app server as root, or use some other mechanism to give the user the app server is running as permission to execute these privileged commands. One small screw-up somewhere and you are "owned".

查看更多
仙女界的扛把子
6楼-- · 2020-02-11 05:51

Use:

Runtime.getRuntim().exec("Command");

where Command is the command string you want to execute.

查看更多
登录 后发表回答