Google搜查列

 

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");

沒有留言:

Google Analytics