473,321 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 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 11690
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.  It worked fine on original tests (on a Windows XP system).  It's now running on a 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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.