LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 09-24-2010, 01:06 PM   #1
bluethundr
Member
 
Registered: Jun 2003
Location: Summit, NJ
Distribution: CentOS 5.4
Posts: 122

Rep: Reputation: 15
Post 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
 
Old 09-24-2010, 03:49 PM   #2
alunduil
Member
 
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

Rep: Reputation: 62
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
 
Old 09-24-2010, 09:27 PM   #3
bluethundr
Member
 
Registered: Jun 2003
Location: Summit, NJ
Distribution: CentOS 5.4
Posts: 122

Original Poster
Rep: Reputation: 15
Question 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
 
Old 09-24-2010, 10:45 PM   #4
alunduil
Member
 
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

Rep: Reputation: 62
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
 
Old 09-24-2010, 11:19 PM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,689

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
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>
 
Old 09-25-2010, 12:09 PM   #6
alunduil
Member
 
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

Rep: Reputation: 62
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
 
  


Reply

Tags
bash, bash scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parse file from remote server to caculate count of string existence in that file saurabhmehan Linux - Newbie 2 08-30-2010 01:30 AM
mirroring my local master server contents to another server located at remote site raasips Linux - Newbie 3 02-04-2010 08:41 PM
grep a shell script variable contents kushalkoolwal Programming 8 02-04-2009 07:15 AM
Output contents to file on server from konsole helptonewbie Linux - Networking 4 02-13-2007 06:00 PM
Backing up the contents of a file server to DVD? CatchFlipsidE Linux - Newbie 2 09-23-2004 08:36 PM


All times are GMT -5. The time now is 12:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration