[CentOS] grep contents of file on remote server

Robert Heller heller at deepsoft.com
Fri Sep 24 16:24:45 EDT 2010


At Fri, 24 Sep 2010 13:50:11 -0400 CentOS mailing list <centos at 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]
> [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`
                                           ^                    ^
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 root at 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
> 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]
> _______________________________________________
> CentOS mailing list
> CentOS at 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
heller at deepsoft.com       -- Contract Programming: C/C++, Tcl/Tk

                                                                            


More information about the CentOS mailing list