LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help with System() command on linux PERL (https://www.linuxquestions.org/questions/programming-9/help-with-system-command-on-linux-perl-273837/)

I R J 01-04-2005 03:35 PM

Help with System() command on linux PERL
 
I am writing a perl script to

1 start program
Code:

system "/usr/bin/chronyc";
2 pass these command to that terminal program

Code:

Password xxxxxxx
online
exit

i have tried

Code:

system "password x";
system "online";
system "exit";

but it does not work

All help appriciated

Ian

wpn146 01-04-2005 05:01 PM

No, three inline "system()" calls will not work. The "system()" call creates a child shell which executes the first command. The parent perl script waits until that terminates, then creates a completely different child shell to execute the second command. In this case, the "command" is the input data you intended to pass to the first command which has already terminated, so you will get the "command not found" message. Same for the third system() call.

Here is how you could do it for most commands. I think you could have problems doing it with passwords because of the way it usually expects to take input from the terminal so it can suppress echoing. Anyway, the below will help for learning how to do this type of a task.

NOTE: I have not tried this out! In fact, none of the systems I work with use chronyc so I am not familiar with it. If you have problems passing the password, look at the man page for chronyc parameters concerning how to redirect the password prompt.

There is always "more than one way to do that".
  • You could write your data to a temporary file and then do a "system()" call redirecting stdin to read from the temporary file. NOTE: This is BAD SECURITY IF YOU ARE WRITING PASSWORDS!
  • Do it the right way. Open a pipe and write directly to the pipe. Notice the "|" in the open call. The file handle "IN" could be whatever name you wish.
    Code:

    open IN, "| chronyc" or die "Could not execute \"chronyc\"";
    print IN "password xxxxxxx\n";
    print IN "online\n";
    print IN "exit\n";
    close IN;

Good luck,
Warren

I R J 01-05-2005 12:20 PM

Thanks that works peftectly

Ian


All times are GMT -5. The time now is 11:02 AM.