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 Code:
[bluethundr@LBSD2:~]$:ssh root@sum1 grep `cat /root/id_rsa.pub` /root/.ssh/id_rsa.pub Code:
#!/bin/sh Code:
[bluethundr@LBSD2:~/bin]$:./key-export.sh Code:
[bluethundr@LBSD2:~/bin]$:ssh root@sum1 |
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" Alunduil |
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 |
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 \ Alunduil |
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> |
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> \ Alunduil |
All times are GMT -5. The time now is 05:13 AM. |