[CentOS] grep contents of file on remote server

Fri Sep 24 17:50:11 UTC 2010
Tim Dunphy <bluethundr at gmail.com>

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]
[bluethundr at LBSD2:~]$:ssh root at sum1 cat /root/id_rsa.pub
root at 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-==
bluethundr at 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]
[bluethundr at LBSD2:~]$:ssh root at sum1 grep `cat /root/id_rsa.pub`
/root/.ssh/id_rsa.pub
root at 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-==
bluethundr at lbsd8-2.summitnjhome.com==: No such file or directory
grep: root at 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 root@$h:~/
 if   [ $? = 0 ]; then
  echo ;  echo ;  echo
  echo "KEY TRANSFERRED TO $h"
 else
   echo "KEY Transfer To $h has FAILED"
   exit 1
 fi
 ssh root@$h $CAT /root/id_rsa.pub | $GREP -i /root/.ssh/authorized_keys
 if [ $? = 1 ]; then
  ssh root@$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]
[bluethundr at LBSD2:~/bin]$:./key-export.sh
root at 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
root at lcent01.summitnjhome.com's password:
[/code]

And I'm pretty sure I have those variables set correctly in order to
execute those commands:

[code]
[bluethundr at LBSD2:~/bin]$:ssh root at sum1
root at 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            #
#                                                       #
#########################################################


[root at LCENT01:~]#which grep
/bin/grep
[root at LCENT01:~]#which cat
/bin/cat
[/code]