how to save all the received message to a file

1. create a list, data []

2. define a function, to send commands, and receive the response, then append the response to the list

def srp (s, send, recv=”recv”, wait = 2):
send = s.send(“{}\n”.format (send))
time.sleep(wait)
recv = s.recv(5000000)
data.append(recv)
return recv

3. you can execute some commands and put all in the list

srp (s, “conf t”)
srp (s, “interface loop 111”)
srp (s, “ip address x.x.x.x/32”)
srp (s, “no shut”)

4. write the list to a file

f = open (“SWAN_install_uninstall_logs.txt”, “w”)
for line in data:
f.write (“{}\n”.format(line))
f.close()

Leave a comment

how to insert a local time to Python programmer and use it

imort time

t = str (time.strftime(“%Y%m%d–%H%M%S”))

then you can use the string t

for example:

filename = backup + “-” + t

Leave a comment

how to read a txt file and perform action based on the contents of the file

f = open (“device_list.txt”, “r”)

for lines in f:

ip = lines.strip (“\n”)

Then you can do something with the ip

Leave a comment

dictionary

dictionary is coded in curly braces, “key : value”

we can index the dictionary, the dictionary index operation uses the same syntax as that used for sequences, but the intem in the square brackets is a key, not a relative position.

space in the “key ” is counted.

>>> d = {“food”:’spam’, ‘quantity’ : 4 , ‘color ‘ : ‘pink’}
>>> d
{‘food’: ‘spam’, ‘color ‘: ‘pink’, ‘quantity’: 4}
>>>
>>>
>>> d [“food”]
‘spam’
>>>
>>>
>>> d [“color”]
Traceback (most recent call last):
File “<pyshell#65>”, line 1, in <module>
d [“color”]
KeyError: ‘color’
>>> d [“color “]

Leave a comment

list append and pop

list []

list. prepend (“x”), x will be added to the end of the list

list.pop (x), the x element in the list will pop out for ever!

>>> l=[3,4,5]
>>> l + [1,2,3]
[3, 4, 5, 1, 2, 3]
>>>
>>>
>>> l.append(‘NI’)
>>>
>>> l
[3, 4, 5, ‘NI’]
>>> l.pop (2)
5
>>>
>>>
>>>
>>> l
[3, 4, ‘NI’]

Leave a comment

ECDSA –ImportError: No module named

paramiko模块安装(win)与使用

Nov 22, 2013 19:28 / Python

116–> 0 Comments

安装

.安装Paramiko模块。下载Paramiko。解压并进入目录,输入python setup.py install 安装。

若提示 No module named ecdsa。下载https://pypi.python.org/pypi/ecdsa/ 。解压并进入目录,输入python setup.py install安装。

C:\Python27>cd ecdsa-0.11
C:\Python27\ecdsa-0.11>python setup.py install
running install
running build
running build_py
creating build
creating build\lib
creating build\lib\ecdsa
copying ecdsa\curves.py -> build\lib\ecdsa
copying ecdsa\der.py -> build\lib\ecdsa
copying ecdsa\ecdsa.py -> build\lib\ecdsa
copying ecdsa\ellipticcurve.py -> build\lib\ecdsa
copying ecdsa\keys.py -> build\lib\ecdsa
copying ecdsa\numbertheory.py -> build\lib\ecdsa
copying ecdsa\rfc6979.py -> build\lib\ecdsa
copying ecdsa\six.py -> build\lib\ecdsa
copying ecdsa\test_pyecdsa.py -> build\lib\ecdsa
copying ecdsa\util.py -> build\lib\ecdsa
copying ecdsa\_version.py -> build\lib\ecdsa
copying ecdsa\__init__.py -> build\lib\ecdsa
running install_lib
creating C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\curves.py -> C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\der.py -> C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\ecdsa.py -> C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\ellipticcurve.py -> C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\keys.py -> C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\numbertheory.py -> C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\rfc6979.py -> C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\six.py -> C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\test_pyecdsa.py -> C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\util.py -> C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\_version.py -> C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\__init__.py -> C:\Python27\Lib\site-packages\ecdsa
byte-compiling C:\Python27\Lib\site-packages\ecdsa\curves.py to curves.pyc
byte-compiling C:\Python27\Lib\site-packages\ecdsa\der.py to der.pyc
byte-compiling C:\Python27\Lib\site-packages\ecdsa\ecdsa.py to ecdsa.pyc
byte-compiling C:\Python27\Lib\site-packages\ecdsa\ellipticcurve.py to ellipticc
urve.pyc
byte-compiling C:\Python27\Lib\site-packages\ecdsa\keys.py to keys.pyc
byte-compiling C:\Python27\Lib\site-packages\ecdsa\numbertheory.py to numbertheo
ry.pyc
byte-compiling C:\Python27\Lib\site-packages\ecdsa\rfc6979.py to rfc6979.pyc
byte-compiling C:\Python27\Lib\site-packages\ecdsa\six.py to six.pyc
byte-compiling C:\Python27\Lib\site-packages\ecdsa\test_pyecdsa.py to test_pyecd
sa.pyc
byte-compiling C:\Python27\Lib\site-packages\ecdsa\util.py to util.pyc
byte-compiling C:\Python27\Lib\site-packages\ecdsa\_version.py to _version.pyc
byte-compiling C:\Python27\Lib\site-packages\ecdsa\__init__.py to __init__.pyc
running install_egg_info
Writing C:\Python27\Lib\site-packages\ecdsa-0.11-py2.7.egg-info
C:\Python27\ecdsa-0.11>
C:\Python27\ecdsa-0.11>
C:\Python27\ecdsa-0.11>

Leave a comment

re.match

>>> help (re.match)
Help on function match in module re:
match(pattern, string, flags=0)
Try to apply the pattern at the start of the string, returning
a match object, or None if no match was found.
>>>
>>>
>>>

re.match (‘what you want to find ‘, ‘from where you want to find’)

name_a=re.match

Leave a comment