2008年4月24日 星期四
把十六進位轉十進位
為什麼呢?
因為在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日 星期三
最近買了一本好書

在於支持作者,我還是跑去買了一本
裡面除了解說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
嗯! 我想到的是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驗證系統身份
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
2008年2月4日 星期一
新研究之一,電話來電控電源
那熱當機的話就只好到機器前面,把電源重開,這樣就很累人了
萬一有長假期,又要跑回公司重開,光用說的累了
別人的家電控制系統太高LEVEL,有點大材小用
自己找了一下,找到4n25小晶片(PDF datasheet)光耦和,加上小電路就可以用了
結合之前作了冷氣搖控器,同一個8051就可以一起工作了
所以設計了下圖的小電路:
成品圖:因為是跟8051連線的,給8051控制
所以有4線連接
+:是8051的5V電源連線
回:是測試是否有來電,有來電就回傳5v道通,告知8051有來電
C:控制4N25上的斷電器跳躍,0為OFF,1為ON
-接地:接8051電路接地
8051單晶片上的程式碼(組合語言):
| ORG 00H JMP START ORG 50H START: SETB P0.0 SETB P0.1 ;註解:冷氣開關程式 START1: CLR P1.0 CALL DELAY1 SETB P1.0 MOV R5,#60 CALL D11 CLR P1.0 CALL DELAY1 SETB P1.0 CALL DELAY2 JMP START1 ;註解:電源控制開關副程式 APP1: CLR P0.1 MOV R5,#250 CALL D11 SETB P0.1 JMP START1 ;註解:五秒鐘空迴圈副程式 DELAY1: MOV R5,#30 D11: MOV R6,#40 D12: MOV R7,#249 DJNZ R7,$ DJNZ R6,D12 DJNZ R5,D11 RET ;註:空迴圈監控來電接腳P0.0副程式30分鐘 DELAY2: MOV R4,#45 D21: MOV R5,#200 D22: MOV R6,#200 D23: MOV R7,#200 D24: CLR P0.0 JB P0.0,APP1 DJNZ R7,D24 DJNZ R6,D23 DJNZ R5,D22 DJNZ R4,D21 RET END |
上面還有指示燈,嘿~
不過呢,這個系統是沒有身份驗證的,也就是任何人打電話來,都會重開電源和冷氣
我是用家裡的或公司的ADSL線路,基本上是不公開的
如果要作驗證密碼,那要加上HT9170等,把電話上的按鍵傳到8051分析,驗證通過後再作其他所需要的動作
運動過程影片: