Results 1 to 2 of 2

Thread: ssh-agent, ssh-add and ssh in a script

  1. #1
    Join Date
    Jun 2008
    Location
    UK
    Beans
    131
    Distro
    Kubuntu 16.04 Xenial Xerus

    ssh-agent, ssh-add and ssh in a script

    For work I often have to SSH into another machine, which I can do by running the following three commands in succession from the command line:
    Code:
    eval `ssh-agent`
    Code:
    ssh-add -l | grep -q `ssh-keygen -lf ~/.ssh/id_rsa_MACHINE_NAME  | awk '{print $2}'` || ssh-add ~/.ssh/id_rsa_MACHINE_NAME
    Code:
    ssh -AY USERNAME@HOST
    However, it's a bit annoying to write these commands out every time (even though I have them saved in a text file so I can just copy and paste them). But when I put them in a script as follows
    Code:
    #!/bin/bash
    eval `ssh-agent`
    ssh-add -l | grep -q `ssh-keygen -lf ~/.ssh/id_rsa_MACHINE_NAME  | awk '{print $2}'` || ssh-add ~/.ssh/id_rsa_MACHINE_NAME
    ssh -AY USERNAME@HOST
    it doesn't work. When I run the ssh-agent command from the command line I get a message like "Agent pid N" and when I run the ssh-add command I have to put in my passphrase. But when run from my script they don't seem to have any effect. Is there a way of putting these into a script so I can just run that script, type in my passphrase and be logged in to the remote machine automatically?

  2. #2
    Join Date
    Mar 2011
    Location
    19th Hole
    Beans
    Hidden!
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: ssh-agent, ssh-add and ssh in a script

    Thread moved to General Help as the more appropriate forum.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •