python telnet并将消息放入特定的日志fi中

2024-06-25 07:18:44 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我正在编写python脚本以登录telnet,它必须将日志文件写入操作文本在

绝对路径是

/netperf/mntpt/array1/home/prg_use/op.txt

python代码

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

终端的服务器端是

$

^{pr2}$

不过,我想写下来

lr: not found

op.txt

但是没有发生,有人能告诉我怎么做吗,谢谢

如果我编码

f=open("/netperf/mntpt/array1/home/prg_use/op.txt", "w")

它会显示出来的

no such file or directory '/netperf/mntpt/array1/home/prg_use/op.txt'

我想这不是路径问题,我可以触摸操作文本

,其

str_all=tn.read_until("$")

无法复制返回消息然后写入操作文本在


Tags: 文本importtxthomereadusealltn