由于要升级ios的交换机数目太多70多台(+_+)!,每条命令进行键盘输入简直会疯掉。所以做个个js脚本使用crt加载登陆脚本一气呵成,比较爽!
注意:**** 部分要替换成用户名,密码。
-------------------------------------------ios.js文件内容如下----------------------------------------------------------------------------
#$language = "JScript"
#$interface = "1.0"
//升级Cisco2950 IOS的JScript
//作者:张锋
//日期:2007-04-28
//版本:V0.1
//--------- 自定义函数---------------------------------------
//获取月份的英文缩写 (例如:clock set 13:00:00 28 apr 2007)
//参考:jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec
//输入:{int类型[月份]}
//输出:{int类型}月份英文缩写
//日期:2007-04-28
//版本:v0.1 (helper class 1)
//作者:张锋
function getMonthEngShort(m){
var engShort = null ;
switch(m){
case 1: engShort="jan"; break;
case 2: engShort="feb"; break;
case 3: engShort="mar"; break;
case 4: engShort="apr"; break;
case 5: engShort="may"; break;
case 6: engShort="jun"; break;
case 7: engShort="jul"; break;
case 8: engShort="aug"; break;
case 9: engShort="sep"; break;
case 10: engShort="oct"; break;
case 11: engShort="nov"; break;
case 12: engShort="dec"; break;
}
return engShort;
}
//alert("测试 getMonthEngShort(4)="+getMonthEngShort(4));
//判断闰年函数
//参考:能被400整除,或者能被4整除而不能被100整除。
//输入:{int类型[年]}
//输出:{boolean类型}是否为闰年
//日期:2007-04-28
//版本:v0.1 (helper class 2)
//作者:张锋
function isLeapYear(y){
var r = false;
if(y%400==0 || (y%4==0 && y%100!=0 )){
r = true;
}
return r;
}
//alert("测试 isLeapYear(2000)="+isLeapYear(2000));
//获取第二天函数
//参考:
//输入:{int类型[年],int类型[月],int类型[日]}
//输出:{Date类型}第二天的日期
//日期:2007-04-28
//版本:v0.1 (helper class 3)
//作者:张锋
function getNextDay(y,m,d){
var nextY=0,nextM=0,nextD=0;
//alert(y+"-"+m+"-"+d);
switch(m){
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 :
if(d==31){
nextD = 1 ;
if(m==12){
nextM = 1 ;
nextY = y + 1 ;
}else{
nextM = m + 1 ;
nextY = y ;
}
}else{
nextD = d + 1 ;
nextM = m ;
nextY = y ;
};
//alert("case 1,3,5,7,8,10,12 :");
break;
case 4 :
case 6 :
case 9 :
case 11 :
if(d==30){
nextD = 1 ;
nextM = m + 1 ;
nextY = y ;
}else{
nextD = d + 1 ;
nextM = m ;
nextY = y ;
};
//alert("case 4,6,9,11 :");
break;
case 2 :
if( ( isLeapYear(y) && d==29 ) || ( !isLeapYear(y) && d==28 ) ){
nextD = 1 ;
nextM = 3 ;
nextY = y ;
}else{
nextD = d + 1 ;
nextM = m ;
nextY = y ;
};
//alert("case 2");
break;
}
//alert (nextY+"\n"+nextM+"\n"+nextD+"\n");
return new Date(nextY,(nextM-1),nextD);
}
//测试getNextDay(y,m,d);
//function testGetNextDay(y,m,d){
// alert("测试 getNextDay("+y+","+m+","+d+")="+getNextDay(y,m,d).getYear()+"-"+(getNextDay(y,m,d).getMonth()+1)+"-"+getNextDay(y,m,d).getDate());
//}
//testGetNextDay(2007,4,28);
//testGetNextDay(2000,2,28);
//testGetNextDay(2000,2,29);
//testGetNextDay(2007,2,28);
//testGetNextDay(2007,2,29);
//testGetNextDay(2007,12,31);
//设置系统时间 (例如:clock set 13:00:00 28 apr 2007)
//参考:jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec
//输入:{}
//输出:{string类型}设定cisco ios系统使用当前系统时间的命令串
//日期:2007-04-28
//版本:v0.1 (Called Class A)
//作者:张锋
function getClockStr(){
var r = "" ;
var nowDay = new Date();
var nowMonthID = nowDay.getMonth() + 1;
var engMonthShort = getMonthEngShort(nowMonthID);
r = nowDay.getHours() + ":" + nowDay.getMinutes() + ":" + nowDay.getSeconds();
r = r + " " + nowDay.getDate() + " " + engMonthShort + " " + nowDay.getYear();
r = "clock set " + r;
return r;
}
//alert("测试 getClockStr()="+getClockStr());
//设置重新启动时间为次日的t点(例如:reload at 4:00 28 apr)
//参考:jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec
//输入:{string类型[次日时间XX:XX格式]}
//输出:{string类型}设定cisco ios系统次日重新启动的时间命令串
//日期:2007-04-28
//版本:v0.1 (Called Class B)
//作者:张锋
function getReloadDateStr(t){
var r = "";
var nowDay = new Date();
var nextDate = null ;
var nowDate = nowDay.getDate() ;
var nowMonth = nowDay.getMonth() + 1;
var nowYear = nowDay.getYear();
var nextDay = getNextDay(nowYear,nowMonth,nowDate);
r = "reload at " + t + " " + nextDay.getDate() + " " + getMonthEngShort((nextDay.getMonth()+1));
return r;
}
//alert("测试 getReloadDateStr(4:00)="+getReloadDateStr("4:00"));
//------自定义函数结束-----------------------------
//-------实际运行。。。。。----------------------
//登陆交换机
crt.Screen.WaitForString ("Username: ");
crt.Screen.Send ("***********" + "\n");
crt.Screen.WaitForString ("Password: ");
crt.Screen.Send ("***********" + "\n");
crt.Screen.WaitForString (">");
crt.Screen.Send ("en" + "\n");
crt.Screen.WaitForString ("Password: ");
crt.Screen.Send ("***" + "\n");
//上传新版本IOS
crt.Screen.WaitForString ("#");
crt.Screen.Send ("archive tar /xtract tftp://10.61.8.230/c2950-i6k2l2q4-tar.121-22.EA9.tar flash:" + "\n");
//设置系统时间 clock set 13:00:00 28 apr 2007
crt.Screen.WaitForString ("#");
crt.Screen.Send (getClockStr() + "\n");
//设置重新启动时间
crt.Screen.WaitForString ("#");
crt.Screen.Send (getReloadDateStr("4:00") + "\n");
crt.Screen.WaitForString ("Proceed with reload? [confirm]");
crt.Screen.Send ("\n");
//设置IOS文件路径
crt.Screen.WaitForString ("#");
crt.Screen.Send ("config" + "\n");
crt.Screen.WaitForString ("Configuring from terminal, memory, or network [terminal]? ");
crt.Screen.Send ("\n");
//设置ios装载文件
crt.Screen.WaitForString ("(config)#");
crt.Screen.Send ("boot system flash:c2950-i6k2l2q4-mz.121-22.EA9.bin" + "\n");
//保存配置
crt.Screen.WaitForString ("(config)#");
crt.Screen.Send ("end" + "\n");
crt.Screen.WaitForString ("#");
crt.Screen.Send ("wr" + "\n");
//退出
crt.Screen.WaitForString ("#");
crt.Screen.Send ("exit" + "\n");