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