こせきの技術日記

2006-12-04

[][] 実行中のスクリプトが置かれたディレクトリを取得する

PerlはFindBinモジュールを使う。Rubyは__FILE__を使って取得する。

# perl
use FindBin qw($Bin);

# ruby
dir = File.dirname(__FILE__)
dir = File.expand_path(__FILE__)

2006-09-01

[] SMTPメール送信

メールサーバ実験用。

#! /usr/bin/perl

use Socket;

unless (-f $ARGV[3]) {
   print "Usage: sendmail.pl [SMTP server] [from] [to] [mail file]\n";
   exit;
}

my $server = $ARGV[0];
my $from = $ARGV[1];
my $to = $ARGV[2];
my $mail = &load_contents($ARGV[3]);

sub load_contents {
    my $file = shift;
    my $bak = $/;
    undef $/;

    open IN, "< $file";
    my $result = <IN>;
    close IN;

    $/ = $bak;
    return $result;
}

sub connect {
    my ($server) = @_;
    my $port = getservbyname("smtp","tcp");
    my $struct = sockaddr_in($port,inet_aton($server));
    socket(SH, PF_INET, SOCK_STREAM, 0) or die("Failed to create socket: $!");
    connect(SH, $struct ) or die("Failed to connect: $!");

    select(SH); 
    $| = 1;
    select(STDOUT);
}
    
sub send {
    my ($command) = @_;
    print $command, "\n" ;
    print SH $command, "\r\n" ;
}

sub recv {
    my ($rex, $failedcmd) = shift;
    print $res = <SH> ;
    if ($res !~ /$rex/) {
        if (defined $failedcmd) {
            print SH "$failedcmd\r\n";
	}
        close(SH);
	die("Unexpected response: $!");
    }
}

print "----------------------------------------------------------------\n";

&connect($server);

&recv("^220");

&send("HELO localhost");
&recv("^250");

&send("MAIL FROM: <$from>");
&recv("^250", "RSET");

&send("RCPT TO: <$to>");
&recv("^25(?:0|1)", "RSET");

&send("DATA");
&recv("^354", "RSET");

&send("$mail\r\n.");
&recv("^250", "RSET");

&send("QUIT");

close(SH);
print "----------------------------------------------------------------\n";

2006-08-23

[][] ファイルのn番目のカラムの数値を合計する

# 5番目のカラムを合計
cat tmp.txt | perl -ne 'split; print $i+=$_[4], "\n"'
# 結果だけ出力
cat tmp.txt | perl -ne 'split; print $i+=$_[4], "\n"' | tail -n1

2006-04-07

[] LWPでクッキーと一緒にPOST

#! /usr/bin/perl

use LWP::UserAgent;
use HTTP::Cookies;
use HTTP::Request;
use HTTP::Response;

my $host = "127.0.0.1";
my $port = "80";
my $jsessionid = "xxxxxxxxx";

my $ua = new LWP::UserAgent;
my $req = new HTTP::Request POST => "http://$host:$port/path";
$req->content_type('application/x-www-form-urlencoded');
$req->content('var1=1&var2=2');

my $cookie_jar = HTTP::Cookies->new;
$cookie_jar->set_cookie(undef, "JSESSIONID", $jsessionid, "/", $host, $port);
$ua->cookie_jar($cookie_jar);

my $res = $ua->request($req);
if ($res->is_success) {
  print "SUCCESS\n";
  print $res->content;
} else {
  print "FAILED\n";
  exit;
}

localhostで送っていてクッキーが送信されなくて悩んだ。ドットが2個以上必要。

2006-03-19

[] ランダムな文字列生成

#! /usr/bin/perl

my @base = split(//, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");

for (1..100) {
    print &random_str(5,\@base), "\n";
}

sub random_str {
    my ($length, $base) = @_;
    my $result = "";
    for (1..$length) {
	$result .= $base->[rand(@$base)];
    }
    return $result;
}

本格的になんかするときは、