sameerperl has asked for the wisdom of the Perl Monks concerning the following question:

can any one tell me how to stop ssh prompt for password while executing command from scriot. Just want to execute command from script and inputs password. Thanks Sameer

Replies are listed 'Best First'.
Re: how stop ssh prompt for password
by Tanktalus (Canon) on Jun 20, 2008 at 22:02 UTC

    What do you have so far? It'll be far easier to help you if we know how you're approaching the problem than just flying blind here.

      Net::SSH. UPDATE: I thought I typed the 2. Honestly, I didn't spend much time on the post.

      -Paul

        Actually Net::SSH won't let you input a password. Number one item on the FAQ in it's docs FWIW. It expects you to use shared keys for passwordless login. You can use Net::SSH::Expect, Net::SSH2 or Net::SSH::Perl which will let you programatically input a password.

Re: how stop ssh prompt for password
by quester (Vicar) on Jun 21, 2008 at 09:00 UTC

    What you probably really want to do is to use the ssh-keygen command to generate a key pair, and use the public and private keys to authenticate without passwords.

    There is a super-brief how-to at http://hkn.eecs.berkeley.edu/~dhsu/ssh_public_key_howto.html. For more detail, look at the output of man ssh-keygen (and man ssh). You could also google for ssh-keygen and the name of the SSH package you are using (OpenSSH, PuTTY, Tectia, etc).

Re: how stop ssh prompt for password
by Khen1950fx (Canon) on Jun 21, 2008 at 07:07 UTC
    Here's an example for inputting passwords:

    #!/usr/bin/perl use strict; use warnings; use Net::SSH::Perl; use Term::ReadKey; my $host = 'localhost'; my $username = 'username'; my $cmd = '/sbin/ifconfig'; warn "Starting ssh: "; my $ssh = Net::SSH::Perl->new($host, protocol => '2,1', debug => 1); print "done", "\n"; print "Your password: "; ReadMode('noecho'); chomp(my $password = ReadLine(0)); ReadMode('restore'); print "\n"; warn "Starting login: "; $ssh->login($username, $password); print "login done", "\n"; warn "Starting command: "; my($stdout, $stderr, $exit) = $ssh->cmd($cmd); print $stdout, "\n";
Re: how stop ssh prompt for password
by casiano (Pilgrim) on Jun 22, 2008 at 09:50 UTC
    SSH includes the ability to authenticate users using public keys. Instead of authenticating the user with a password, the SSH server on the remote machine will verify a challenge signed by the user's private key against its copy of the user's public key. To have a full programming control of the machine with Perl and automatic ssh-authentication you can do it this way:

    • Install GRID::Machine. It will help you controlling SSH from Perl. You will have Perl RPC at hand.
    • Generate a public key use the ssh-keygen utility. For example:

        local.machine$ ssh-keygen -t rsa -N ''
      

      The option -t selects the type of key you want to generate. There are three types of keys: rsa1, rsa and dsa. The -N option is followed by the passphrase. The -N '' setting indicates that no pasphrase will be used. This is useful when used with key restrictions or when dealing with cron jobs, batch commands and automatic processing which is the context in which this module was designed. If still you don't like to have a private key without passphrase, provide a passphrase and use ssh-agent to avoid the inconvenience of typing the passphrase each time. ssh-agent is a program you run once per login sesion and load your keys into. From that moment on, any ssh client will contact ssh-agent and no more passphrase typing will be needed.

      By default, your identification will be saved in a file /home/user/.ssh/id_rsa. Your public key will be saved in /home/user/.ssh/id_rsa.pub.

    • Once you have generated a key pair, you must install the public key on the remote machine. To do it, append the public component of the key in

                 /home/user/.ssh/id_rsa.pub
      

      to file

                 /home/user/.ssh/authorized_keys
      

      on the remote machine. If the ssh-copy-id script is available, you can do it using:

        local.machine$ ssh-copy-id -i ~/.ssh/id_rsa.pub user@remote.machine
      

      Alternatively you can write the following command:

        $ ssh remote.machine "umask 077; cat >> .ssh/authorized_keys" < /home/user/.ssh/id_rsa.pub
      
      

      The umask command is needed since the SSH server will refuse to read a /home/user/.ssh/authorized_keys files which have loose permissions.

    • Edit your local SSH configuration file /home/user/.ssh/config (see man ssh_config in UNIX) and create a new section for automatic connections to that host. Here follows an example:

       ...
      
       # A new section inside the config file: 
       # it will be used when writing a command like: 
       #                     $ ssh gridyum
      
       Host gridyum
      
       # My username in the remote machine
       user my_login_in_the_remote_machine
      
       # The actual name of the machine: by default the one provided in the
       # command line
       Hostname real.machine.name
      
       # The port to use: by default 22
       Port 2048
      
       # The identitiy pair to use. By default ~/.ssh/id_rsa and ~/.ssh/id_dsa
       IdentityFile /home/user/.ssh/yumid
      
       # Useful to detect a broken network
       BatchMode yes
      
       # Useful when the home directory is shared across machines,
       # to avoid warnings about changed host keys when connecting
       # to local host
       NoHostAuthenticationForLocalhost yes
      

       # Another section ...
       Host another.remote.machine an.alias.for.this.machine
       user mylogin_there
      
       ...
      

      This way you don't have to specify your login name on the remote machine even if it differs from your login name in the local machine, you don't have to specify the port if it isn't 22, etc. This is the recommended way to work with GRID::Machine. Avoid cluttering the constructor new.

    • Once the public key is installed on the server you should be able to authenticate using your private key

        $ ssh remote.machine
        Linux remote.machine 2.6.15-1-686-smp #2 SMP Mon Mar 6 15:34:50 UTC 2006 i686
        Last login: Sat Jul  7 13:34:00 2007 from local.machine
        user@remote.machine:~$
      
      

      You can also automatically execute commands in the remote server:

        local.machine$ ssh remote.machine uname -a
        Linux remote.machine 2.6.15-1-686-smp #2 SMP Mon Mar 6 15:34:50 UTC 2006 i686 GNU/Linux
      

    Hope it helps

    Casiano