472,783 Members | 1,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,783 software developers and data experts.

Why won't this system command run?

I'm trying to change a users password on a Linux system
by spawning a process. Here's what the command looks
like on the command line:

echo 'myuser:mypasswd' | chpasswd

It uses a pipe, which I think may be causing the poblem.
Here's the java code to execute the above command, which
doesn't work. I've tried it with and without single
quotes, and numerous other permutations, to no avail.
try {

String psswd = "qwerty";
String user = "jane";

String[] passCmd = {"/bin/echo", user, ":", passwd, "|",
"/usr/sbin/chpasswd"};

Process p = Runtime.getRuntime().exec(passCmd);

}
catch (IOException e) {
System.out.println(e);
}
-Thank you
Jul 17 '05 #1
6 11665
somebody wrote:
I'm trying to change a users password on a Linux system
by spawning a process. Here's what the command looks
like on the command line:

echo 'myuser:mypasswd' | chpasswd

It uses a pipe, which I think may be causing the poblem.
Here's the java code to execute the above command, which
doesn't work. I've tried it with and without single
quotes, and numerous other permutations, to no avail.
try {

String psswd = "qwerty";
String user = "jane";

String[] passCmd = {"/bin/echo", user, ":", passwd, "|",
"/usr/sbin/chpasswd"};

Process p = Runtime.getRuntime().exec(passCmd);

}
catch (IOException e) {
System.out.println(e);
}


Things like pipe and redirection are performed by a shell. You will
need to execute the commands within a shell like bash, csh, ksh, etc.
For example

Runtime.getRuntime().exec(new String[] {
"/bin/bash", "-c", "echo 'myuser:mypasswd' | chpasswd"
});

Ray
Jul 17 '05 #2
On Sat, 27 Mar 2004 04:12:42 +0000, Raymond DeCampo wrote:
somebody wrote:
[quoted text muted]

Things like pipe and redirection are performed by a shell. You will need
to execute the commands within a shell like bash, csh, ksh, etc. For
example

Runtime.getRuntime().exec(new String[] {
"/bin/bash", "-c", "echo 'myuser:mypasswd' | chpasswd"
[quoted text muted]


Ray


Thanks for your help. I tried what you suggested, and still
can't get it to work. I even tried using full path names
to all the binaries involved:

String[] passCmd = {"/bin/bash", "-c", "/bin/echo
'user:password' | /usr/sbin/chpasswd"};

I also tried eliminating the single quotes, since "user" and
"password" are variables. I also tried separating each part
of the above string in double quotes. Any ideas?

-Thanks

Jul 17 '05 #3
> I also tried eliminating the single quotes, since "user" and
"password" are variables. I also tried separating each part
of the above string in double quotes. Any ideas?

-Thanks


maybe it's a cool security feature instead of a bug?
- nate

Jul 17 '05 #4
You can't do pipe or redirect in the Runtime.exec() argument.
Here's a very good article:
http://www.javaworld.com/javaworld/j...229-traps.html

somebody <so**@body.com> wrote in message news:<pa****************************@body.com>...
On Sat, 27 Mar 2004 04:12:42 +0000, Raymond DeCampo wrote:
somebody wrote:
[quoted text muted]

Things like pipe and redirection are performed by a shell. You will need
to execute the commands within a shell like bash, csh, ksh, etc. For
example

Runtime.getRuntime().exec(new String[] {
"/bin/bash", "-c", "echo 'myuser:mypasswd' | chpasswd"
[quoted text muted]


Ray


Thanks for your help. I tried what you suggested, and still
can't get it to work. I even tried using full path names
to all the binaries involved:

String[] passCmd = {"/bin/bash", "-c", "/bin/echo
'user:password' | /usr/sbin/chpasswd"};

I also tried eliminating the single quotes, since "user" and
"password" are variables. I also tried separating each part
of the above string in double quotes. Any ideas?

-Thanks

Jul 17 '05 #5
On Sat, 27 Mar 2004 04:11:41 -0800, hiwa wrote:
You can't do pipe or redirect in the Runtime.exec() argument. Here's a
very good article:
http://www.javaworld.com/javaworld/j...229-traps.html

somebody <so**@body.com> wrote in message
news:<pa****************************@body.com>...
[quoted text muted]

Very good article. I guess I'll just write a shell script
to do the chpasswd command, and run it from exec().

-Thanks

Jul 17 '05 #6
Sorry. I gave you a little bit of mis-info. And I believe if you
construct your array of String right, you could run pipe and/or
redirect as an argument for sh, not for Runtime.exec() directly.

See, the following code works perfect:

public class RtExecTest{
public static void main(String[] args) throws Exception{
String sh = "/bin/sh";
String cop = "-c";
String cmd = "ps -ax | grep mozilla > test.txt";

String[] exe = new String[] {sh, cop, cmd};

Runtime.getRuntime().exec(exe);
}
}

---test.txt result-----------------------------------------
1044 ? S 0:00 /bin/sh /usr/mozilla/run-mozilla.sh
/usr/mozilla/mozilla-bin
1050 ? S 0:03 /usr/mozilla/mozilla-bin
1078 pts/0 S 0:00 /bin/sh -c ps -ax | grep mozilla > test.txt
1080 pts/0 S 0:00 grep mozilla
------------------------------------------------------------

somebody <so**@body.com> wrote in message news:<pa****************************@body.com>...
On Sat, 27 Mar 2004 04:11:41 -0800, hiwa wrote:
You can't do pipe or redirect in the Runtime.exec() argument. Here's a
very good article:
http://www.javaworld.com/javaworld/j...229-traps.html

somebody <so**@body.com> wrote in message
news:<pa****************************@body.com>...
[quoted text muted]

Very good article. I guess I'll just write a shell script
to do the chpasswd command, and run it from exec().

-Thanks

Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Hal Vaughan | last post by:
I have a script used to find and run Java on a Windows system.Itworked fine on original tests (on a Windows XP system).It'snowrunningona Windows 2000 (sp3) system, and it won't work -- the...
2
by: David Carroll | last post by:
I'm at my wits end with this silly thing. I'm new to python, so please excuse my impending ignorance ;). I am trying to execute a command, and get the output back into a string variable. Here's...
16
by: Kerry Neilson | last post by:
For the past couple of months, Idle won't start when I invoke it. I am at a complete loss for why this is. When this happens, they python command line still starts, and python works fine...
4
by: jabailo | last post by:
This is driving me crazy. I finally got the Remoting sample chat application working almost. When I run the chat client in VS.NET it goes into an endless loop -- that's because I assume that...
13
by: cab0san | last post by:
I downloaded the DB2 runtime client, but the Client Base support and the TCP protocol do not install. I re-run the setup select them to run from disk, and they still show as not installed. What am...
3
by: billr | last post by:
Excuse me if you consider this a cross post, however, it was originally destined for this group not the vc.atl group, but somehow it went there instead of here! (obviously it was something I did, but...
7
by: DC Gringo | last post by:
I have a datagrid that won't sort. The event handler is firing and return label text, just not the sort. Here's my Sub Page_Load and Sub DataGrid1_SortCommand: -------------------- Private...
4
by: Jonathan Upright | last post by:
Greetings to anyone who can help: I'm using WebMatrix to make ASP.NET pages, and I chose the "Editable DataGrid" at the project selector screen. As you may know, it defaults to the Microsoft...
3
by: Blarneystone | last post by:
Ok, I've got a simple access 97 db. named S_tracking.mdb It has two tables 1- Jobs 2- Employees I've set up the references: Imports System.Data.OleDb Imports System.IO Imports System.data
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.