The manpage for ssh claims:
ssh exits with the exit status of the remote command or with 255 if an error occurred.
Have you tried to look at the return value of system?
my $status = system("/usr/bin/ssh $server \"/usr/sbin/useradd $login\"
+");
or, using qq...
my $status = system qq(/usr/bin/ssh $server "/usr/sbin/useradd $login"
+);
| [reply] [d/l] [select] |
That's what I used, but my interpretation of the values was wrong. I did not expect ssh to be so clever to "pipe" the result I wanted. Thank you too!
| [reply] |
If you want the output of a command, which is not really a "return value", then look at Backticks or the qx operator. Both will return you the output of the command. The exit status of the command will still be stored in $?.
| [reply] |
As mentioned above my question was not clear. I really found what I was looking in $?. It actually is the return value of the useradd command. I thought it is from the ssh command.
Getting results is not understanding them. ;-)
Thank you!
| [reply] |
system("/usr/bin/ssh $server \"/usr/sbin/useradd $login\"");
Do you really use the single-argument form of system? This usually involves a shell, and forces you to guess what shell is invoked and what type of quoting it needs. Typically, you will get lots of problems as soon as you include white space, special characters, or variables. You have all of these.
Use the list form of system:
system('/usr/bin/ssh',$server,"/usr/sbin/useradd $login");
And I'm very sure that this call will fail with a "command not found" error. You don't want to invoke the program "/usr/sbin/useradd joe average user" when $login is "joe average user". You want to invoke the program "/usr/sbin/useradd" and pass whatever $login contains as first argument. ssh does not invoke the shell on the remote system.
So:
system('/usr/bin/ssh',$server,'/usr/sbin/useradd',$login);
What's the big difference? No quoting mess, all arguments end exactly where you want them, even if they contain usual characters.
Why?
Unix systems pass an array of arguments to each program (that's where C's argv argument to main() comes from, and perl copies that information into $^X, $0, and @ARGV). Only the shell splits the entered string into an array of arguments, typically on $ENV{'IFS'}. It also expands variables, * and ?, and several other constructs. The first element of the array is also taken as the name of the program to be started.
DOS and Windows, on the other hand, pass a single string to the program invoked. command.com trims the input and splits it at the first white space into executable name and argument string, expanding variables, but not * and ?. That is left to the invoked executable, and the executable also has to split the argument string into an array. The latter typically happens in the C runtime library and it depends on the implementation, * and ? are typically passed unmodified to the program code.
Perl tries to hide the evil differences between differrent operating systems from you, so even on Windows, the multiple-argument form of system() works on Windows, automatically quoting arguments where needed (or at least, it should do so). There are several special tricks for system() on different platforms, see http://search.cpan.org/~rgarcia/perl-5.10.0/pod/perlport.pod#system.
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
| [reply] [d/l] [select] |
You get the return value of any command in $? >> 8. I don't understand the title of your question (which is different from the content) though. What do you mean with "multiple return values"? A process only has a single return value. And it's an integer between 0 and 255, inclusive. | [reply] [d/l] |
Sorry, what I ment was: one return value from the ssh command and one from the useradd command. My question was for the second one only.
Thank you for the fast response!
handarbyte
| [reply] |