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

Here's the deal: I am writing a script to perform several system monitoriing tasks, one of which is checking disk partition space. My question is this - Once I have used ssh to access my machine, how may I implement scripting that will enter my username and password. Granted, this is a security hazard under many circumstances, but as access to my monitoring tool will be limited, I would like to script an auto-login and password feature. Any ideas?

Replies are listed 'Best First'.
Re: Auto login and password entry
by ncw (Friar) on Sep 20, 2000 at 23:33 UTC
    If you are using ssh then you are half way there!

    You need to generate yourself an identity using ssh-keygen on the local machine. You then copy the public part of this into the ~/.ssh/authorized_keys file on the remote server.

    You'll then be able to log into the remote machine without using a password (provided you didn't set a password on the identity you made).

    This requires that you keep your identity secret, but you are proposing putting the username and password in a file which is just as bad.

    This is the way I use ssh for all our system administration. We have a web page showing disk usage on all or our servers generated in exactly this way.

    Once you've got it working you can do this

    # ssh myremoteserver.net 'df -k' Filesystem 1024-blocks Used Available Capacity Mounted on /dev/sda3 17092820 2255644 13954808 14% / /dev/sdb4 30639063 11498971 17533191 40% /home
    Note the lack of login propmt!

    You need to take care passing commands to ssh - if you don't use the list form of system you'll end up running your commands through two shells (one remote, one local) which may be bad for your server health. Use like this

    system("ssh", "-l", "root", "df -k") == 0 or die "Couldn't run command";
    Take care with commands run on the remote server since they will be run through the shell. Judicous use of ''s or \Q\E can help, or use Shell::StringQuote.
      Thanks a million - it looks like it's going to work.
(jcwren) RE: Auto login and password entry
by jcwren (Prior) on Sep 20, 2000 at 20:38 UTC
    Here's a link to a page that talks about logging in via Perl and SSH. It's a little long, so I didn't read it very well. It may or may not be the solution you're looking for.

    This link appears to be some sort of commercial product.
    A link to an Expect module modified for SSH usage.

    If I find anything else, I'll update this node.

    --Chris

    e-mail jcwren
      Much thanks - I'm a beginning programmer. I will check out the link. Any additional input will be appreciated. BlackLab