LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-04-2005, 03:35 PM   #1
I R J
LQ Newbie
 
Registered: Jul 2004
Posts: 3

Rep: Reputation: 0
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
 
Old 01-04-2005, 05:01 PM   #2
wpn146
Member
 
Registered: Jan 2005
Distribution: Solaris, Linux Fedora Core 6
Posts: 170

Rep: Reputation: 30
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
 
Old 01-05-2005, 12:20 PM   #3
I R J
LQ Newbie
 
Registered: Jul 2004
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks that works peftectly

Ian

Last edited by I R J; 01-05-2005 at 12:33 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Require Linux/Perl equivalent command for windows Command alix123 Programming 7 08-19-2005 02:23 AM
Getting PID from a system command in Linux perl script sumbabs Linux - General 1 03-03-2005 10:38 AM
how do i command linux to recognize the new system? ph4tk1dd Linux - Laptop and Netbook 5 08-09-2004 06:00 PM
How to call the arp linux shell command from within a perl program Bassam Programming 1 03-11-2004 02:00 AM
C program code calling a Linux system command Linh Programming 1 06-05-2003 01:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration