码迷,mamicode.com
首页 > 编程语言 > 详细

Windows Live Writer - CNBlogs.CodeHighlighter - 测试一下Blog会如何显示Python源代码

时间:2015-09-12 06:17:28      阅读:481      评论:0      收藏:0      [点我收藏+]

标签:

 
技术分享
 
 
#!/usr/local/bin/python
# -*- coding: utf-8 -*-.
# line of utf-8 is for multi-language
# optional 4-spaces before line rule for Python.

import sys
sys.path.append("/homes/sli/pexpect-2.3/")
import pexpect
import time
import re
import os

statsre = re.compile("(May 1. \d+:\d+:\d+).*? Input  packets:[ ]+(\d+).*? Output packets:[ ]+(\d+).*? Errors: (\d+),", re.DOTALL)

# def function_name():
# The definition ends where the indentation ends.

def logging(fo, str):
    fo.write(str)
    print str

def process(prev, output):
    m = statsre.findall(output)
    val = int(m[0][3])
    #if prev:
    #    if (val-prev>0):
    #        print "%d drops at %s" % (val-prev, m[0][0])
    return val

# input() actually evaluates the input as Python code.
# raw_input() returns the verbatim string entered by the user.
routername = raw_input("enter routername:")   # raw_input is the same as input but omits the evaluation.

for i in range(10):
    print "Loop #%d" % i
    try:
        cct =  pexpect.spawn("ssh labroot@%s" % routername)  # ssh the router and answered yes/no first before this script
#        cct.expect("login: ")
#        cct.sendline("labroot")
        cct.expect("assword:")
        cct.sendline("lab123")
        cct.expect("> ")
        print "on router",routername, "1st try ok!"
        prompt = cct.before.split(\n)[-1]+">"    # correct one for the prompt
        prompt0 = cct.before
        prompt1 = cct.before.split(">")            # test prompt1
        prompt2 = cct.before.split(\n)           # test prompt2
        prompt3 = cct.before.split(\n)[-1]       # test prompt3

        print "prompt is", prompt
        print "prompt0 is", prompt0
        print "prompt1 is", prompt1
        print "prompt2 is", prompt2
        print "prompt3 is", prompt3
    except:
        time.sleep(5)
        cct =  pexpect.spawn("ssh labroot@%s" % routername)
#        cct.expect("login: ")
#        cct.sendline("labroot")
        cct.expect("assword:")
        cct.sendline("lab123")
        cct.expect("> ")
        print "on router", routername, "2nd try ok!"
        prompt = cct.before.split(\n)[-1]+">"
        print "prompt is", prompt

    cct.sendline(set cli screen-width 300)
    cct.expect(prompt)
    cct.sendline(set cli screen-length 0)
    cct.expect(prompt)
    cct.sendline(set cli timestamp)
    cct.expect(prompt)
    f = open(%s_%d.txt % (routername,i), w)

    # open with mode w for write, a for append

    # commands
    cct.sendline(show system uptime | no-more)
    cct.expect(prompt)
    print show system uptime | no-more
#    print cct.before            # test what‘s cct.before is, also the syntax for print
    f.write(cct.before)

    cct.sendline(show bgp summary | no-more)
    cct.expect(prompt)
    print show bgp summary | no-more
#    print(cct.before)          # test what‘s cct.before is, , also the syntax for print
    f.write(cct.before)

    cct.sendline(show chassis routing-engine | no-more)
    cct.expect(prompt)
    print show chassis routing-engine | no-more
#    print cct.before          # test what‘s cct.before is
    f.write(cct.before)

    cct.sendline(show system core-dumps | no-more )
    cct.expect(prompt)
    print show system core-dumps | no-more 
    f.write(cct.before)

    cct.timeout = 30
    cct.sendline(show krt queue | no-more)
    cct.expect(prompt)
    print show krt queue | no-more
    f.write(cct.before)

    cct.sendline(show system storage | no-more)
    cct.expect(prompt)
    print show system storage | no-more
    f.write(cct.before)

    cct.sendline(show system core-dumps | match "sep " | no-more)
    cct.expect(prompt)
    print show system core-dumps | match "sep " | no-more
    f.write(cct.before)

    cct.sendline(show krt table )
    cct.expect(prompt)
    print show krt table 
    f.write(cct.before)

    # check if new core dump on backup RE
    def extract_nbr(str):
        [int(s) for s in str.split() if s.isdigit()]  # extract all numbers from the string "str"

    cct.sendline(show system core-dumps | match ksync | count)
    cct.expect(prompt)
    f.write(cct.before)
    str1 = cct.before                            # print out should be <x> lines
    str2 = cct.before.split(count)[-1]
    str3 = cct.before.split(Count)[-1]         # print out should be : #n lines ( of ksyncd cores )
    str4 = test1 1909 test2 3030 test3 -55 test4 -1001 test5 2002 test6 6000
    print "string1 is : ", str1
    print "string2 is : %s " % str2
    print "string3 is : %s " % str3
    k2 = 1* [int(s) for s in str3.split() if s.isdigit()]   # output should be [n]
    print k2
    k3 = k2[0]                                               # output should be number n now, #n lines of ksyncd there :-)
    print "k3=",k3
    list = [int(s) for s in str4.split() if s.isdigit()]     # output should be a list of numbers [ n1 n2 n3 n4] for positive numbers
    k5 = list[0]                                             # negative numbers can‘t be extracted from this function!
    k6 = list[1]
    k7 = list[2]
    k8 = list[3]
#    k9 = list[4]
#    k10 = list[5]
#    k11 = list[6]
    print (k5,k6,k7,k8)

    cct.sendline(show system core-dumps | match ksync)
    cct.expect(prompt)
    f.write(cct.before)

    res = cct.before
    print res
    if (ksyncd in res) and k3 > 20 :
        print "Found new core dump!!!"
        f.write(New ksyncd core dump on backup\n)
        break
    else:
        f.write(no new ksyncd core dump on backup\n)
        print no new ksyncd core dump on backup


    cct.sendline(re-request chassis routing-engine master switch no-confirm | no-more)
    cct.expect(prompt)
    f.write(cct.before)
    print switchover just happened and will sleep %d seconds % cct.timeout

    # close the file
    f.close()

    cct.sendline(exit)

    time.sleep(cct.timeout)


$ ./login-1.py
enter routername:iqbal
Loop #0
on router iqbal 1st try ok!
prompt is labroot@Iqbal-re0>
prompt0 is
--- JUNOS 12.1R4-S2.2 built 2013-01-17 02:10:25 UTC
labroot@Iqbal-re0
prompt1 is [ \r\n--- JUNOS 12.1R4-S2.2 built 2013-01-17 02:10:25 UTC\r\nlabroot@Iqbal-re0]
prompt2 is [ \r, --- JUNOS 12.1R4-S2.2 built 2013-01-17 02:10:25 UTC\r, labroot@Iqbal-re0]
prompt3 is labroot@Iqbal-re0
show system uptime | no-more
show bgp summary | no-more
show chassis routing-engine | no-more
show system core-dumps | no-more
show krt queue | no-more
show system storage | no-more
show system core-dumps | match "sep " | no-more
show krt table
string1 is :   show system core-dumps | match ksync | count
Sep 11 12:55:39
Count: 16 lines


string2 is :
Sep 11 12:55:39
Count: 16 lines


string3 is : : 16 lines


[16]
k3= 16
(1909, 3030, 2002, 6000)
 show system core-dumps | match ksync
Sep 11 12:55:39
-rw-r--r--  1 root  field          0 Sep 10 09:54 /var/tmp/ksyncd.core.30
-rw-r--r--  1 root  field          0 Sep 10 09:54 /var/tmp/ksyncd.core.31
-rw-r--r--  1 root  field          0 Sep 10 09:54 /var/tmp/ksyncd.core.32
-rw-r--r--  1 root  field          0 Sep 10 09:54 /var/tmp/ksyncd.core.33
-rw-r--r--  1 root  field          0 Sep 10 09:54 /var/tmp/ksyncd.core.34
-rw-r--r--  1 root  field          0 Sep 10 09:54 /var/tmp/ksyncd.core.35
-rw-r--r--  1 root  field          0 Sep 10 09:54 /var/tmp/ksyncd.core.36
-rw-r--r--  1 root  field          0 Sep 10 09:54 /var/tmp/ksyncd.core.37
-rw-r--r--  1 root  field          0 Sep 10 09:54 /var/tmp/ksyncd.core.38
-rw-r--r--  1 root  field          0 Sep 10 09:54 /var/tmp/ksyncd.core.39
-rw-r--r--  1 root  field          0 Sep 10 09:54 /var/tmp/ksyncd.core.50
-rw-r--r--  1 root  field          0 Sep 10 09:54 /var/tmp/ksyncd.core.51
-rw-r--r--  1 root  field          0 Sep 10 09:54 /var/tmp/ksyncd.core.52
-rw-r--r--  1 root  field          0 Sep 10 09:54 /var/tmp/ksyncd.core.53
-rw-r--r--  1 root  field          0 Sep 10 09:54 /var/tmp/ksyncd.core.54
-rw-r--r--  1 root  field          0 Sep 10 09:54 /var/tmp/ksyncd.core.55


no new ksyncd core dump on backup
switchover just happened and will sleep 30 seconds
^CTraceback (most recent call last):
  File "./login-1.py", line 175, in <module>
    time.sleep(cct.timeout)
KeyboardInterrupt
$

Windows Live Writer - CNBlogs.CodeHighlighter - 测试一下Blog会如何显示Python源代码

标签:

原文地址:http://www.cnblogs.com/sli6000cn/p/4802454.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!