painfullyxpretty
asked on
PERL execution inside shell script / replace in file
Okay, I have a function that was written in PERL, and I could try to convert it to shell but I think it would be easier just to use a PERL script and have it print the code into the shell, but I am having troubles getting it to work. Here is the PERL script:
encrypt.pl
-------------------------- ---------- ---------
#!/usr/bin/perl
@palace_key = ( 55,197,96,114,205,165,11,6 ,209,249,2 03,60,87,1 2,13,27,12 1,30,45,11 4,180,4,17 9,96,178,1 28,221,137 ,160,237,2 42,132,124 ,250,196,1 53,159,39, 237,222,10 8,222,30,1 84,72,52,1 00,41,118, 165,1,134, 33,11,191, 176,153,23 7,32,45,11 7,66,76,10 6,192,117, 126,137,51 ,0,101,130 ,3,4,147,6 1,87,74,17 0,47,165,2 13,116,189 ,215,49,21 8,131,150, 226,24,89, 238,32,83, 36,61,136, 171,119,74 ,153,185,4 ,225,211,1 57,208,137 ,191,137,6 8,50,100,1 99,46,147, 51,213,107 ,41,149,18 7,59,130,1 50,61,96,3 8,208,132, 91,69,90,1 39,76,90,6 0,91,138,8 8,148,184, 155,19,219 ,107,190,2 6,10,96,11 5,116,85,2 31,89,176, 186,68,118 ,172,226,1 33,22,196, 16,83,151, 56,192,238 ,168,6,66, 169,27,189 ,210,151,2 26,221,37, 218,165,68 ,199,32,81 ,58,251,14 6,148,23,1 13,47,2,85 ,96,150,18 1,38,76,87 ,247,139,7 0,120,25,2 26,182,96, 143,208,24 7,148,105, 4,45,197,6 5,210,64,1 31,137,103 ,199,238,2 45,93,167, 137,49,215 ,146,82,87 ,216,167,1 6,246,211, 20,138,18, 118,127,26 ,226,23,21 3,177,255, 126,130,21 6,50,179,1 37,29,178, 68,106,120 ,175,15,35 ,249,100,1 22,59,122, 19,33,80,1 34,128,7,7 1,152,188, 132,36,110 ,224,171,1 32,222,220 ,1,32,237, 3,2,153,16 3,47,158,1 37,103,3,1 05,197,165 ,46,18,177 ,84,95,211 ,54,249,12 8,220,55,9 5,35,105,1 78,203,57, 153,123,23 3,89,99,40 ,9,5,182,1 46,170,69, 121,84,212 ,227,45,10 7,108,90,1 67,41,231, 108,102,14 9,64,201,4 5,206,51,5 7,85,102,2 02,126,209 ,122,192,3 2,64,9,78, 41,179,222 ,9,205,153 ,174,251,2 4,173,115, 213,126,18 6,2,91,143 ,92,163,24 9,169,43,2 42,99,222, 152,47,38, 153,40,202 ,190,239,1 19,114,174 ,200,93,41 ,114,108,4 2,24,59,13 9,161,21,1 72,30,93,9 ,72,125,22 1,44,171,1 38,7,228,7 4,164,107, 150,17,136 ,205,156,4 8,21,207,8 5,18,241,1 30,76,4,36 ,236,168,1 74,210,28, 46,28,18,6 6,154,232, 32,100,92, 191,222,23 2,244,229, 76,119,228 ,81,176,38 ,218,167,1 34,243,16, 180,134,23 2,14,121,1 45,78,222, 117,123,21 3,209,215, 170,22,189 ,91,3,165, 49,56,147, 51,102,242 ,14,111,16 ,168,216,5 3,149,80,4 5,161,63,2 53,6,9,144 ,134,204 );
sub palace_encrypt
{
local $lastchar = 0;
local $rc = 0;
local @bs;
for($i=length($_[0])-1; $i>=0; $i--) {
local $b = ord(substr($_[0], $i, 1));
$bs[$i] = $b ^ $palace_key[$rc++] ^ $lastchar;
$lastchar = $bs[$i] ^ $palace_key[$rc++];
}
return join("", map { chr($_) } @bs);
}
sub encode_escaped
{
local $str = $_[0];
$str =~ s/\\/\\\\/g;
$str =~ s/([^A-Za-z0-9\.\\])/sprin tf("\\%2.2 X", ord($1))/ge;
return $str;
}
print &encode_escaped(&palace_en crypt($ARG V[0]));
-------------------------- ---------- ---------- ---------- ---------- ---------- --
I am trying to use the encrypted output inside a shell script that will replace
OWNERPASSWORD "X"
with
OWNERPASSWORD "encyptedstring"
where encryptedstring is the return of &encode_escaped(&palace_en crypt($ARG V[0]))
The problem is that I try to run perl -e encrypt.pl owner and it doesnt seem to correctly work. I realize it might just be easier to do the whole thing in PERL, which is acceptable. But everything I read on search/replace in a file for PERL is all one-liners for the linux command line and not really for PERL script.
encrypt.pl
--------------------------
#!/usr/bin/perl
@palace_key = ( 55,197,96,114,205,165,11,6
sub palace_encrypt
{
local $lastchar = 0;
local $rc = 0;
local @bs;
for($i=length($_[0])-1; $i>=0; $i--) {
local $b = ord(substr($_[0], $i, 1));
$bs[$i] = $b ^ $palace_key[$rc++] ^ $lastchar;
$lastchar = $bs[$i] ^ $palace_key[$rc++];
}
return join("", map { chr($_) } @bs);
}
sub encode_escaped
{
local $str = $_[0];
$str =~ s/\\/\\\\/g;
$str =~ s/([^A-Za-z0-9\.\\])/sprin
return $str;
}
print &encode_escaped(&palace_en
--------------------------
I am trying to use the encrypted output inside a shell script that will replace
OWNERPASSWORD "X"
with
OWNERPASSWORD "encyptedstring"
where encryptedstring is the return of &encode_escaped(&palace_en
The problem is that I try to run perl -e encrypt.pl owner and it doesnt seem to correctly work. I realize it might just be easier to do the whole thing in PERL, which is acceptable. But everything I read on search/replace in a file for PERL is all one-liners for the linux command line and not really for PERL script.
perl encrypt.pl owner
encyptedstring=`encrypt.pl $plaintextstring`
ASKER
I am not sure where you were getting at with your 2nd post, It gives me an error, ill paste you what I have so far...
#!/bin/sh
ENCRYPTEDPASSWORD=perl encrypt.pl owner
sed -i 's/OWNERPASSWORD "x"/OWNERPASSWORD "$ENCRYPTEDPASSWORD"/g' ./psdata/pserver
#!/bin/sh
ENCRYPTEDPASSWORD=perl encrypt.pl owner
sed -i 's/OWNERPASSWORD "x"/OWNERPASSWORD "$ENCRYPTEDPASSWORD"/g' ./psdata/pserver
ASKER
This does not correctly store the output of the perl script to ENCRYPTEDPASSWORD, it also does not correctly use the ENCRYPTEDPASSWORD in the sed command either.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Okay, but this just prints the word "$ESCAPEDPASSWORD" whereas it should be "5\8DT\85E" when using the word owner.
Sorry, I thought I had typed
sed 's/OWNERPASSWORD "X"/OWNERPASSWORD "'"$ESCAPEDPASSWORD"'"/g'
sed 's/OWNERPASSWORD "X"/OWNERPASSWORD "'"$ESCAPEDPASSWORD"'"/g'
ASKER
Thank youuuu. I know you answered the question in the first reply but you helped me along so A+++++