LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   grep contents of file on remote server (https://www.linuxquestions.org/questions/linux-newbie-8/grep-contents-of-file-on-remote-server-834295/)

bluethundr 09-24-2010 12:06 PM

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


alunduil 09-24-2010 02:49 PM

So just to make sure I understand. You want to pass a key file and a host and check if that key is in the authorized_keys of the host?

If so you should be able to do the following:

Code:

ssh [user@]<host> 'cat .ssh/authorized_keys' | grep "$(cat <keyfile>)" &>/dev/null && echo "FOUND" || echo "NOT FOUND"
Regards,

Alunduil

bluethundr 09-24-2010 08:27 PM

grep contents of file on remote server
 
Hello,

Thanks for your reply. Yes I am passing a key file to a remote server and then checking if it is already in the list of authorized_keys.

If so, I am attempting to append it to said key. Also (not attempted yes, but would be nice) if I could have some advice on how to check the remote authorized_keys file for duplicate keys would be great.

Regards

alunduil 09-24-2010 09:45 PM

Alright, so you can do that with the script I mentioned earlier in the following way:

Code:

ssh [user@]<host> 'cat .ssh/authorized_keys' | grep "$(cat <keyfile>)" &>/dev/null \
&& echo "Already Installed" \
|| ssh [user@]<host> 'cat >> .ssh/authorized_keys' < <keyfile>

Regards,

Alunduil

grail 09-24-2010 10:19 PM

Is there a reason we need to use cat all over the place?
I am not a ssh guru so could complications occur?
Other wise I would do the following:
Code:

ssh [user@]<host> grep -q -f <keyfile> .ssh/authorized_keys && echo "Already Installed" || ssh [user@]<host> 'cat >> .ssh/authorized_keys' < <keyfile>

alunduil 09-25-2010 11:09 AM

It's because of where the files are and how the SSH process is going to look for them. In your example, the `-f <keyfile>` will look on the remote machine for the keyfile rather than the local machine. You did suggest an improvement to my script though:

Code:

ssh [user@]<host> 'cat .ssh/authorized_keys' | grep -qf <keyfile> \
&& echo "Already Installed" \
|| ssh [user@]<host> 'cat >> .ssh/authorized_keys' < <keyfile>

Regards,

Alunduil


All times are GMT -5. The time now is 05:13 AM.