- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- perl pie question
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2008 11:43 AM
06-09-2008 11:43 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2008 12:34 PM
06-09-2008 12:34 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2008 12:38 PM
06-09-2008 12:38 PM
SolutionThis 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2008 08:04 PM
06-09-2008 08:04 PM
Re: perl pie question
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2008 10:14 AM
06-10-2008 10:14 AM