hi I am coding the python script to login telnet and it has to write log file to op.txt
the absolute path is
/netperf/mntpt/array1/home/prg_use/op.txt
the python code
ptlog.py
import getpass
import sys
import telnetlib
import time
host ="12.12.1.123"
user ="user_name"
passwd ="telnet_password"
tn = telnetlib.Telnet(host)
tn.read_until("login: ")
tn.write(user + "\n")
tn.read_until("Password: ")
tn.write(passwd + "\n")
tn.write("touch op.txt \n")
time.sleep(5)
tn.write("lr \n")
str_all=tn.read_until("$")
#f=open("/netperf/mntpt/array1/home/prg_use/op.txt", "w")
f=open("op.txt", "w")
f.write(str_all)
f.close()
tn.write("exit \n")
print tn.read_all()
the server side in terminal is
$
lr: not found
$
however, I wanna it wrote
lr: not found
in
op.txt
but it did not happen, can any one tell me how to do it, thank you
if I code
f=open("/netperf/mntpt/array1/home/prg_use/op.txt", "w")
it will show
no such file or directory '/netperf/mntpt/array1/home/prg_use/op.txt'
I think it is not the path problem, I can touch op.txt
, its
str_all=tn.read_until("$")
can not copy the return message and then write in op.txt
来源:https://stackoverflow.com/questions/48221195/python-telnet-and-put-the-message-in-a-particular-log-file