Archive for December, 2014

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()

Advertisements

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