Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Ssh Keys

by j^2 (Novice)
on Nov 28, 2005 at 18:34 UTC ( #512315=CUFP: print w/replies, xml ) Need Help??

I put this together today at work, it's nothing special but i think it's pretty neat for a first perl script. I hope ya'll like it.
#!/usr/bin/perl # # To set up ssh keys # # set up "global" vars $home = $ENV{"HOME"}; $remotemachine = $ARGV[0]; $user = $ENV{"USER"}; # see if keys exist if (-e !"/$home/.ssh/id_dsa.pub") { `ssh-keygen -t dsa` }; `scp /$home/.ssh/id_dsa.pub $remotemachine://home/$user/.ssh/authorize +d_keys2`; `ssh-agent sh -c 'ssh-add < /dev/null && bash'`; `ssh $remotemachine`; exit (0);

Replies are listed 'Best First'.
Re: Ssh Keys
by belg4mit (Prior) on Nov 28, 2005 at 18:51 UTC
    1. Useless use of back ticks, try system or exec.

      ...and so your test is probably not doing what you think it is. spoke too soon.

      Or maybe you intended to discard the output as a side-effect, do it explicitly with /dev/null or some such.

    2. Why not use the environment variables directly?
    3. What if I don't have a $HOME? ;-)

    UPDATE:

    #Globals are bad mojo my $remotemachine = $ARGV[0]; # see if keys exist if (-e !"/$home/.ssh/id_dsa.pub"){ system('ssh-keygen', '-t', 'dsa'); } #Only use semi-colon's after do{} blocks. system('scp', "/$ENV{HOME}/.ssh/id_dsa.pub, "$remotemachine://home/$EN +V{USER}/.ssh/authorized_keys2"); system('ssh-agent', q(sh -c 'ssh-add < /dev/null && bash')); exec('ssh', $remotemachine); #exit (0); #That's the default

    --
    In Bob We Trust, All Others Bring Data.

      Ah neat! thanks for your help. Can i ask why global vars are "bad mojo"?

        Globals are bad in just about any programming language because it's so easy to tinker with the value wherever you are in the code. The flipside of that is, when Something Bad happens in the code, you have no idea where that global is being changed.

        A better coding style involves limiting a variable to the smallest possible scope, like within a small piece of code, such as within an object definition, where you can control the setters and getters of the object's attributes.

        And if you need a rule, it's probably something like "Global variables should be the exceptions, not the rule".

        Alex / talexb / Toronto

        "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: Ssh Keys
by merlyn (Sage) on Dec 01, 2005 at 19:51 UTC
    Hmm. Just noticed that everyone so far seems to have missed this amazing typo:
    if (-e !"/$home/.ssh/id_dsa.pub")
    which should be:
    if (! -e "/$home/.ssh/id_dsa.pub")
    Otherwise, you're just checking the existance of a file named "" (undef, coerced to a string). {grin}

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Ssh Keys
by talexb (Canon) on Nov 28, 2005 at 18:53 UTC

    Umm .. I see three or four system calls, and a single conditional. That's not much Perl; and there isn't even any error checking on the system calls.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

      yeah i understand. i just thought i might submit it to perlmonks to get some feed back. i'm really trying to learn perl and wanted to see what more experianced users thought.
        I think the criticism is accurate, but I definitely do a lot of little glue programs that aren't interesting in terms of Perl, but are incredibly useful pragmatically. So, while programs like this aren't that interesting on a Perl-centric site like PM, pragmatic programs are good tools to build. I really enjoy it when someone in my lab asks me how to do something and I say "Aha! I have a script for that. Let me show you."

        Here's one more small criticism: most people make ssh all caps or all lowercase.

        Another thing you might do with your script is verify that permissions are correctly set on the .ssh directory and the authorized_keys file. Maybe add a usage message. Toss it into the Code Catacombs. Or even better, poke around there. I'm sure you're not the first person to do this. It's interesting to see how other people code programs to do the same thing, in terms of style, documentation and cool tricks. Reading the code of others critically is a great way to learn, and is one reason that I love PM.

        Cheers and good luck with your programming.

        It's great to jump in and get your feet wet with the language .. my only criticism was that your example didn't contain that much Perl.

        But keep trying things, reading posts and asking questions .. I'm sure you'll learn fast.

        Alex / talexb / Toronto

        "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: Ssh Keys
by andyford (Curate) on Dec 01, 2005 at 19:00 UTC
    One thing I always thought was really neat when I learned it is the "unless" conditional.
    if (-e !"/$home/.ssh/id_dsa.pub")
    becomes
    unless (-e "/$home/.ssh/id_dsa.pub")
    Once you grok that, you can go one step further and say
    if (-e !"/$home/.ssh/id_dsa.pub") { `ssh-keygen -t dsa` };
    becomes
    `ssh-keygen -t dsa` unless (-e "/$home/.ssh/id_dsa.pub");

    Update: Bad advice withdrawn

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://512315]
Approved by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2018-10-17 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...














    Results (90 votes). Check out past polls.

    Notices?