Теперь решил вынести функции в модуль:
Основной файл:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
import telnetlib
import test
f = open('ip.txt')
for host in f.readlines():
# print (host)
tl = test.connect()
test.comand(tl)
test.disconnect(tl)
test.py:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
import telnetlib
def connect():
user ="admin"
password ="pas"
# host = '172.29.114.141'
tn = telnetlib.Telnet(host,23,15)
# tn.set_debuglevel(1)
tn.read_until("Login:",5)
tn.write(user+"\n")
tn.read_until("Password:",5)
tn.write(password+"\n")
tn.read_until(">",5)
tn.write("ena\n")
tn.read_until("Password:",5)
tn.write(user+"\n")
return tn
def comand(tn):
# tn.write("sh ver\n")
# tn.write("sh inter port\n")
# tn.write("q\n")
f = open ('comand.txt')
for comands in f.readlines():
tn.write(comands+"\n")
print tn.read_all()
def disconnect(tn):
tn.close()
s = tn.read_all()
# print type (s)
print s
./run.py
Traceback (most recent call last):
File "./run.py", line 11, in <module>
tl = test.connect()
File "/home/chipset/python/test.py", line 11, in connect
tn = telnetlib.Telnet(host,23,15)
NameError: global name 'host' is not defined