Re: grep contents of file on remote server

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



At Fri, 24 Sep 2010 13:50:11 -0400 CentOS mailing list <centos@xxxxxxxxxx> 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@LBSD2:~]$:ssh root@sum1 cat /root/id_rsa.pub
> root@xxxxxxxxxxxxxxxxxxxxxxxx'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@xxxxxxxxxxxxxxxxxxxxxxxx
> [/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@LBSD2:~]$:ssh root@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@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@xxxxxxxxxxxxxxxxxxxxxxxx'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@xxxxxxxxxxxxxxxxxxxxxxxx==: No such file or directory
> grep: root@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@LBSD2:~/bin]$:./key-export.sh
> root@xxxxxxxxxxxxxxxxxxxxxxxx's password:
> id_rsa.pub
>                                                         100%  417
> 0.4KB/s   00:00
> 
> 
> 
> KEY TRANSFERRED TO sum1
> ./key-export.sh: /bin/grep: not found
> root@xxxxxxxxxxxxxxxxxxxxxxxx's password:
> [/code]
> 
> And I'm pretty sure I have those variables set correctly in order to
> execute those commands:
> 
> [code]
> [bluethundr@LBSD2:~/bin]$:ssh root@sum1
> root@xxxxxxxxxxxxxxxxxxxxxxxx'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@LCENT01:~]#which grep
> /bin/grep
> [root@LCENT01:~]#which cat
> /bin/cat
> [/code]
> _______________________________________________
> CentOS mailing list
> CentOS@xxxxxxxxxx
> 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@xxxxxxxxxxxx       -- Contract Programming: C/C++, Tcl/Tk

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


[Index of Archives]     [CentOS]     [CentOS Announce]     [CentOS Development]     [CentOS ARM Devel]     [CentOS Docs]     [CentOS Virtualization]     [Carrier Grade Linux]     [Linux Media]     [Asterisk]     [DCCP]     [Netdev]     [Xorg]     [Linux USB]
  Powered by Linux