At Fri, 24 Sep 2010 13:50:11 -0400 CentOS mailing list <centos@centos.org> 
wrote:

> 
> Hello,
> 
>  I am attempting to grep the contents of a key file I have SCP'd to a
> remote server. I am able to cat it:
> 
> [code]
> [bluethu...@lbsd2:~]$:ssh r...@sum1 cat /root/id_rsa.pub
> r...@lcent01.summitnjhome.com's password:
> ssh-rsa 
> AAAAB3NzaC1yc2EAAAABIwAAAQEApnUSYyrM96qIBZKjwSNYycgeSv/FAKE-KEY-DATA--KEY-DATA-PWReyVuOn9Fb/uH/FAKE-KEY-DATA-+ttLzUELGrfn/n+FAKE-KEY-DATA-/FAKE-KEY-DATA-/FAKE-KEY-DATA-/FAKE-KEY-DATA-==
> bluethu...@lbsd8-2.summitnjhome.com
> [/code]
> 
> But I cannot cat / grep it in order to determine if this key is
> already in the authorized_hosts file of the remote host.
> 
> [code]
> [bluethu...@lbsd2:~]$:ssh r...@sum1 grep `cat /root/id_rsa.pub`
                                           ^                    ^
Why the backticks around cat? The above evaluates the *local*
/root/id_rsa.pub, and then passes the result lines as arguments
(filenames) to grep on the remote machine, which of course makes no
sense...

What does the output of 

ssh r...@sum1 grep `hostname` /root/id_rsa.pub

display?  You don't need to cat the file to grep it.  Grep does
understand how to use fopen() all by itself, it does not need any help
from cat... :-)


> /root/.ssh/id_rsa.pub
> r...@lcent01.summitnjhome.com's password:
> /root/.ssh/id_rsa.pub:ssh-rsa ssh-rsa
> AAAAB3NzaC1yc2EAAAABIwAAAQEApnUSYyrM96qIBZKjwSNYycgeSv/FAKE-KEY-DATA--KEY-DATA-PWReyVuOn9Fb/uH/FAKE-KEY-DATA-+ttLzUELGrfn/n+FAKE-KEY-DATA-/FAKE-KEY-DATA-/FAKE-KEY-DATA-/FAKE-KEY-DATA-==
> bluethu...@lbsd8-2.summitnjhome.com==: No such file or directory
> grep: r...@bt-laptop: No such file or directory
> [/code]
> 
> Ultimately, what I would like to do is script this in order to
> automate this process:
> 
> [code]
> #!/bin/sh
> HOSTS="sum1 sum2 virt1 virt2 virt3 virt4 virt5 virt6 virt7"
> SSHDIR=~/.ssh
> RSYNC=/usr/local/bin/rsync
> KEYFILE=/home/bluethundr/.ssh/id_rsa.pub
> CAT='/bin/cat'
> GREP='/bin/grep'
> 
> for h in $HOSTS ; do
>  scp $KEYFILE r...@$h:~/
>  if   [ $? = 0 ]; then
>   echo ;  echo ;  echo
>   echo "KEY TRANSFERRED TO $h"
>  else
>    echo "KEY Transfer To $h has FAILED"
>    exit 1
>  fi
>  ssh r...@$h $CAT /root/id_rsa.pub | $GREP -i /root/.ssh/authorized_keys
>  if [ $? = 1 ]; then
>   ssh r...@$h $CAT /root/id_rsa.pub >> /root/.ssh/authorized_keys
>   if   [ $? = 0 ]; then
>   echo ;  echo ;  echo
>   echo "KEY APPENDED TO $h Authorized Hosts"
>  else
>    echo "KEY APPEND FAILED"
>  fi
>  exit 1
> fi
> done
> [/code]
> 
> This is what results from the above script:
> 
> [code]
> [bluethu...@lbsd2:~/bin]$:./key-export.sh
> r...@lcent01.summitnjhome.com's password:
> id_rsa.pub
>                                                         100%  417
> 0.4KB/s   00:00
> 
> 
> 
> KEY TRANSFERRED TO sum1
> ./key-export.sh: /bin/grep: not found
> r...@lcent01.summitnjhome.com's password:
> [/code]
> 
> And I'm pretty sure I have those variables set correctly in order to
> execute those commands:
> 
> [code]
> [bluethu...@lbsd2:~/bin]$:ssh r...@sum1
> r...@lcent01.summitnjhome.com's password:
> Last login: Fri Sep 24 07:34:02 2010 from 192.168.1.44
> #########################################################
> #               SUMMITNJHOME.COM                        #
> #               TITLE:       LCENT01  BOX               #
> #               LOCATION:    SUMMIT BASEMENT            #
> #                                                       #
> #########################################################
> 
> 
> [r...@lcent01:~]#which grep
> /bin/grep
> [r...@lcent01:~]#which cat
> /bin/cat
> [/code]
> _______________________________________________
> CentOS mailing list
> CentOS@centos.org
> http://lists.centos.org/mailman/listinfo/centos
> 
>                                      

-- 
Robert Heller             -- Get the Deepwoods Software FireFox Toolbar!
Deepwoods Software        -- Linux Installation and Administration
http://www.deepsoft.com/  -- Web Hosting, with CGI and Database
hel...@deepsoft.com       -- Contract Programming: C/C++, Tcl/Tk

                                                                            
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos

Reply via email to