By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,009 Members | 1,767 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,009 IT Pros & Developers. It's quick & easy.

Why won't this system command run?

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
> 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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.