Archive

Archive for the ‘MoreScript’ Category

[ Perl ] 원하는 월과 년도의 달력을 출력한다.

February 4th, 2007



caleander.pl


acount#> perl caleander.pl month year

[원하는 월과 년도의 달력을 출력한다.]


#!/usr/bin/perl


use integer;


%mon_ord = (
"jan" => 1, "feb" => 2, "mar" => 3, "apr" => 4,
"may" => 5, "jun" => 6, "jul" => 7, "aug" => 8,
"sep" => 9, "oct" => 10, "nov" => 11, "dec" => 12,
);


@monames = ("",
" 1월", " 2월", " 3월", " 4월",
" 5월", " 6월", " 7월", " 8월",
"9월", " 10월", " 11월", " 12월"

);


@monlens = (0,31,28,31,30,31,30,31,31,30,31,30,31);


die "Usage: cal.pl [month] [year]n" if $#ARGV > 1;


($t,$t,$t,$t,$mon1,$year1,$t,$t,$t) = localtime(time);
$mon = (defined $ARGV[0]) ? $ARGV[0] : $mon1+1;


if ($mon =~ /^ *d{1,2} *$/)
{
die "Month must be between 1 and 12!n" unless ($mon>=1 && $mon<=12);
}
else
{
$mon = $mon_ord{lc(substr($mon,0,3))};
die "Wrong month name: $ARGV[0]!n" unless defined($mon);
}


$year = (defined $ARGV[1]) ? $ARGV[1] : $year1+1900;
die "Wrong year: $year!n" unless $year =~ /^ d{4} *$/;
$year = int($year);
die "Year must be greater than 0!n" unless $year>0;
print "nt$monames[$mon] $yearnnSun Mon Tue Wed Thu Fri Satn";
$monlens[2] = 29 if ($year%400==0) || (($year%4==0) && ($year%100!=0));
—$year;
$st = 1 + $year
365 + $year/4 – $year/100 + $year/400;
for ($i=1; $i<$mon; ++$i)
{
$st += $monlens[$i];
}
$st %= 7;
for ($i=0; $i<$st; ++$i)
{
print " ";
}
for ($i=1; $i<=$monlens[$mon]; ++$i)
{
printf "%3d ", $i;
print "n" if ($st+$i)%7==0;
}
print "nn";



[결과]


[sugo@www][8:35pm][~/script]> perl caleander.pl


2월 2007


Sun Mon Tue Wed Thu Fri Sat
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28

sugo MoreScript

[ Script ] inet_ntoa() 구현하기(IP변환 함수)

February 3rd, 2007

inet_ntoa 함수는 인터넷 네트웍 주소(Ipv4)를 인터넷 표준 쩜 표기법 형태의 주소로 변환합니다.
MySQL이나 c/c++ 에는 있지만 javascript 나 ActionScript에는 없다.


예들들면
3690862078 를 ‘inet_ntoa’ 요 함수를 통하게되면
‘219.254.21.254’로 변한다!


IP주소 변환 함수
/**
* ip2long는 IP주소를 긴주소로 변환한다.
**/
function ip2long(ip)
{
    var ips = ip.split(‘.’);
    var iplong = 0;
    with (Math)
    {
    iplong =
        ips0pow(256,3)+ips
        1
pow(256,2)+ips
        2
pow(256,1)+ips
        3
pow(256,0)
    }
    return iplong;
}



/**
* long2ip는 긴 주소를 IP주소로 변환한다.
**/
function long2ip(l)
{
    with (Math)
    {
        var ip1 = floor(l/pow(256,3));
        var ip2 = floor((l%pow(256,3))/pow(256,2));
        var ip3 = floor(((l%pow(256,3))%pow(256,2))/pow(256,1));
        var ip4 = floor((((l%pow(256,3))%pow(256,2))%pow(256,1))/pow(256,0));
    }
    return ip1 + ‘.’ + ip2 + ‘.’ + ip3 + ‘.’ + ip4;
}


/**
* lastIP는 mask인자에 따른 해당 위치의 값을 리턴한다.
**/
function lastIP(ip, mask)
{
    return ip + (Math.pow(2, (32 – mask))-1);
}


Bonus
정규식을 사용하여 현재 IP값이 유효한지 체크할수 있다!
/^d{1,3}.d{1,3}.d{1,3}.d{1,3}$/

sugo MoreScript

[Shell] 특정포트 접속현황 모니터링

February 3rd, 2007

시스템의 특정 포트 사용현황을 보여준다.
현재 시스템에서는 아파치(80)와 SSH포트에 연결된 목록을 보여준다.


ServerConnect.sh
——————-
——————-——————-——————-
N=`hostname`


while(true)
do


DT=`date +’%y/%m/%d %H:%M:%S’`
A=`netstat an | grep 192.168.1.141.80 | grep EST | wc -l`
B=`netstat -an | grep 192.168.1.141.22 | grep EST | wc -l`
C=`netstat -an | grep 192.168.1.141 | grep EST | wc -l`
echo "|
————————————————————————————————|"
echo "| WAS Socket Monitor |"
echo "|————————————————————————————————-|"
echo "| Server : "$HN" Date : "$DT" |"
echo "|————————————————————————————————-|"
echo "| 192.168.1.141:80 | 192.168.1.141:22 | 192.168.1.141:22:*|"
echo "|————————————————————————————————-|"
echo "|————" $A "————|————" $B "————|————" $C "————|"
echo "|————————————————————————————————-|"
sleep 2
clear
done
——————-——————-——————-——————-

sugo MoreScript