0

I want to delete/comment one of the ssh key in my remote server . current node = node1 remote node = node2 In node one I have pub file and the content of the pub file need to delete from remote node /home/cloud-user/.ssh/authorized_keys . Tried with sed command but its not worked. Below Command tried in my setup .

test1

pub file name = cluster.pub (content of pub includes space )

key=$(cat  /opt/key/cluster.pub)
ssh -i key.pem node2 "sed -i 's/$key/#$key/g'  /home/cloud-user/.ssh/authorized_keys"

error msg

sed: -e expression #1, char 0: no previous regular expression

test2

copied the cluster.pub into node2:/tmp/

ssh -i key.pem node2   "key= cat '/tmp/cluster_login.pub' ;sed -e 's/${key}/#${key}/'    /home/cloud-user/.ssh/authorized_keys"

error msg

sed: -e expression #1, char 230: unknown option to `s'

Looks like while running in remote shell sed command is executing in a different method .

Barmar
  • 741,623
  • 53
  • 500
  • 612
SARATH CHANDRAN
  • 307
  • 1
  • 6
  • 16
  • The first error will happen if `$key` is empty. Put `set -x` at the beginning of the script to see a trace with variables expanded. – Barmar Jun 18 '23 at 07:53
  • In the second code you're missing `$()` around `cat /tmp/cluster_login.pub`. You also need to escape all the `$` characters so they'll be sent to the server, not ecxxpanded locally. – Barmar Jun 18 '23 at 07:54
  • 1
    It would be easier if you put the remote code in a remote script. Then you could do `ssh node2 /path/to/script` – Barmar Jun 18 '23 at 07:56
  • if there are unexpected chars for regexp https://askubuntu.com/questions/395602/how-to-replace-strings-in-file-without-regex – yvs2014 Jun 18 '23 at 09:11

1 Answers1

0

tried the below option which is worked for me .

ssh node2 "sed -i '/$(echo "$var" | sed 's/[\/&]/\\&/g')/ s/^/#/' /home/cloud-user/.ssh/authorized_keys"
SARATH CHANDRAN
  • 307
  • 1
  • 6
  • 16