ssh key via stdin: perl vs bash
Hi, I was trying to embed a ssh key in a script and pass it via stdin (unfortunately not directly supported by ssh). Investigating ways how this can be done, I ran into a curiosity. With bash, the following does about what I need: #!/bin/bash ssh -i /dev/stdin luser@localhost ls EOF -BEGIN RSA PRIVATE KEY- Trying to do the same thing with perl: #!/usr/bin/perl open STDIN, DATA; exec ssh -i /dev/stdin luser\@localhost ls; __END__ -BEGIN RSA PRIVATE KEY- does _not_ work - ssh will complain Permissions 0755 for '/dev/stdin' are too open (which is not true anyway) and refuse to use the key. Does somebody have a good explanation for this different behavior? I thought, from the perspective of ssh both variants should be equivalent ... Regards, Peter -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
ssh key via stdin: perl vs bash
Hi, I was trying to embed a ssh key in a script and pass it via stdin (unfortunately not directly supported by ssh). Investigating ways how this can be done, I ran into a curiosity. With bash, the following does about what I need: #!/bin/bash ssh -i /dev/stdin luser@localhost ls EOF -BEGIN RSA PRIVATE KEY- Trying to do the same thing with perl: #!/usr/bin/perl open STDIN, DATA; exec ssh -i /dev/stdin luser\@localhost ls; __END__ -BEGIN RSA PRIVATE KEY- does _not_ work - ssh will complain Permissions 0755 for '/dev/stdin' are too open (which is not true anyway) and refuse to use the key. Does somebody have a good explanation for this different behavior? I thought, from the perspective of ssh both variants should be equivalent ... Regards, Peter -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
ssh key via stdin: perl vs bash
Hi, I was trying to embed a ssh key in a script and pass it via stdin (unfortunately not directly supported by ssh). Investigating ways how this can be done, I ran into a curiosity. With bash, the following does about what I need: #!/bin/bash ssh -i /dev/stdin luser@localhost ls EOF -BEGIN RSA PRIVATE KEY- Trying to do the same thing with perl: #!/usr/bin/perl open STDIN, DATA; exec ssh -i /dev/stdin luser\@localhost ls; __END__ -BEGIN RSA PRIVATE KEY- does _not_ work - ssh will complain Permissions 0755 for '/dev/stdin' are too open (which is not true anyway) and refuse to use the key. Does somebody have a good explanation for this different behavior? I thought, from the perspective of ssh both variants should be equivalent ... Regards, Peter -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Benchmark.pm and threads ...
Hi, I use a simple little CPU-Benchmark based on the perl Benchmark module for rough performance comparison. Now I wanted to extend it to run the same test in parallel with a given number of threads. After I got totally unusable results, I stripped down the code for investigation: use Time::HiRes qw( gettimeofday tv_interval ); use Benchmark qw(:all :hireswallclock); use threads; my $num_threads=8; my $secs=10; my (@bench, @thr, $total); my $testcode=sub { my $sum; for (my $i=0; $i 1000; $i++) { $sum+=$i } }; my $t0 = [gettimeofday]; for (my $i=1; $i = $num_threads; $i++) { $thr[$i]= threads-create(sub { countit($secs, $testcode) }); } for (my $i=1; $i = $num_threads; $i++) { $bench[$i]=$thr[$i]-join(); printf %02d: iter: %d\n, $i, $bench[$i]-iters; $total += $bench[$i]-iters; } printf %04.1f secs iter: %d\n, tv_interval($t0,[gettimeofday]), $total; For $num_threads=1 it works as expected; the higher the number of threads, the more unpredictable it behaves. Instead of running the code for 10 secs, there is a tendency that each thread runs ~($secs/$num_threads) seconds (+ occasional runaways that terminate after a totally unpredictable time). At 1st I thought there might be some general problem between timers and threads. With the following instead of Benchmark::countit, I get the expected results: sub countit { my ($secs,$code)=@_; my $count=0; my $start=time(); while ((time()-$start) $secs ) { $count++; $code-() } return $count; }; Does anybody happen to have any experience with Benchmark.pm. Is it generally not usable with multiple threads or am I just doing something wrong? (Or is it maybe system dependent - I tried it on several machines running Debian on Linux 2.6.26 - 3.2.0 amd64) Regards, Peter -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
substitution: interpolate capture buffer into variable?
Hi, I would like to store regular expressions and substitution strings in a hash variable. If a given string matches any of the stored patterns, the corresponding substitution should be applied. What originally looked trivial turned out to be quite a challenge, particularly if the substitution string contains references to capture buffers. Here's a minimal example: my $rx=qr/^_(.*)/; my $r='a_$1'; my $s=_bla_fasel_blub; if ($s=~ /$rx/) { # pattern matches, apply substitution if you can # hardcoded, it's trivial: # $s =~ s/$rx/a_$1/; # but how to interpolate the capture buffer? Not like this: # eval '$s=$r'; # eval { $s=$r }; # $s =~ s/$rx/$r/e; } Can anybody think of a straightforward way to do this? Regards, Peter -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: substitution: interpolate capture buffer into variable?
On 2012-12-26 19:29, Paul Johnson wrote: This is a situation where string eval is warranted: eval \$s =~ s/\$rx/$r/; ... thanks a lot! Now that I now how it works, I can't believe I couldn't find the problem! I had tried string eval too; the real trick that I didn't get right is that the string to be eval'ed needs to be in double quotes, because $r (in my example) has to be interpolated before the eval. I think, I'll go for: eval qq(\$s=$r); Regards, Peter -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Reliably restarting sleep
Hi, the subject is admittedly not very enlighening ... ;-; what I am trying to do is: - run a little program, that just sleeps for a given time - when it receives a signal, restarts sleeping again for the full time period until it receives another signal or the timer elapses. In the latter case it should just exit. Something like: sub sleeper { warn strftime(%H:%M:%S sleep $sleeptime\n, localtime); $SIG{USR1}=\sleeper; sleep $sleeptime; }; warn $$; sleeper; It sounds very simple, but I can't get it to work as intended. I tryied it in numberless variations using sleep, Time::HiRes, alaram/pause ... the actual result is always pretty much the same: - When the program receives the 1st USR1 signal, it is interrupted immediately and sleeper is called. The pending alarm is obviously canceled and restarted as intended - Any subsequent USR1 signal is only reacted to when the full $sleeptime starting from the previous signal is elapsed. If more than 1 signal is received in the meantime, additional signals are lost. Does anybody have an idea why this doesn't work and how to get the intendeded result? Any suggestions are appreciated ... (This is taking place on a Linux 2.6.32 kernel using perl 5.10.0 ...) Regards, Peter -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Reliably restarting sleep
Hi, On 2011-06-14 09:23, gator...@yahoo.de wrote: what I am trying to do is: - run a little program, that just sleeps for a given time - when it receives a signal, restarts sleeping again for the full time period until it receives another signal or the timer elapses. In the latter case it should just exit. Something like: sub sleeper { warn strftime(%H:%M:%S sleep $sleeptime\n, localtime); $SIG{USR1}=\sleeper; sleep $sleeptime; }; warn $$; sleeper; It sounds very simple, but I can't get it to work as intended. ... meanwhile a found a solution; in case somebody with the same problem stumbles upon this, here's what I came up with: my $caught_signal=0; sub expired { warn strftime(%H:%M:%S expired\n, localtime); exit 0; } sub sleeper { warn strftime(%H:%M:%S sleep $sleeptime\n, localtime); alarm $sleeptime; pause; }; sub usr1 { alarm 0; $caught_signal=1; } $SIG{USR1}=\usr1; $SIG{ALRM}=\expired; while(1) { if($caught_signal) { $caught_signal=0; } else { sleeper(); } } The problem obviously was, that I called sleep from within the USR1 signal handler and (generally not a bad idea ;) this signal had been blocked there. If somebody knows a more elegant solution, let me know ... Regards, Peter -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/