Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

how to send input to stdin automatically?

by skyworld_chen (Acolyte)
on Dec 18, 2012 at 06:35 UTC ( #1009286=perlquestion: print w/replies, xml ) Need Help??
skyworld_chen has asked for the wisdom of the Perl Monks concerning the following question:


Is there a way to send the input to stdin directly? e.g when I use "cvs update", I always need to answer the password with ssh. If ignore security problem, is there a way I can write a script to run "cvs update" and answer password automatically? thanks.

  • Comment on how to send input to stdin automatically?

Replies are listed 'Best First'.
Re: how to send input to stdin automatically?
by Utilitarian (Vicar) on Dec 18, 2012 at 09:04 UTC
    As a general rule you should enable ssh key exchange on your cvs repository and then the question becomes moot.
    From your development machine:
    ssh-keygen -t dsa (accept the default for all queries) $ cat ~/.ssh/ | ssh ${repository_host} "mkdir -p .ssh ; cat +>> .ssh/authorized_keys" (enter your password) $ ssh ${repository_host}echo (answer "yes") $ ssh ${repository_host} echo (Nothing should happen, you should just get your prompt back)
    You need to set the following environment variables (put them in your .profile or .bashrc) before you can run any cvs command:
    $ export CVS_RSH=ssh $ export CVSROOT=${username}@${repositry_host}:/path/to/cvsroot/
    Hope that helps

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
      Hi all,

      I'm a new comer for Perl, both SSH and Expect seems difficult to me. I need some time to understand them. Thanks for all of your kind replies.

Re: how to send input to stdin automatically?
by tobyink (Abbot) on Dec 18, 2012 at 07:56 UTC

    Many programs that read passwords do not read them from STDIN - they read keystrokes from the terminal. If CVS is one of those, then sending data to its STDIN will not help you.

    But yes, you can run a program from Perl and pipe data to its STDIN...

    open my $pipe, '|-', 'some-program arg1 arg2'; print {$pipe} "some data\n"; close $pipe;

    If you also need to read from that program's STDOUT or STDERR, check out IPC::Open2, IPC::Open3 or System::Command.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      ssh (as most programs asking for a password) does not read the password from stdin but from the process tty, so not opening a pipe neither using any of the modules you have named will work.

      The usual solution to automate password authentication is to use Expect.

      Hi tobyink,

      thanks for your kind reply. You are right, I do need to send password to keyboard, not STDIN. I have tried code you suggested, but the linux still asks for the password. My code is as this:

      #!/usr/bin/perl open my $pipe,'|-', 'cvs update'; print {$pipe} "my_password\n"; close $pipe;

      did I miss something? thanks.

Re: how to send input to stdin automatically?
by graff (Chancellor) on Dec 18, 2012 at 08:43 UTC
    I think you'll need to look at Expect (or one of its variants).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1009286]
Approved by LanX
[choroba]: Isn't Using PerlPod Creatively rather a meditation?
[choroba]: I don't see a question
[1nickt]: ugh, I stuck my head in the bass bin for 30 seconds on a dare at Ted Nugent at Hammersmith Odeon. Yes, I am 40% deaf now.
[johngg]: My daughter is incredibly jealous of my wife who got to see The Clash at Brixton many years ago. They went to see The Vaccines there recently.
[1nickt]: But the bands are even louder! I saw Spearhead (Michael Franti) at an outdoor show and had to walk a mile away to not feel pain in my chest! Babies were crying ... I asked the sound engineer why it was necessary to have the bass so loud and he laughed...
[Discipulus]: but the best i attended live was Mano Negra Patchanka at Forte Prenestino .. in 1990
[Corion]: Hmmm - Mano Negra or at least Manu Chao seem to put on a good live show. At least the one live CD I have from Manu Chao sounds good ;)
Discipulus feels the same jealousity of the johngg's daughter
[1nickt]: choroba I agree
[choroba]: Playing in a punk rock band for 20 years... my hearing is quite bad

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (13)
As of 2017-03-24 12:17 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (301 votes). Check out past polls.