1833012 Members
2797 Online
110048 Solutions
New Discussion

Re: perl pie question

 
SOLVED
Go to solution
Shabu Khan-2
Frequent Advisor

perl pie question


I am struggling to get this substitution going.

I am trying to replace the hash keys in /etc/shadow with new hash keys, but my hash keys has special characters $ and . and all that, I need to use 'perl -p -i -e'

# perl -p -i -e "s/tomcat:\S+?:/tomcat:$1$Ic8qfl8h$cqja.zFziPK.j3bWx4SAL0:/" /etc/shadow

# tail -1 /etc/shadow
tomcat:.zFziPK.j3bWx4SAL0:0:99999:7:::

If you notice it is ignoring from '$' upto 'a' in the string.
I also tried double quotes and single quotes and different combinations.

# HASH='$1$Ic8qfl8h$cqja.zFziPK.j3bWx4SAL0'
# perl -p -i -e "s/tomcat:+?:/tomcat:${HASH}:/" /etc/shadow
# tail -1 /etc/shadow
tomcat:.zFziPK.j3bWx4SAL0:0:99999:7:::

# perl -p -i -e 's/tomcat:\S+?:/tomcat:'${HASH}':/' /etc/shadow
# tail -1 /etc/shadow
tomcat:.zFziPK.j3bWx4SAL0:99999:7:::

# perl -p -i -e 's/tomcat:\S+?:/tomcat:${HASH}:/' /etc/shadow
# tail -1 /etc/shadow
tomcat::99999:7:::

What would be the right combination?

I also tried 'ed' works great, but how do I use a variable with ed?

# EDITOR=/bin/ed
# ed /etc/shadow
1380
,s/^tomcat:.*:99999:7:::/tomcat:$1$Ic8qfl8h$cqja.zFziPK.j3bWx4SAL0/
w
1403
q

# tail -1 /etc/shadow
tomcat:$1$Ic8qfl8h$cqja.zFziPK.j3bWx4SAL0

Please advise.

Thanks,
Shabu
4 REPLIES 4
Shabu Khan-2
Frequent Advisor

Re: perl pie question


I can always backslash the $ and any special characters to get this working, but I am looking for a better solution - without mucking with the actual hash key.

This would work but inelegant.
# HASH='\$1\$Ic8qfl8h\$cqja\.zFziPK\.j3bWx4SAL0'
# perl -p -i -e "s/tomcat:\S+?:/tomcat:${HASH}:/" /etc/shadow
# tail -1 /etc/shadow
tomcat:$1$Ic8qfl8h$cqja.zFziPK.j3bWx4SAL0:0:99999:7:::

Thanks,
Shabu
James R. Ferguson
Acclaimed Contributor
Solution

Re: perl pie question

Hi Shabu:

This is one way:

# cat ./fixshadow
#!/usr/bin/perl
use strict;
use warnings;
my $user = shift or die "User Name expected";
my $hash = shift or die "New password (hash) expected";
local $^I = '.old';
local @ARGV = '/etc/shadow';
while (<>) {
my @a = split(/:/);
if ($a[0] =~ /^$user$/) {
$a[1] = $hash;
$_ = join ':', @a;
}
print;
}
1;

...run as:

# HASH='$1$Ic8qfl8h$cqja.zFziPK.j3bWx4SAL0'
# ./fixshadow tomcat ${HASH}

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: perl pie question

$ cat > shadow
tomcat:.zFziPK.j3bWx4SAL0:0:99999:7:::
$ export HASH='$1$Ic8qfl8h$cqja.zFziPK.j3bWx4SAL0'
$ perl -p -e 's/tomcat:[^:]+:/tomcat:$ENV{HASH}:/' shadow
tomcat:$1$Ic8qfl8h$cqja.zFziPK.j3bWx4SAL0:0:99999:7:::

or
$ perl -p -e 's/^(tomcat:)[^:]+:/$1$ENV{HASH}:/' shadow
tomcat:$1$Ic8qfl8h$cqja.zFziPK.j3bWx4SAL0:0:99999:7:::

- NO command line shell substitutions.
- Pick up the shell variable from the %ENV associative array.
- Make the replace target be: [^:]+
That is a sequence of 1 or more non-colon characters.
- you add the -i and real file.

Enjoy,
Hein.
Shabu Khan-2
Frequent Advisor

Re: perl pie question

Closing this out