Hi.. I'm working on a script that manages sharepoints on our fileservers. We have several fileservers throughout town, and my script is to run locally, and execute commands on a remote fileserver. Everything is Mac OS X. My eventual goal is to implement this over CGI on a secured website, but for now it runs at the prompt. I've tried 4 approaces, with mixed results:

1. $ssh_out = `ssh -2 $user\@$host $ssh_cmd`;
This gets complicated when running multiple commands (I have a few loops that generate a directory hierarchy). I also don't know how to send data to the stdin when prompted, so if my $ssh_cmd starts with a 'sudo', I have to type the password manually.

2. system "ssh -2","$user",'@',"$host","$ssh_cmd";
This gives me similar problems as above, as well as issues with my chgrp command, which must include a backslash as part of the group name.

3. using Net::SSH
I don't immediately see how to pass options to ssh itself, like the -2 or a -v. It also seems difficult, or at least expensive, to work this thru a loop.

4. using Net::SSH::Perl
Because this requires dozens of dependencies, I'm having great difficulty compiling and installing this under OS X.

What's the best way to open an SSH connection with a remote host and run a bunch of commands, some as the super-user, without asking for a password. I've already exchanged SSH keys so I can log in from the terminal without supplying a password (yes, it's insecure, i know).

Thanks

Disclaimer: I'm a self-taught sysadmin, and an even newer self-taught perl enthusiast; this is my first script using cpan modules. No formal training in CompSci or programming. Electronics is my thing.


In reply to Installing Net::SSH::Perl on OS X, or how to run commands remotely by kazeeks

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.