FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > CentOS > CentOS

 
 
LinkBack Thread Tools
 
Old 09-24-2010, 05:50 PM
Tim Dunphy
 
Default grep contents of file on remote server

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@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@lbsd8-2.summitnjhome.com
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`
/root/.ssh/id_rsa.pub
root@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@lbsd8-2.summitnjhome.com==: No such file or directory
grep: root@bt-laptop: No such file or directory
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
This is what results from the above script:

Code:
[bluethundr@LBSD2:~/bin]$:./key-export.sh
root@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@lcent01.summitnjhome.com's password:
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@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@LCENT01:~]#which grep
/bin/grep
[root@LCENT01:~]#which cat
/bin/cat
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 09-24-2010, 06:08 PM
Les Mikesell
 
Default grep contents of file on remote server

On 9/24/2010 12:50 PM, Tim Dunphy 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 grep `cat /root/id_rsa.pub`

Put single quotes around the whole command you want to send to the
remote. Otherwise your local shell is going to process the backtick
expansion before anything else. And grep is just as capable of reading
the file as cat anyway.

--
Les Mikesell
lesmikesell@gmail.com



_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 09-24-2010, 08:24 PM
Robert Heller
 
Default grep contents of file on remote server

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:
> [bluethundr@LBSD2:~]$:ssh root@sum1 cat /root/id_rsa.pub
> root@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@lbsd8-2.summitnjhome.com
>
>
> 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@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@lbsd8-2.summitnjhome.com==: No such file or directory
> grep: root@bt-laptop: No such file or directory
>
>
> 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
>
>
> This is what results from the above script:
>
>
Code:
> [bluethundr@LBSD2:~/bin]$:./key-export.sh
> root@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@lcent01.summitnjhome.com's password:
>
>
> 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@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@LCENT01:~]#which grep
> /bin/grep
> [root@LCENT01:~]#which cat
> /bin/cat
>
> _______________________________________________
> 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
heller@deepsoft.com -- Contract Programming: C/C++, Tcl/Tk


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

Thread Tools




All times are GMT. The time now is 11:04 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org