in reply to One liner: remove ssh keys with quotemeta

Hi Guys,
so, getting it to the cmd line wasn't the problem, it was the interpretation by Perl eg
Puppet out: Debug: Exec[line_remove](provider=posix): Executing '/usr/bin/perl -w +-ni -e 'my $qs = quotemeta('ssh-rsa AAAA/BBBB/knMQ== user@host.f.q.d. +n'); print unless /^$qs$/ ' '/root/user/b1.bak'' Debug: Executing: '/usr/bin/perl -w -ni -e 'my $qs = quotemeta('ssh-rs +a AAAA/BBBB/knMQ== user@host.f.q.d.n'); print unless /^$qs$/ ' '/roo +t/user/b1.bak'' Notice: /Stage[main]/Main/Line[ssh_key]/Exec[line_remove]/returns: Unq +uoted string "ssh" may clash with future reserved word at -e line 1. Notice: /Stage[main]/Main/Line[ssh_key]/Exec[line_remove]/returns: syn +tax error at -e line 1, at EOF Notice: /Stage[main]/Main/Line[ssh_key]/Exec[line_remove]/returns: Exe +cution of -e aborted due to compilation errors. ## Same errors as direct at the cmd line # /usr/bin/perl -w -ni -e 'my $qs = quotemeta('ssh-rsa AAAA/BBBB/knMQ= += user@host.f.q.d.n'); print unless /^$qs$/ ' '/root/user/b1.bak' Unquoted string "ssh" may clash with future reserved word at -e line 1 +. syntax error at -e line 1, at EOF Execution of -e aborted due to compilation errors. # Try swap outer quotes '-> " # /usr/bin/perl -w -ni -e "my $qs = quotemeta('ssh-rsa AAAA/BBBB/knMQ= += user@host.f.q.d.n'); print unless /^$qs$/ " '/root/user/b1.bak' syntax error at -e line 1, near "my =" Execution of -e aborted due to compilation errors. # Try "" around quotemeta string # /usr/bin/perl -w -ni -e 'my $qs = quotemeta("ssh-rsa AAAA/BBBB/knMQ= += user@host.f.q.d.n"); print unless /^$qs$/ ' '/root/user/b1.bak' Possible unintended interpolation of @host in string at -e line 1. Name "main::host" used only once: possible typo at -e line 1.

However, using the post by gam3 here https://www.perlmonks.org/?node_id=438860 ('general soln'), it seemed to work
# Direct cli # perl -w -ni -e 'my $str = sprintf( qq(%s), q(ssh-rsa AAAA/BBBB/knMQ= += user@host.f.q.d.n) ); print $_ unless $_ =~ /$str/ ' /root/user/b1 +.bak # From within puppet command => "/usr/bin/perl -w -ni -e 'my \$str = sprintf( qq(%s), q($li +ne) ); print \$_ unless \$_ =~ /\$str/ ' '${file}'",

But a full ssh key has a '+' char as well, and that fails to match :(
# cat b1.bak ssh-rsa CCCC/DDDD/knMQ== user@host2.f.q.d.n ssh-rsa AAAA/BBBB/kPW+Yi9yZ7Kh0mL/knMQ== user@host.f.q.d.n # perl -w -n -e 'my $str = sprintf( qq(%s), q(ssh-rsa AAAA/BBBB/kPW+Y +i9yZ7Kh0mL/knMQ== user@host.f.q.d.n) ); print $_ unless $_ =~ /$str/ +' /root/user/b1.bak ssh-rsa CCCC/DDDD/knMQ== user@host2.f.q.d.n ssh-rsa AAAA/BBBB/kPW+Yi9yZ7Kh0mL/knMQ== user@host.f.q.d.n

So close ... damn it ... looks like I DO need to get quotemeta() working...
An ideas?

Cheers
Chris

BTW, when I write the post for perlmonks, it uses all the whitespace, but when I actually commit the post, it crams all code stuff to one side; why ?

Replies are listed 'Best First'.
Re^2: One liner: remove ssh keys with quotemeta
by hippo (Archbishop) on Nov 29, 2019 at 09:30 UTC

    As per my previous reply, discarding the regex and just using string equality:

    $ cat b1.bak ssh-rsa CCCC/DDDD/knMQ== user@host2.f.q.d.n ssh-rsa AAAA/BBBB/kPW+Yi9yZ7Kh0mL/knMQ== user@host.f.q.d.n $ perl -lne 'print unless $_ eq q(ssh-rsa AAAA/BBBB/kPW+Yi9yZ7Kh0mL/kn +MQ== user@host.f.q.d.n)' b1.bak ssh-rsa CCCC/DDDD/knMQ== user@host2.f.q.d.n
      Hey mate,

      thanks for that.
      It turns out you were right and in fact we don't need to do all the escapes / quotemeta stuff !
      I can't believe it's that simple - I was convinced the previous guy was on the right track...

      To be fair, I've done a lot of Perl programming in the past, but almost never one liners like this (especially also having to worry about passing it through the shell etc).

      Final code (inside puppet)
      exec { "/usr/bin/perl -i -lne 'print unless \$_ eq q($line)' '${file} +'":
      BTW, that -l switch is magic :)

      Cheers
      Chris
Re^2: One liner: remove ssh keys with quotemeta
by jcb (Parson) on Nov 29, 2019 at 17:27 UTC

    If you insist on this approach, try inserting quotemeta just before sprintf.