|
|||
|
Net::FTP=GLOB(0x837217c)<<< 226 Transfer complete
.. .. logs index.html wsb6121022001 energy green2.m4v Video 151.wmv false.wmv vids images zen rev1.html luther1.html lh_1.html lh_17.html lh_18.html lh_19.html ceiling_2.html ceiling_2_files {word}_2.html {la_veta}_2.html la_veta1.html la_veta2.html la_veta3.html la_veta4.html la_veta5.html la_veta6.html lh_abc_2.html $ echo 'piss on it, I'll do itagain' > > C^C $ perl ceiling4.pl Net::FTP>>> Net::FTP(2.77) [blobbity bla] (74,208,244,112,254,89). Net::FTP=GLOB(0x9dad8d4)>>> NLST Net::FTP=GLOB(0x9dad8d4)<<< 150 Opening BINARY mode data connection for file list Net::FTP=GLOB(0x9dad8d4)<<< 226 Transfer complete .. .. logs index.html wsb6121022001 energy green2.m4v Video 151.wmv false.wmv vids images zen rev1.html luther1.html lh_1.html lh_17.html lh_18.html lh_19.html ceiling_2.html ceiling_2_files {word}_2.html {la_veta}_2.html la_veta1.html la_veta2.html la_veta3.html la_veta4.html la_veta5.html la_veta6.html lh_abc_2.html lh_abc_3.html $ cat lh_abc_3.html <html> <head> <title>Lutherhaven Renovation</title> </head> <body bgcolor=white> <h1>Basement Ceiling Materials</h1> <img src="/images/image_62.jpg"/> <p>caption for image_62.jpg </p> <img src="/images/image_63.jpg"/> <p>caption for image_63.jpg </p> <img src="/images/image_64.jpg"/> <p>caption for image_64.jpg </p> <img src="/images/image_65.jpg"/> <p>caption for image_65.jpg </p> <img src="/images/image_66.jpg"/> <p>caption for image_66.jpg </p> $ cat ceiling4.pl $ cat ceiling4.pl $ echo "I murdered my source." I murdered my source. $ ls -l total 145524 -rw-r--r-- 1 dan dan 2359350 2011-11-06 16:59 1and1.bmp -rw-r--r-- 1 dan dan 66174 2012-01-10 20:03 402789_2985747999782_1144491549_33319470_913147571 _n.jpg -rw-r--r-- 1 dan dan 26886706 2011-10-14 22:47 abq1.wmv -rw-r--r-- 1 dan dan 688 2011-11-07 16:55 agd1.html -rw-r--r-- 1 dan dan 102369 2012-01-10 02:15 agd1.jpg -rw-r--r-- 1 dan dan 673 2011-11-12 17:21 agd2.html -rw-r--r-- 1 dan dan 92055 2012-01-10 02:15 agd.jpg -rw-rw-r-- 1 dan dan 143 2012-06-12 15:23 andrea1.pl -rw-rw-r-- 1 dan dan 103 2012-06-12 15:22 andrea1.pl~ -rw-r--r-- 1 dan dan 1748 2011-10-11 21:39 beck1.txt -rw-r--r-- 1 dan dan 896 2011-06-22 13:27 bill1.txt -rw-r--r-- 1 dan dan 2810 2011-07-06 18:32 BOOTEX.LOG -rw-rw-r-- 1 dan dan 2090 2012-07-04 01:09 ceiling1.pl -rw-rw-r-- 1 dan dan 2090 2012-06-25 14:42 ceiling1.pl~ -rw-rw-r-- 1 dan dan 457 2012-06-25 14:42 ceiling_2.html -rw-rw-r-- 1 dan dan 2107 2012-07-03 23:53 ceiling2.pl -rw-rw-r-- 1 dan dan 2106 2012-07-03 23:52 ceiling2.pl~ -rw-rw-r-- 1 dan dan 2109 2012-07-27 18:07 ceiling3.pl -rw-rw-r-- 1 dan dan 2107 2012-07-27 18:07 ceiling3.pl~ -rw-rw-r-- 1 dan dan 0 2012-07-28 04:08 ceiling4.pl -rw-rw-r-- 1 dan dan 2085 2012-07-27 21:26 ceiling4.pl~ [snip] $ cat ceiling4.pl~ #!/usr/bin/perl -w use strict; use 5.010; use Net::FTP; my $domain = 'www.merrillpjensen.com'; my $username = 'u61210220'; my $password = ''; my $word = "lh_abc"; my $ftp = Net::FTP->new( $domain, Debug => 1, Passive => 1 ) or die "Can't connect: $@\n"; $ftp->login( $username, $password ) or die "Couldn't login\n"; $ftp->binary(); # get files from remote root that end in html: my @remote_files = $ftp->ls(); # print "remote files are: @remote_files\n"; my @matching = map /${word}_(\d+)\.html/, @remote_files; print "matching is @matching\n"; push( @matching, 1 ); @matching = sort { $a <=> $b } @matching; my $winner = pop @matching; my $newnum1 = $winner + 1; my $html_file = "${word}_$newnum1.html"; print "html file is $html_file\n"; # create file for html stubouts open( my $fh, '>', $html_file ) or die("Can't open $html_file for writing: $!"); print $fh "<html>\n"; print $fh "<head>\n"; print $fh "<title>Lutherhaven Renovation</title>\n"; print $fh "</head>\n"; print $fh "<body bgcolor=white>\n"; print $fh "<h1>Basement Ceiling Materials</h1>\n"; # get files from Desktop/images/ my $path = '/home/dan/Desktop/upload/'; my @files = <$path*>; # get ls from remote image directory $ftp->cwd('/images/') or die "cwd failed $@\n"; my @list = $ftp->ls(); # main control for my $name (@files) { print "name is $name\n"; my ($ext) = $name =~ /([^.]*)$/; print "ext is $ext\n"; @matching = map /image_(\d+)\.$ext$/, @list; print "matching is @matching\n"; push( @matching, 1 ); @matching = sort { $a <=> $b } @matching; $winner = pop @matching; my $newnum = $winner + 1; my $new_file2 = "image_$newnum.$ext"; print "newfile is $new_file2\n"; $ftp->put( $name, $new_file2 ) or die "put failed $!\n"; push( @list, $new_file2 ); # unlink($name); print $fh "<img src=\"/images/$new_file2\"/>\n\n"; print $fh "<p>caption for $new_file2 <\/p>\n"; } close $fh; $ftp->cdup() or die "cdup failed $@\n"; $ftp->put($html_file) or die "put failed $@\n"; my @r = $ftp->ls(); print "@r\n"; $ What I tried to do was to write a source file to output. Does argv contain the prog's name? How would I switch _abc_ for a random composition of friendly letters in a character class? I can take the easier route and look at script n-1, too. Peace. -- Cal |
|
|
||||
|
||||
|
|
|
|||
|
Quoth Cal Dershowitz <cal@example.invalid>: <snip> I don't understand what any of that's supposed to be doing, or what it's doing wrong... > What I tried to do was to write a source file to output. Does argv > contain the prog's name? No. The program's name is in $0, and it may or may not be an absolute path depending on how it was invoked. > How would I switch _abc_ for a random composition of friendly letters in > a character class? What do you mean? Ben |
|
|||
|
On 28/07/12 11:51, Cal Dershowitz wrote:
snipped tedious console listing. A couple of lines of explanation would have been better. > $ cat ceiling4.pl > $ cat ceiling4.pl > $ echo "I murdered my source." > I murdered my source. snipped unnecessary directory listing. > $ cat ceiling4.pl~ Snipped Perl listing which looked OK at a glance. > What I tried to do was to write a source file to output. Does argv > contain the prog's name? > > How would I switch _abc_ for a random composition of friendly letters in > a character class? > > I can take the easier route and look at script n-1, too. Cal, this isn't a well-posed question. In fact I have no idea what you're trying to do and what you ended up doing instead. I've looked at your program (it's not runnable and I'm not going to invest the effort required to make it so) and I can't immediately see any reason why it should over-write its own source. What's "argv" (actually ARGV in Perl) got to do with it? A "source file"? Source for what? Do you mean "an HTML file"? And I have no clue what you want to do when you talk about random compositions of friendly letters in a character class. What's "friendly" in this context? What character class? If your reply looks interesting and makes clearer what you're trying to do I'll have a go at it. Unless you're a bot, that is ... -- Henry Law Manchester, England |
|
|||
|
On 07/28/2012 03:29 PM, Henry Law wrote:
> Cal, this isn't a well-posed question. In fact I have no idea what > you're trying to do and what you ended up doing instead. I was trying verify if a person could do what I've now put in the subject, but my first problem was that I killed my source. I had the the arrow going this way > instead of this way < in the open statement: bye bye source. > > I've looked at your program (it's not runnable and I'm not going to > invest the effort required to make it so) and I can't immediately see > any reason why it should over-write its own source. > > What's "argv" (actually ARGV in Perl) got to do with it? > > A "source file"? Source for what? Do you mean "an HTML file"? > > And I have no clue what you want to do when you talk about random > compositions of friendly letters in a character class. What's "friendly" > in this context? What character class? > > If your reply looks interesting and makes clearer what you're trying to > do I'll have a go at it. Unless you're a bot, that is ... > Not a bot, henry, JAPH. $ cat lp3.pl #!/usr/bin/perl -w use strict; use 5.010; use Net::FTP; my $domain = 'www.merrillpjensen.com'; my $username = ''; my $password = ''; my $sourcefile = $0; # get some ramdom letters my $word_length = 5; my $word = &generate_random_string($word_length); print "Random string: " . $word . "\n"; print "Length: " . length($word) . "\n"; my $ftp = Net::FTP->new( $domain, Debug => 1, Passive => 1 ) or die "Can't connect: $@\n"; $ftp->login( $username, $password ) or die "Couldn't login\n"; $ftp->binary(); # get files from remote root that end in html: my @remote_files = $ftp->ls(); # print "remote files are: @remote_files\n"; my @matching = map /${word}_(\d+)\.html/, @remote_files; print "matching is @matching\n"; push( @matching, 1 ); @matching = sort { $a <=> $b } @matching; my $winner = pop @matching; my $newnum1 = $winner + 1; my $html_file = "${word}_$newnum1.html"; print "html file is $html_file\n"; # create file for html stubouts open( my $fh, '>', $html_file ) or die("Can't open $html_file for writing: $!"); print $fh "<Content-Type: text/html>\n"; print $fh "<html>\n"; print $fh "<head>\n"; print $fh "<title>" . $sourcefile . "</title>\n"; print $fh "</head>\n"; print $fh "<body bgcolor=white>\n"; print $fh "<h1>Source Listing</h1>\n"; # get files from Desktop/images/ my $path = '/home/dan/Desktop/upload/'; my @files = <$path*>; # get ls from remote image directory $ftp->cwd('/images/') or die "cwd failed $@\n"; my @list = $ftp->ls(); # main control for my $name (@files) { print "name is $name\n"; my ($ext) = $name =~ /([^.]*)$/; print "ext is $ext\n"; @matching = map /image_(\d+)\.$ext$/, @list; # print "matching is @matching\n"; push( @matching, 1 ); @matching = sort { $a <=> $b } @matching; $winner = pop @matching; my $newnum = $winner + 1; my $new_file2 = "image_$newnum.$ext"; print "newfile is $new_file2\n"; $ftp->put( $name, $new_file2 ) or die "put failed $!\n"; push( @list, $new_file2 ); # unlink($name); print $fh "<img src=\"/images/$new_file2\"/>\n\n"; print $fh "<p>caption for $new_file2 <\/p>\n"; } open( my $gh, '<', $sourcefile ) or die("Can't open $sourcefile for writing: $!"); while (<$gh>) { chomp; # replace things that look like diamonds with html literals s/</' < '/; s/>/' > '/; # this may look obvious, but it's not s/my \$password = '.*';/my \$password = 'redacted';/; s/my \$username = '.*';/my \$username = 'redacted';/; print $fh '<p>' . $_ . '</p>' . "\n"; } close $gh; close $fh; $ftp->cdup() or die "cdup failed $@\n"; $ftp->put($html_file) or die "put failed $@\n"; my @r = $ftp->ls(); # print "@r\n"; ################################################## ######### # Written by Guy Malachi http://guymal.com # 18 August, 2002 ################################################## ######### # This function generates random strings of a given length sub generate_random_string { my $length_of_randomstring = shift; # the length of # the random string to generate my @chars = ( 'a' .. 'z' ); my $random_string; foreach ( 1 .. $length_of_randomstring ) { # rand @chars will generate a random # number between 0 and scalar @chars $random_string .= $chars[ rand @chars ]; } return $random_string; } $ So, this almost works, and I think my trouble is with the regex for substituting out the password and user name. Sometimes it works; sometimes it doesn't: http://www.merrillpjensen.com/ifeod_2.html It seems like the wheels fall off the RE engine when I add the subroutine. But I'm really proud of this so far. I had all the time I wanted at night to read for the last few weeks and had only _Learning Perl_ to read. No tv, 'puter, cell phone. A very well-crafted book. -- Cal |
|
|||
|
On 07/29/2012 07:10 AM, Ben Morrow wrote:
> > Quoth Cal Dershowitz<cal@example.invalid>: >> On 07/28/2012 05:28 AM, Ben Morrow wrote: [snipped, re-ordered, title changed] >> I still don't get how to call a subroutine >> properly, unless it's with&. > > sub foo { ... } > > foo(...); > > The brackets are required unless you declare the sub before using it. If > you want to be able to leave them out you can use one of > > sub foo; > use subs qw/foo/; > > to predeclare subs at the top of the file. (This is not entirely unlike > C prototypes, except for the 'prototype' part.) Ok, I think programmers of a certain age are simply averse to pushing the limits. Perl is already so flexible compared to what we had 20 years ago. > I'll try once more. Please explain, *IN ENGLISH*, what you are trying to > do and what is not working. Dumping a whole lot of logs and code is not > an alternative to an explanation. $ cat quine2.pl #!/usr/bin/perl -w use strict; use 5.010; my $html_file = "/home/dan/Desktop/previousdec2011/iypjx_2.html"; # print "html file is $html_file\n"; open( my $gh, '+<', $html_file ) or die("Can't open $html_file for writing: $!"); while (<$gh>) { chomp; # this is not working # this may look obvious, but it's not # s/my \$password = '.*';/my \$password = 'redacted';/; # s/my \$username = '.*';/my \$username = 'redacted';/; if (/\$password/) { print "matched\n"; $_ =~ s/'.*'/'redacted'/; } } close $gh; $ When I run this on these ftp upload files that have my password in it, I get "matched" 3 times like I would suspect, but I get no effective substitution. Tried several things now. As you see it, there's nothing that writes the line to the file. When I add my best guess, I get very exotic output, which I would show but for the insistence that I not do so. Hasn't anyone else used a a script to erase his passwords so that he can share his source without torpedoing his security? -- Cal -- Cal |
|
|||
|
Am 30.07.2012 23:26, schrieb Cal Dershowitz:
> On 07/29/2012 07:10 AM, Ben Morrow wrote: >> The brackets are required unless you declare the sub before using it. If >> you want to be able to leave them out you can use one of >> >> sub foo; >> use subs qw/foo/; >> >> to predeclare subs at the top of the file. (This is not entirely unlike >> C prototypes, except for the 'prototype' part.) > > Ok, I think programmers of a certain age are simply averse to pushing > the limits. Perl is already so flexible compared to what we had 20 > years ago. Sorry, I don't understand - what does it have to do with pushing something to the limits? Usually you do NOT need to predeclare a sub. perl -wE'foo(); sub foo{ say q(I am foo) }' > $ cat quine2.pl > #!/usr/bin/perl -w > use strict; > use 5.010; > > my $html_file = "/home/dan/Desktop/previousdec2011/iypjx_2.html"; > > # print "html file is $html_file\n"; > > open( my $gh, '+<', $html_file ) > or die("Can't open $html_file for writing: $!"); ah, you open the file in rw mode. > while (<$gh>) { This is while (defined($_ = <$gh>)), i.e. you now have a new value in $_ corresponding to a line. This is a copy! So modifying $_ doesn't do anything to the file! > chomp; > > # this is not working > # this may look obvious, but it's not > # s/my \$password = '.*';/my \$password = 'redacted';/; > # s/my \$username = '.*';/my \$username = 'redacted';/; > > if (/\$password/) { > print "matched\n"; > $_ =~ s/'.*'/'redacted'/; Are you sure about this replacement? Replacing anyhing between the first ' of the line and the last ' of the line? > } > > } > close $gh; > $ > > When I run this on these ftp upload files that have my password in it, I > get "matched" 3 times like I would suspect, but I get no effective > substitution. Tried several things now. As you see it, there's > nothing that writes the line to the file. When I add my best guess, I > get very exotic output, which I would show but for the insistence that I > not do so. You are not writing to the file. So the replacement doesn't have any effect. > Hasn't anyone else used a a script to erase his passwords so that he can > share his source without torpedoing his security? What about perl's in-place edit? See perldoc perlrun! Just run perl -pi -e 's/password/redacted/g' /path/to/iypjx_2.html or maybe perl -pi -e "s/'password'/'redacted'/g" /path/to/iypjx_2.html if you only want to replace the password if it is between single quotes. (you can also run perl -pi.bak -e ... to get a backup file with .bak extension) - Wolf |
|
|||
|
Quoth Cal Dershowitz <cal@example.invalid>: > On 07/29/2012 07:10 AM, Ben Morrow wrote: > > > I'll try once more. Please explain, *IN ENGLISH*, what you are trying to > > do and what is not working. Dumping a whole lot of logs and code is not > > an alternative to an explanation. > > $ cat quine2.pl You have omitted the part where you explain in English what you are trying to do. It may be possible to divine from your code, but that is not the same as an explanation. An explanation looks something like I am trying to open a Perl source file and copy it to another file with all the passwords removed. The code you posted before was doing a great number of things, and it wasn't at all clear which of those things wasn't working. > #!/usr/bin/perl -w > use strict; > use 5.010; > > my $html_file = "/home/dan/Desktop/previousdec2011/iypjx_2.html"; > > # print "html file is $html_file\n"; > > open( my $gh, '+<', $html_file ) > or die("Can't open $html_file for writing: $!"); > while (<$gh>) { > chomp; > > # this is not working > # this may look obvious, but it's not > # s/my \$password = '.*';/my \$password = 'redacted';/; > # s/my \$username = '.*';/my \$username = 'redacted';/; > > if (/\$password/) { > print "matched\n"; > $_ =~ s/'.*'/'redacted'/; > } > > } > close $gh; > $ > > When I run this on these ftp upload files that have my password in it, I > get "matched" 3 times like I would suspect, but I get no effective > substitution. Tried several things now. As you see it, there's > nothing that writes the line to the file. When I add my best guess, I > get very exotic output, which I would show but for the insistence that I > not do so. You could at least show us what your best guess was, since I believe the substitution is working perfectly. You are, I hope, aware that attempting to overwrite a file while you are reading it is likely to produce confusing results? The normal procedure is to write a new file (you can create a secure temporary file with File::Temp) and rename it over the top. This #!/usr/bin/perl use warnings; use strict; while (<DATA>) { if (/\$password/) { s/'.*'/'redacted'/; } print; } __DATA__ use warnings; use strict; use Net::FTP; my $password = 'foo'; my $F = Net::FTP->new(...); works for me: that is, it prints the contents of the DATA section with the password assignment replaced. > Hasn't anyone else used a a script to erase his passwords so that he can > share his source without torpedoing his security? It's not a common thing to want to do, because it's generally a bad idea to put passwords directly in the script file in the first place. The best thing to do, if you need a password, is to prompt for it (you can use Term::ReadKey to prompt without echoing); if you must keep it in a file, make it a separate config file so the script can be shared with others/checked into a VCS/whatever without revealing your passwords. Ben |
|
|||
|
On 07/30/2012 03:46 PM, Wolf Behrenhoff wrote:
> Am 30.07.2012 23:26, schrieb Cal Dershowitz: > ah, you open the file in rw mode. Thx for your comment, Wolf, I didn't have any luck with it at all. For the small thing I'm doing, I just to decided to read the file into an array, manipulate the array, and then write it to a file. > >> while (<$gh>) { > > This is while (defined($_ =<$gh>)), i.e. you now have a new value in $_ > corresponding to a line. This is a copy! So modifying $_ doesn't do > anything to the file! I think I solve that here: #!/usr/bin/perl -w use strict; use 5.010; my $source_file = "/home/dan/Desktop/source/marni1.pl"; my (@lines); open( my $gh, '<', $source_file ) or die("Can't open $source_file for writing: $!"); chomp( @lines = <$gh> ); close $gh; my $name = 'post1.pl'; open( my $fh, '>', $name ) or die("Can't open $name for writing: $!"); foreach (@lines) { if (/\$password|\$username/) { print "matched\n"; s/'.*'/'redacted'/; } $_ .= "\n"; print $fh $_; } > >> chomp; >> >> # this is not working >> # this may look obvious, but it's not >> # s/my \$password = '.*';/my \$password = 'redacted';/; >> # s/my \$username = '.*';/my \$username = 'redacted';/; >> >> if (/\$password/) { >> print "matched\n"; >> $_ =~ s/'.*'/'redacted'/; > > Are you sure about this replacement? Replacing anyhing between the first > ' of the line and the last ' of the line? It works in the context I'm using i, but I'd feel better if the regex checked for and equals sign being between them. (a little ambitious for me) > What about perl's in-place edit? See perldoc perlrun! > > Just run > perl -pi -e 's/password/redacted/g' /path/to/iypjx_2.html > > or maybe > perl -pi -e "s/'password'/'redacted'/g" /path/to/iypjx_2.html > if you only want to replace the password if it is between single quotes. > > (you can also run perl -pi.bak -e ... to get a backup file with .bak > extension) > > - Wolf > Those again, are clever and a little bit beyond my game, but I wouldn't see how they wouldn't do bad things to this line, which is very typical in an ftp program: $ftp->login($username, $password) or die "Couldn't login\n"; |
|
|||
|
>>>>> "WB" == Wolf Behrenhoff <NoSpamPleaseButThisIsValid3@gmx.net> writes:
>> while (<$gh>) { WB> This is while (defined($_ = <$gh>)), i.e. you now have a new value in $_ WB> corresponding to a line. This is a copy! So modifying $_ doesn't do WB> anything to the file! wow. the OP had a massively odd view of rw files. WB> What about perl's in-place edit? See perldoc perlrun! WB> Just run WB> perl -pi -e 's/password/redacted/g' /path/to/iypjx_2.html WB> or maybe WB> perl -pi -e "s/'password'/'redacted'/g" /path/to/iypjx_2.html WB> if you only want to replace the password if it is between single quotes. since he is doing this in a script and not the command line, edit_file or edit_file_lines from File::Slurp with do that easily use File::Slurp qw( edit_file ) ; edit_file { s/'password'/'redacted'/g } '/path/to/iypjx_2.html' ; done. to the OP: next time stop with the nonsense, the quine, the logs, and just say you want to edit a file in place. pretty simple concept but you said everything BUT that. uri |
|
|||
|
>>>>> "CD" == Cal Dershowitz <cal@example.invalid> writes:
CD> my $source_file = "/home/dan/Desktop/source/marni1.pl"; CD> my (@lines); CD> open( my $gh, '<', $source_file ) CD> or die("Can't open $source_file for writing: $!"); CD> chomp( @lines = <$gh> ); why chomp all the lines just to add a newline back later??? CD> close $gh; CD> my $name = 'post1.pl'; CD> open( my $fh, '>', $name ) CD> or die("Can't open $name for writing: $!"); CD> foreach (@lines) { CD> if (/\$password|\$username/) { CD> print "matched\n"; CD> s/'.*'/'redacted'/; CD> } CD> $_ .= "\n"; CD> print $fh $_; CD> } see my other post for clean one line solution. uri |
|
|||
|
On 07/30/2012 05:00 PM, Ben Morrow wrote:
> > Quoth Cal Dershowitz<cal@example.invalid>: >> On 07/29/2012 07:10 AM, Ben Morrow wrote: >> >>> I'll try once more. Please explain, *IN ENGLISH*, what you are trying to >>> do and what is not working. Dumping a whole lot of logs and code is not >>> an alternative to an explanation. >> >> $ cat quine2.pl > > You have omitted the part where you explain in English what you are > trying to do. It may be possible to divine from your code, but that is > not the same as an explanation. An explanation looks something like > > I am trying to open a Perl source file and copy it to another file > with all the passwords removed. That's one little tool in a chain I'm trying build. To hear you say it helped me figure out what I had to do. I'm not trying to be a jerk about not describing the subject as well as I can, but if I knew what I was talking about, then I wouldn't have to ask questions. >> When I run this on these ftp upload files that have my password in it, I >> get "matched" 3 times like I would suspect, but I get no effective >> substitution. Tried several things now. As you see it, there's >> nothing that writes the line to the file. When I add my best guess, I >> get very exotic output, which I would show but for the insistence that I >> not do so. > > You could at least show us what your best guess was, since I believe the > substitution is working perfectly. You are, I hope, aware that > attempting to overwrite a file while you are reading it is likely to > produce confusing results? The normal procedure is to write a new file > (you can create a secure temporary file with File::Temp) and rename it > over the top. > > This > > #!/usr/bin/perl > > use warnings; > use strict; > > while (<DATA>) { > if (/\$password/) { > s/'.*'/'redacted'/; > } > print; > } > > __DATA__ > use warnings; > use strict; > use Net::FTP; > > my $password = 'foo'; > > my $F = Net::FTP->new(...); > > works for me: that is, it prints the contents of the DATA section with > the password assignment replaced. I certainly appreciate that you take the time to entertain yourself with my computer problems, ben. That part was solid, but somehow I seemed to get tripped up in the same material tonight. (see below) > >> Hasn't anyone else used a a script to erase his passwords so that he can >> share his source without torpedoing his security? > > It's not a common thing to want to do, because it's generally a bad idea > to put passwords directly in the script file in the first place. The > best thing to do, if you need a password, is to prompt for it (you can > use Term::ReadKey to prompt without echoing); if you must keep it in a > file, make it a separate config file so the script can be shared with > others/checked into a VCS/whatever without revealing your passwords. > > Ben > $ perl safe_post4.pl String found where operator expected at safe_post4.pl line 17, near "$word =~ m/'(\w+)'" (Missing operator before '(\w+)'?) syntax error at safe_post4.pl line 17, near "$word =~ m/'(\w+)'" Unmatched right curly bracket at safe_post4.pl line 21, at end of line syntax error at safe_post4.pl line 21, near "}" Execution of safe_post4.pl aborted due to compilation errors. $ cat safe_post4.pl #!/usr/bin/perl -w use strict; use 5.010; use Net::FTP; my ($domain, $username, $password); my $identity_file = 'ftp_passwords_1.txt'; my (@lines, $word); open( my $gh, '<', $identity_file ) or die("Can't open $identity_file for writing: $!"); chomp(@lines = <$gh>); close $gh; print "@lines\n"; foreach (@lines) { if (/\$domain\) { print "matched\n"; $word =~ m/'(\w+)'/; $domain = $word; } print "domain is $domain\n"; } $ cat ftp_passwords_1.txt my $domain = 'www.bedrock.com'; my $username = 'barney'; my $password = 'rocks'; $ This looks like perl to me. Perl.exe disagrees. What gives? -- Cal |
|
|||
|
Am 01.08.2012 08:00, schrieb Cal Dershowitz:
> if (/\$domain\) { > print "matched\n"; > $word =~ m/ This is the whole match! From the first / right after the if to the second one which only appears 2 lines later. You simply forgot to end the match with / and used the \ instead. |
|
|||
|
On 08/01/2012 04:11 AM, Wolf Behrenhoff wrote:
> Am 01.08.2012 08:00, schrieb Cal Dershowitz: >> if (/\$domain\) { >> print "matched\n"; >> $word =~ m/ > > This is the whole match! From the first / right after the if to the > second one which only appears 2 lines later. > > You simply forgot to end the match with / and used the \ instead. > > Thanks, wolf. I get times where I can't see the difference between the forward and backslash. I call it "Dual-Boot Fatigue Syndrome." $ perl safe_post4.pl matched my $domain = 'www.bedrock.com'; word is www.bedrock.com matched my $username = 'barney'; word is barney matched my $password = 'rocks'; word is rocks $ cat safe_post4.pl #!/usr/bin/perl -w use strict; use 5.010; use Net::FTP; my ( $domain, $username, $password ); my $identity_file = 'ftp_passwords_1.txt'; my ( @lines, $word ); open( my $gh, '<', $identity_file ) or die("Can't open $identity_file for writing: $!"); chomp( @lines = <$gh> ); close $gh; foreach (@lines) { if (/\$domain/) { print "matched\n"; print $_ . "\n"; m/'(.*)'/; $domain = $1; print "word is $domain\n"; } } foreach (@lines) { if (/\$username/) { print "matched\n"; print $_ . "\n"; m/'(.*)'/; $username = $1; print "word is $username\n"; } } foreach (@lines) { if (/\$password/) { print "matched\n"; print $_ . "\n"; m/'(.*)'/; $password = $1; print "word is $password\n"; } } $ This works, but I wouldn't be too wild about seeing it included in all my ftp scripts. Q1) How could one read the word $domain or anything with a dollar sign on it in the help file and then declare it at top scope in the script? |
|
|||
|
On 07/31/2012 10:29 PM, Uri Guttman wrote:
>>>>>> "WB" == Wolf Behrenhoff<NoSpamPleaseButThisIsValid3@gmx.net> writes: > > >> while (<$gh>) { > > WB> This is while (defined($_ =<$gh>)), i.e. you now have a new value in $_ > WB> corresponding to a line. This is a copy! So modifying $_ doesn't do > WB> anything to the file! > > wow. the OP had a massively odd view of rw files. I don't think I have enough of an idea about them for it to be odd. Of course, when I saw the output, I knew I'd just put my data through a chipper, where the resulting chips may have been the sum total of input and output, but like I say, it was a chipper. > > > WB> What about perl's in-place edit? See perldoc perlrun! > > WB> Just run > WB> perl -pi -e 's/password/redacted/g' /path/to/iypjx_2.html > > WB> or maybe > WB> perl -pi -e "s/'password'/'redacted'/g" /path/to/iypjx_2.html > WB> if you only want to replace the password if it is between single quotes. > > since he is doing this in a script and not the command line, edit_file > or edit_file_lines from File::Slurp with do that easily > > use File::Slurp qw( edit_file ) ; > > edit_file { s/'password'/'redacted'/g } '/path/to/iypjx_2.html' ; > > done. ....in a way that unnecessarily ruins this line: $ftp->login($username, $password) or die "Couldn't login\n"; > > to the OP: next time stop with the nonsense, the quine, the logs, and > just say you want to edit a file in place. pretty simple concept but you > said everything BUT that. > > uri Well, uri, I just came down from the mountains, and I've decided to turn over a few new leafs. Maybe one of them could be with you. Would you like me to try File::Slurp and see if it can work into my own meaningful projects? If I did without your permission, you could say, "I never asked you to touch this software." I hope not to sound ironic, combative, all that. Cheers, -- Cal |
|
|||
|
Cal Dershowitz <cal@example.invalid> wrote:
>Q1) How could one read the word $domain or anything with a dollar sign >on it in the help file and then declare it at top scope in the script? Are you looking for a template system? jue |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|