Scripts Review
  • ตัวอย่างการใช้งาน Syslog Server เพื่อรับ log จาก Cisco Router
    Syslog Server หรืออาจจะเรียกเป็น Syslog Daemon ในที่นี้เป็นซอร์ฟแวร์ที่ทำหน้าที่ รับ, บันทึก logs, แสดงผล,
    http://www.itmanage.info/technology/router/syslog/kiwi_syslog_server.html


    Forward log to keyboard vb-script example by kiwi
    Function Main()

    Dim objShell, MsgTxt, MsgHostAddress
    Set objShell = CreateObject("WScript.Shell")
      MsgTxt =        Fields.VarRawMessageText
      MsgHostAddress = Fields.VarPeerAddress
      objShell.sendkeys MsgHostAddress & vbtab & MsgTxt
      objShell.sendkeys "{ENTER}"

    End Function


    Basic Linux Command
    • [li]ls เอาไว้ดูว่าในโฟลเดอร์ที่เราอยู่มีอะไรบ้าง[/li]
      [li]cd xxx เหมือน windows เข้าโฟลเดอร์[/li]
      [li]cd .. ออกจากโฟลเดอร์[/li]
      [li]./ filename เป็นการสั่ง run ไฟล์[/li]
      [li]mkdir xxx สร้างโฟลเดอร์[/li]
      [li]rmdir xxx ลบโฟลเดอร์[/li]
      [li]rm xxx ลบไฟล์[/li]
      [li]cp dir/file dir2/file copy จาก โฟลเดอร์ dir -> dir2[/li]
      [li]nano filename สร้างไฟล์ใหม่หรือเปิดไฟล์ที่มีอยู่แล้ว เป็น text editor ตัวนึง[/li]

    show cdp neighbor
    ./cdp x.x.x.x
    Output = Local Intrfce -> Port ID  Loopback Neighbor  Device ID

    cdp file
    #!/usr/bin/php
    <?php
    $res = shell_exec("./do1.sh $argv[1]");
    shell_exec("./do1.sh $argv[1] > output1.txt");
    $ex = explode("Port ID",$res);
    $res = trim($ex[1]);
    $ex = explode("\n",$res);

    function getip($interface,$hostname) {
            global $argv;
            $hostname = explode(".",$hostname);
            $hostname = $hostname[0];
            $do2 = "#!/usr/bin/expect\n";
            $do2 .= "set ip [lrange \$argv 0 0]\r\n";
            $do2 .= "spawn telnet \${ip}\r\n";
            $do2 .= "expect \"Username:\"\r\n";
            $do2 .= "send \"xxx\\n\"\r\n";
            $do2 .= "expect \"Password:\"\r\n";
            $do2 .= "send \"yyy\\n\"\r\n";
            $do2 .= "expect \">\"\r\n";
            $do2 .= "send \"enable\\n\"\r\n";
            $do2 .= "expect \"Password:\"\r\n";
            $do2 .= "send \"yyy\\n\"\r\n";
            $do2 .= "expect \"#\"\r\n";
            $do2 .= "send \"termon len 0\\n\"\r\n";
            $do2 .= "expect \"#\"\r\n";
            $do2 .= "send \"show cdp ne ".trim($interface)." detail\\n\"\r\n";
            $do2 .= "expect \"#\"\r\n";
            $do2 .= "send \"ping $hostname\\n\"\r\n";
            $do2 .= "expect \"#\"\r\n";
            $do2 .= "send \"quit\\n\"\r\n";
            $fp = fopen("do2.sh","w+");
            fwrite($fp,$do2);
            fclose($fp);
            shell_exec("chmod 777 ./do2.sh");
            //$res = shell_exec("./do2.sh $argv[1] | grep \"IP address:\"");
            shell_exec("./do2.sh $argv[1] > output2.txt");
            //$ex = explode("\n",trim($res));
            $res = shell_exec("cat output2.txt | grep \"ICMP \"");
            $res = explode(" to ",$res);
            if(count($res)>1) $res = explode(",",$res[1]);
            if(trim($res[0])!="") {
                    return sprintf("%-16s",trim($res[0]));
            } else {
                    $res = shell_exec("./do2.sh $argv[1] | grep \"IP address:\"");
                    $ex = explode("\n",trim($res));
                    return sprintf("%-16s",trim(str_replace("IP address: ","",$ex[0]))."*");
            }
    }
    for($i=0; $i<count($ex)-1; $i++) {
            $tmp = trim($ex[$i])."\r\n";
            $ex2 = explode(" ",$tmp);
            $dhost = $ex2[0];
            $ex3 = explode(".",$dhost);
            //if(strlen($ex2[0])==17) {     
                    //$sint = substr
            //} else
            if(strlen($ex2[0])>18) {
                    //echo trim($ex[++$i]);
                    $ex4 = explode(" ",trim($ex[++$i])); 
                    $sint = trim($ex4[0])." ".trim($ex4[1]);
                    $dint = trim($ex4[count($ex4)-2])." ".trim($ex4[count($ex4)-1]);

            } else {
                    $dint = trim($ex2[count($ex2)-2])." ".trim($ex2[count($ex2)-1]);
                    $sint = substr($tmp,17,16);
            }
            echo trim($sint)." \t-->\t".trim($dint)." \t".getip($sint,$dhost)."  \t".trim($dhost)."\r\n";
    }

    //echo "=======================================\r\n";
    //if($argv[2]!="") {                   
    $res = shell_exec("./do3.sh $argv[1] | grep 10.150");
    echo $res;     
    $res = shell_exec("./do3.sh $argv[1] | grep 10.164");
    echo $res;
    $res = shell_exec("./do3.sh $argv[1] | grep 10.33"); 
    echo $res;
    //}
    ?>

    do1.sh file
    #!/usr/bin/expect
    set ip [lrange $argv 0 0]
    spawn telnet ${ip}
    expect "Username:"
    send "xxx\n"
    expect "Password:"
    send "yyy\n"
    expect ">"
    send "enable\n"
    expect "Password:"
    send "yyy\n"
    expect "#"
    send "ter len 0\n"
    expect "#"
    send "show cdp neighbors\n"
    expect "#"
    send "quit\n"

    show module
    ./showmod x.x.x.x
    output =
    WS-X6704-10GE&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  TBM2417IWI<br />|___WS-F6700-DFC3CXL&nbsp; &nbsp; TBM2419L2K<br />RSP720-3CXL-GE&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; KBF2356ANZ<br />|___7600-PFC3CXL&nbsp; &nbsp; &nbsp; &nbsp; KBF2356AEV<br />|___7600-MSFC4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; KBF2356ACZ<br />WS-X6724-SFP&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TBM2417K38<br />|___WS-F6700-DFC3CXL&nbsp; &nbsp; TBM2419L2L


    showmod file
    #!/usr/bin/php
    <?php
            $res = shell_exec("./do4.sh $argv[1]");
            $ex = explode("module",$res);
            $showmodule = trim($ex[count($ex)-1]);
            $ex = explode("\r\n\r\n",$showmodule);
            $text1 = explode("\n",$ex[0]);
            $text2 = explode("\n",$ex[2]);

            $module = array();
            $submod = array();
            for($i=2; $i<count($text1); $i++) {
                    $module[$i-2]['mod'] = (int)substr($text1[$i],0,3);
                    $module[$i-2]['model'] =  substr($text1[$i],49,17);
                    $module[$i-2]['sn'] = substr($text1[$i],68,10);
            }
            //echo "**********************************\n";
            for($i=2; $i<count($text2); $i++) {
                    $submod[$i-2]['mod'] = (int)substr($text2[$i],0,3);
                    $submod[$i-2]['model'] = substr($text2[$i],33,16);
                    $submod[$i-2]['sn'] = substr($text2[$i],52,10);
            }

            for($i=0; $i<count($module); $i++) {
                    echo $module[$i]['model']."\t".$module[$i]['sn']."\n";
                    for($j=0; $j<count($submod); $j++) {
                            if($module[$i]['mod']==$submod[$j]['mod']) echo "|___".$submod[$j]['model']."\t".$submod[$j]['sn']."\n";
                    }
            }
            //echo $card;
            //print_r($ex);
            //echo $showmodule;

    ?>

    do4.sh file
    #!/usr/bin/expect
    set ip [lrange $argv 0 0]
    spawn telnet ${ip}
    expect "Username:"
    send "xxx\n"
    expect "Password:"
    send "yyy\n"
    expect ">"
    send "enable\n"
    expect "Password:"
    send "yyy\n"
    expect "#"
    send "show module\n"
    expect "#"
    send "quit\n"

    show xxx/config Cisco by SecureCRT
    Example show inventory
    # $language = &quot;VBScript&quot; &#039; xxx.vbs<br /># $interface = &quot;1.0&quot;<br /><br />&#039;==========================================================================<br />&#039; DATE&nbsp; : 26/10/2010<br />&#039; UPDATED: <br />&#039; Thank : Brian Desmond<br />&#039;==========================================================================<br /><br />Sub Main<br />	Const username = &quot;plawansai&quot; &#039; Username to use for login<br />	Const password = &quot;rmutt&quot; &#039; Password for corresponding user<br />	Const loginPass = &quot;rmutt&quot; &#039; Password to use for password only login (aka no aaa new-model)<br />	<br />	Const oldEnablePass = &quot;rmutt&quot; &#039; The current enable Password<br />	<br />	Const DEVICE_FILE_PATH = &quot;C:sh inv.txt&quot; &#039; สั่งให้ไปอ่าน list ip จาก File ซึ่งอยู่ folder เดียวกับตัว script<br />	<br />	Dim fso<br />	Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)<br />	<br />	Dim fil<br />	Set fil = fso.OpenTextFile(DEVICE_FILE_PATH)<br />	<br />	Dim ip<br />	Dim name<br />	Dim protocol<br />	Dim line<br />	Dim cnxnString<br />	While Not fil.AtEndOfStream<br />		line = fil.ReadLine<br />		<br />		name = Split(line, &quot;;&quot;)(0)<br />		ip = Split(line, &quot;;&quot;)(1)<br />		protocol = Split(line, &quot;;&quot;)(2)<br />		<br />		Select Case protocol<br />			Case &quot;Telnet&quot;<br />				cnxnString = &quot;/TELNET &quot; &amp; ip &amp; &quot; 23&quot;<br />			Case &quot;SSH2&quot;<br />				cnxnString = &quot;/SSH2 /L &quot; &amp; username &amp; &quot; /PASSWORD &quot; &amp; password &amp; &quot; /C 3DES /M MD5 &quot; &amp; ip<br />			Case &quot;SSH1&quot;<br />				cnxnString = &quot;/SSH1 /L &quot; &amp; username &amp; &quot; /PASSWORD &quot; &amp; password &amp; &quot; /C 3DES /M MD5 &quot; &amp; ip<br />		End Select	<br />				<br />		&#039;Connect By Telnet&#039;<br />		crt.Screen.Synchronous = True<br />		crt.Session.Connect cnxnString<br />		If protocol = &quot;Telnet&quot; Then<br />		&#039;Authentication by TACACS+&#039;<br />			Dim index<br />			index = crt.Screen.WaitForStrings(&quot;Username:&quot;, &quot;Password:&quot;)<br />		<br />			If index = 1 Then <br />				crt.Screen.Send username &amp; vbCr	<br />				crt.Screen.WaitForString &quot;Password:&quot;<br /> 				crt.Screen.Send password &amp; vbCr<br />			End If<br />		End If<br />		<br />		&#039;Enable&#039;<br />		crt.Screen.WaitForString &quot;&gt;&quot;<br />		crt.Screen.Send &quot;en&quot; &amp; vbCr <br />		crt.Screen.WaitForString &quot;Password:&quot;<br />		crt.Screen.Send oldEnablePass &amp; vbCr<br /><br />		&#039;Privilege Mode&#039;<br />		&#039;crt.Screen.WaitForString &quot;#&quot;<br />		&#039;crt.Screen.Send &quot;conf t&quot; &amp; vbCr<br />		&#039;crt.Screen.WaitForString &quot;(config)#&quot;<br /><br />		&#039;***** Command Input *****&#039;<br />			crt.Screen.WaitForString &quot;#&quot; &#039; รอเครื่องหมาย #<br />			crt.Screen.Send &quot;sho inv&quot; &amp; vbCr &#039; ส่ง string sho inv<br />			crt.Screen.WaitForString &quot;#&quot;<br />		<br />		&#039;***** Disconnect *****&#039;<br />		crt.Session.Disconnect 			<br />	Wend<br />	<br />	fil.Close <br />End Sub


    sho inv.txt example
    ;10.5.0.1;Telnet<br />;10.5.0.2;Telnet


    ตั้งเวลาให้ save config ใน router
    		crt.Screen.Send &quot;conf t&quot; &amp; vbCr<br />		crt.Screen.WaitForString &quot;(config)#&quot;<br />		crt.Screen.Send &quot;kron policy-list Backupcfg&quot; &amp; vbCr<br />		crt.Screen.WaitForString &quot;(config)#&quot;<br />		crt.Screen.Send &quot;cli show run | redirect tftp://10.5.3.22/&quot; &amp; ip &amp; &quot;.cfg&quot; &amp; vbCr &#039; ได้ ip มาแล้วก็เอามาใช้ประโยชน์ซะ<br />		crt.Screen.WaitForString &quot;(config)#&quot;<br />		crt.Screen.Send &quot;kron occurrence Backupcfg in 1:00:00 recurring&quot; &amp; vbCr &#039; ให้เก็บวันละครั้ง<br />		crt.Screen.WaitForString &quot;(config)#&quot;<br />		crt.Screen.Send &quot;policy-list Backupcfg&quot; &amp; vbCr<br />		crt.Screen.WaitForString &quot;(config)#&quot;<br />		crt.Screen.Send &quot;end&quot; &amp; vbCr<br />		crt.Screen.WaitForString &quot;#&quot;

    ตรวจสอบการทำงานของ kron
    R1#show kron schedule
    [center]Cr: boonmeelive & P'Phong@AIT[/center]
    วิธีทำให้ SecureCRT save session ลงไฟล์ auto

    image
    [center]Credit: P'Pong@AIT[/center]
    การเขียน Shell Script
    http://www.vwin.co.th/document.php?node=5
    8)
  • 1 Comment sorted by
  • Script for Configure Cisco Router & Switch by SecureCRT

    http://upload.siamza.com/1744466

    How to use:
    1. Open vbs file with Text Editor such as EditPlus.
    2. Change user and password (abc & bcd).
    3. Const DEVICE_FILE_PATH = "C:\IP.txt" < Check this line for txt file path.
    4. Change the send & wait string depend which you want:
      crt.Screen.Send "access-list 1 permit 10.122.99.0 0.0.0.255" & vbCr
      crt.Screen.WaitForString "(config)#"
    5. Open the IP.txt and change the IP Address.
    6. Open SecureCRT > Script > Run...your vbs file.