Google搜查列

 
顯示具有 perl 標籤的文章。 顯示所有文章
顯示具有 perl 標籤的文章。 顯示所有文章

2014年5月19日 星期一

雜湊-加密應用

雜湊在程式語言世界裡,大多用來處理密碼的儲存,不用明碼(太明顯)儲存在資料庫!
一般來說我的註冊密碼是 54321 ,那麼我存在MySql或MSSQL等資料庫裡,不會明目張膽的存 54321 ,而是會方入一個函式加密公式,比如說常用的MD5()函式,他會得到一串32個由0~9 A~F排列組合的加密文,而 md5("54321") 就會得到01cfcd4f6b8770febfb40cb906715822 ,我就會把這一串加密後的32位字串存入資料庫的密碼欄中。就算資料庫被人看到欄位內的加密文,因為MD5() 算是不可反運算得出54321 的加密方式,所以得知32位字串也不能反算出原密碼。

那我又如何作出檢證呢?


這種方式也是很常使用的,把我在登入時,輸入 44444 ,再使用 md5("44444") 算出的是 79b7cdcd14db14e9cb498f1793817d69 ,再拿去和資料庫密碼欄的 01cfcd4f6b8770febfb40cb906715822 if() 後發現密碼不相等,所以就可以顯示登入失敗了。



線上試用MD5() 加密
http://www.jdoit3.com/tools/md5-hash.php

2013年7月9日 星期二

使用Perl 發GTalk

常常在使用PERL管理我的系統,用得最多就是系統檢測
在管理裡,最希望是可以達到即時通知道Syster Administrator管理人

最即時收到訊息有手機的簡訊(短訊) ,電話的方式
桌上型電腦有MSN,SKYPE,GTalk,Pidgin等
而智慧型手機的通訊有SKYPE,GTalk,LINE,Whatsapp等……

寫這個程式的出發點是我的系統裡有UPS不斷電,但如在假日發生時,也可以透過即時通訊軟體通知管理人
我的UPS是山特(台灣飛瑞)



其管理軟體winpower裡,用得上的是郵件和簡訊
不過沒有即時通知功能
不過被我在安裝目錄下發現了一個UPSEVENT.CSV 的事件文字檔案
在裡面的我只關注4個事件代號和發生事件時間
00 市電斷電
03 市電覆電
04 RS232斷線
05 RS232連線

使用Perl定期檢讀這個檔案(例如每5分鐘),在發生事件時使用XMMP協定通知我的指定帳號
經過多方測試,使用GTALK是大宗表現不錯,也有其優點
1.在GMAIL裡可以記錄GTALK通訊內容(歷史事件)
2.也有PC和手機app 或GMAIL,都會同時通知到(NB,桌機,手機,WEB MAIL的GMAIL)
3.GTalk接口簡單,使用XMPP,但連線需TLS加密

編寫前,如果執行Perl的系統是windows ,請記得安裝XMPP套件模塊,是LINUX的話那要安裝一下XMPP喔

在傳送後,手機會收到GTALK的訊息(無奈傳送中文會產生亂碼,只好再研究一下囉)


另外,如果想要寫GTALK機器人的話,可以參考使用perl gtalk套件
Net::XMPP::Client::GTalk

#!/usr/bin/perl


use strict;
use Net::XMPP;
if(1==1) { #這裡可以方一些檢查的程式
  $temptext="EVENT\n";
  &sendgtalk("$temptext");#呼叫傳送gtalk
}


#副程式 傳送GTalk 訊息給指定帳號
sub sendgtalk{
        my $username = "flybird001";#傳送人帳號
        my $password = "xxxxxxxxxx";#傳送人密碼
        my $resource = "nagios";
        ## End of configuration
        $ARGV[0]="touser";#接收人帳號
        $ARGV[1]=shift;     #訊息內容
        my $len = scalar @ARGV;
       
        if ($len ne 2) { #檢查是否有兩個參數
           die "about [touser] and [message]?\n";
        }
       
        my @field=split(/,/,$ARGV[0]);
       
        #------------------------------------
       
        # Google Talk & Jabber parameters :
       
        my $hostname = 'talk.google.com';
        my $port = 5222;
        my $componentname = 'gmail.com';
        my $connectiontype = 'tcpip';
        my $tls = 1;
       
        #------------------------------------
       
        my $Connection = new Net::XMPP::Client();
       
        # Connect to talk.google.com
        my $status = $Connection->Connect(
               hostname => $hostname, port => $port,
               componentname => $componentname,
               connectiontype => $connectiontype, tls => $tls);
       
        if (!(defined($status))) {
           print "ERROR:  XMPP connection failed.\n";
           print "        ($!)\n";
           exit(0);
        }
       
        # Change hostname
        my $sid = $Connection->{SESSION}->{id};
        $Connection->{STREAM}->{SIDS}->{$sid}->{hostname} = $componentname;
       
        # Authenticate
        my @result = $Connection->AuthSend(
               username => $username, password => $password,
               resource => $resource);
       
        if ($result[0] ne "ok") {
           print "ERROR: Authorization failed: $result[0] - $result[1]\n";
           exit(0);
        }
       
        # Send messages
        foreach ( @field ) {
        $Connection->MessageSend(
                to       => "$_\@$componentname",
                resource => $resource,
                subject  => "Notification",
                type     => "chat",
                body     => $ARGV[1]);
        }

}

2008年10月2日 星期四

Perl使用 mail::sendmail MIME::Base64

最近要需要把server上的一些資訊
每天定時的E-mail到指定信箱

是管理員看倒是還好
但是是長官有要看,就要用HTML的mail格式了

HTML還要插入圖片,這下就不是那麼簡單了

努力了幾天,可以找到幾個套件,合併使用,可以達到我要的目的

用來把文字編碼成Base64的套件 MIME::Base64;
郵件寄送套件 Mail::Sendmail;

注意同顏色的變數,要一樣的才會插入圖片生效
而且最好用outlook收件才會有效果

#!/usr/bin/perl

use MIME::QuotedPrint;
use HTML::Entities;
use Mail::Sendmail 0.75; # doesn't work with v. 0.74!
use Mysql;
use MIME::Base64;

# MYSQL CONFIG VARIABLES
$host = "localhost";
$database = "useracc";
$tablename = "acc";
$user = "bird";
$pw = "12345";

# PERL MYSQL 建立連線()
$connect = Mysql->connect($host, $database, $user, $pw);

# SELECT DB 選擇資料庫
$connect->selectdb($database);

$myquery = "SELECT * FROM $tablename";

# EXECUTE THE QUERY 對連線下指令
$execute = $connect->query($myquery);

#取回得到的資料放入變數 $retnumber
my $retnumber = $execute->numrows();

#SQL CLOSE
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#使用localtime()取得現在時間
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime(time);

#年份要加上1900才是公元
$year=$year+1900;
#月份要加上1才正確
$mon=$mon+1;

#開始要預先編輯html的內容放入變數
my $html="$year 年 $mon 月 $mday 日 $hour : $min
";
$html=$html."$tablename 現在有$retnumber 筆資料
";

#使用. 符號合併 字串

#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ FILE encode64
#放入附件前,要先把圖檔作編碼為base64
open(FILE, "/home/eth0-day.png") or die "$!";
while (read(FILE, $buf, 60*57)) {
$ee= encode_base64($buf);
$ff =$ff.$ee;
}
close FILE;
#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ FILE encode64

#=================== mail資訊
%mail = (
from => 'report@domain.com',
to => 'you@domain.com',
subject => "XXX Server $year/$mon/$mday $hour:$min Report",
);
#=================== mail資訊end

#一些標準標籤設定宣告一下
$boundary = "====" . time() . "====";
$boundary = '--'.$boundary;
$mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";

$file = $^X; # This is the perl executable

open (F, $file) or die "Cannot read $file: $!";
binmode F; undef $/;
$mail{body} = encode_base64();
close F;

#---------------------------------- 真正編輯mail標頭及內容
$mail{body} = << END_OF_BODY;
$boundary Content-Type: text/html;
charset="UTF-8" Content-Transfer-Encoding: quoted-printable

$html
< src=""cid:eth0-day.png>

$boundary
Content-Type: image/png;
name="eth0-day.png"
Content-Transfer-Encoding: base64
Content-ID: <656020898@300920332-0c65>
Content-Description:
Content-Location: eth0-day.png"

$ff

$boundary--
END_OF_BODY

#---------------------------------- 真正編輯mail標頭及內容 結束


#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= 使用MAIL::SENDMAIL 寄出郵件
sendmail(%mail) || print "Error: $Mail::Sendmail::error\n";

2008年4月24日 星期四

把十六進位轉十進位

最近在找PERL有關於進位的轉換,好像比較少

為什麼呢?
因為在PERL的世界裡,不用宣告這個變數是什麼特別格式

一時是整數,有時是字串…
但你要知道如果變數內的2B如何變成十進位

也就是如果在螢幕上看到的是 A--->ASC II是65(這是十進位表示)---->或是十六進位表示是0x41

這次我是要把AAA當成十六進位的0x00414141轉為十進位的4276545

或許有人會問為什麼要這樣作,因為我目前遇到的問題是封包問題,因封包Packet分析的器Wireshark上某一欄位看到的是1B,我想轉成十進位來看裡面的東西

好比是IP位置,PORT號,轉成十進位比較好看

目的是把ASC II旳值轉成十進位看

#!/usr/bin/perl

use strict;
my $tt;
my $go1=chr(65); #大寫A的十進位為65
my $go2=chr(0x41); #大寫A的十六進位為41
my $go3=chr(0x41);

my $text="$go1$go2$go3"; #合起來當作是十六進位0AAA轉十進位來看

print "$text\n";
my $tot=0;
my $ligh=length($text);
for(my $i=0;$i < $ligh;$i++){
my $SC=substr($text,$i,1);

if(ord($SC)!=0 ){
$tt=int(ord($SC)/16)*(16**(($ligh*2)-($i*2)-1))+int(ord($SC)%16)*(16**(($ligh*2)-($i*2)-2));

#print " $i\t".ord($SC)."\t$SC\t";

print $tt,'+';

}
$tot=$tot+$tt;

}
print ' 轉成10進位後',"\t$tot\n";

2008年4月23日 星期三

最近買了一本好書





這一本不錯,有在台北天瓏找到中文版的
網路上也找到原文的電子書
下載
在於支持作者,我還是跑去買了一本
原文書要一千多,在天瓏的中文版,$680,就買了,厚度還是很厚
老闆說這本已經第九刷了

裡面除了解說IO::Socket以外,還說了物件的關係,<>角符號的使用、open()、sysopen()、read()、sysread()、以二進位方式開始檔案的binmode()……

還有很多網路protocol的使用方法
smtp,pop,ftp,imap,nntp,web,telnet……UDP程式設計、Broadcasting、Multicasting

還有用perl寫多執行緒應用程式……

很多很多,這本是一本很好的進階書

原文介紹:

Network Programming with Perl is a comprehensive, example-rich guide to creating network-based applications using the Perl programming language. Among its many capabilities, modern Perl provides a straightforward and powerful interface to TCP/IP, and this book shows you how to leverage these capabilities to create robust, maintainable, and efficient custom client/server applications.

The book quickly moves beyond the basics to focus on high-level, application programming concepts, tools, and techniques. Readers will find a review of basic networking concepts and Perl fundamentals, including Perl's I/O functions, process model, and object-oriented extensions. In addition, the book examines a collection of the best third-party modules in the Comprehensive Perl Archive Network, including existing network protocols for e-mail, news, and the Web.

The core of the book focuses on methods and alternatives for designing TCP-based client/server systems, and more advanced techniques for specialized applications. Some of the specific topics covered are: The Berkeley Sockets API; The TCP protocol and the IO::Socket API; FTP file-sharing service; The Net::Telnet module for adapting clients to interactive network services; SMTP, including how to create and send e-mails with multimedia attachments; HTTP and the LWP module for communicating with Web servers; Bulletproofing servers; Broadcasting and multicasting; and Interprocess communication with UNIX domain sockets.

Useful, working programs demonstrate ideas and techniques in action, including a real-time chat and messaging system, a program for processing e-mail containing MIME attachments, a program for mirroring an FTP site, and a Web robot.

Network Programming with Perl focuses on TCP/IP rather than just the common Web protocols. Modeled after the critically acclaimed TCP/IP Illustrated by W. Richard Stevens, this book achieves a level of detail far superior to most. It is an essential resource for network administrators and Perl programmers who are creating network applications.


2008年3月17日 星期一

使用perl來作telnet

你有試想跑telnet可以全自動化嗎?

或者你會想問說,什麼時候會用到自動telnet

嗯! 我想到的是tftp啦,也就是很多設備在更新時可以用

以下只對特別設備撰寫,並不能對其他設備使用,請自行修改


#!c:\perl\bin\perl.exe

print "請輸入IPHome的ip:";
my $PhoneIP=<>; #IPHome IP
chomp $PhoneIP;

print "輸入img file name:";
my $imgFile=<>;
chomp $imgFile;

print "輸入tftp server ip:";
my $tftp_ip=<>;
chomp $tftp_ip;

print "輸入更新過程參考時間(預設90/秒):";
my $sec=<>;
chomp $sec;
if($sec == ''){$sec=90;};


my ($forecast, $t);


use Net::Telnet ();
$t = new Net::Telnet;
$t->open(host=>$PhoneIP,port=>4159);

($forecast) = $t->waitfor('/MXP.*$/i');
print $forecast;
#$t->waitfor('/MXP.*$/');
$t->print("test -start_telnet");

($forecast) = $t->waitfor('/MXP.*$/i');
print $forecast;
#$t->waitfor('/MXP.*$/');
$t->print("quit");
print "第一次連線結束\n";

#=========================
print "建立第二次telnet";
$t2 = new Net::Telnet;
$t2->open(host=>$PhoneIP,Timeout=>$sec);

($forecast) = $t2->waitfor('/#.*$/i');
print $forecast;
$t2->print("cd /tmp/");
($forecast) = $t2->waitfor('/#.*$/i');
print $forecast;

$t2->print("tftp.sh $imgFile $tftp_ip");
#$t2->waitfor('/\#.*$/');
($forecast) = $t2->waitfor('/\-r.*$/i');
print $forecast;
$t2->print("cp fs.img /dev/mtdblock/0");
sleep $sec;
($forecast) = $t2->waitfor('/#.*$/i');
print $forecast;
$t2->print("ls -l");
#檢查檔案大小
($forecast) = $t2->waitfor('/#.*$/i');
print $forecast;
$t2->print("reboot");
print "------------ip為$PhoneIP的IPHome改完成動作,請按Enter開始瀏覽器視窗作設定------------";
<>;
system("\"C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE\" $PhoneIP");

2008年2月18日 星期一

使用perl驗證系統身份

如果有要作跟系統的shadow密碼檔作驗證,可以使用Crypt::PasswdMD5套件幫忙的

要先安裝perl-Crypt-PasswdMD5

如果你的是RH或FC系列,可以使用YUM安裝

#yum install perl-Crypt-PasswdMD5

在使用前,請確認你有讀取shadow的權限

試用程式:
#!/usr/bin/perl

#getpw*();
use Crypt::PasswdMD5 qw(unix_md5_crypt);
print "輸入使用者:";
$user = <>;
chomp $user;
print "輸入密碼:";
$pass = <>;
chomp $pass;

$line = `cat /etc/shadow | grep $user`;
if($line eq ''){
print "沒有這使用者\n";}else{
@shadow = split(/:/,$line);
$passwd_unix = $shadow[1];
$key = substr($passwd_unix,3,8),"\n";
$passwd_perl = unix_md5_crypt($pass,$key),"\n";

print 'passwd_unix = ',$passwd_unix,"\n";
print 'passwd_perl = ',$passwd_perl,"\n";

if ($passwd_unix eq $passwd_perl){print "驗證成功\n";}else{print "驗證失敗\n";}
}

參考網站:http://blog.pixnet.net/gisanfu/post/8179025

2007年9月14日 星期五

最近在用perl寫e-mail收集程式

perl在文字處理方面真的很讚,簡單幾個寫就可以了

最近在想這個東西

給程式一個含有網址的檔案

程式就會把這些網址裡所包括下一層的網址另存到另一個網址去了

(當然也可以另存包括的e-mail囉)

用perl寫不到50行就搞定了

不過還在寫一些重複出現的網址檢查步驟
perl程式碼如下
-------------------------------------------------------------------
#!perl

use LWP::Simple;

print
"輸入要尋找的網址:";

my $url=<> ;

my $file='tmp.html';
my $GoURL='GoURL.txt';
my $mailURL='mailURL.txt';


chomp $url;
my $url1=$url;
$url1 =~ s/[?].*//g;
print "$url1";
my $www = get("$url");
getstore($www, tmp.html); # 存入檔案
open(html,"> $file")||print "不能寫入$file";
print
html "$www";
close(html);

open (html,"< $file")||print "不能開啟$file";
open (mailurl,">>$mailURL")||print "不能開啟$mailURL";
open (url,">>$GoURL")||print "不能開啟$GoURL";
while(
$line=<html>){
chomp $line;
if(
$line =~ /(href="\S{1,100}\")/is){
my $url2=$1;
$url2 =~ s/href\=//ig;
$url2 =~ s/"
//g;
if($url2 =~ /b(/.*)/is){
print
url "$url1$1\n";
}
elsif($1 =~ /b(h.*)/is){
print
url "$url2\n";
}
elsif($url2 =~ /([?].*)/is){
print
"$1\n";
}
elsif($url2 =~ /b(m.*)/is){
my $mail=$1;
$mail =~ s/mailto://g;
if($mail =~ /(.*@.*)/ ){
print
mailurl "$1\n";
}
}
}
if(
$line =~ /(w{1,20}dw{1,20}@S{1,20}[.]S{1,20}tw)/is){
print
mailurl "$1\n";
}
if(
$line =~ /(w{1,20}dw{1,20}@S{1,20}[.]com)/is){
print
mailurl "$1\n";
}
if(
$line =~ /(w{1,20}dw{1,20}@S{1,20}[.]org)/is){
print
mailurl "$1\n";
}
if(
$line =~ /(w{1,20}dw{1,20}@S{1,20}[.]hk)/is){
print
mailurl "$1\n";
}
}
close(url);
close(mailurl);
close(html);
------------------------------------------------------------------
windwos下的直接執行exe檔下載測試

會自行產生GoURL.txt來放置收集到的網址
mailURL.txt來放置收集到的e-mail
比對兩次的關係,這會比對.com和.tw結尾的

比對成功,所以找到bk20403@yahoo.com和bk20403@yahoo.com.tw

這是試用版,現在的新版就己改進這個問題了,只比對一次

像我程式的這一段也做了限制只找.tw .com .org .hk的e-mail結尾的
==================================================
if($line =~ /(w{1,20}dw{1,20}@S{1,20}[.]S{1,20}tw)/is){
print
mailurl "$1\n";
}
if(
$line =~ /(w{1,20}dw{1,20}@S{1,20}[.]com)/is){
print
mailurl "$1\n";
}
if(
$line =~ /(w{1,20}dw{1,20}@S{1,20}[.]org)/is){
print
mailurl "$1\n";
}
if(
$line =~ /(w{1,20}dw{1,20}@S{1,20}[.]hk)/is){
print
mailurl "$1\n";
}
==================================================

是以免有心人拿去賣錢啦,所以做了限制~

Google Analytics