python如何生成文件_Python如何生成文件nam

我想知道社区能不能帮我,我是个编程新手。我试图通过ssh连接到包含列表.txt“一旦它登录到路由器,我就向每个人发送相同的命令,并将输出写入文件。但是,在下面的代码中,它会覆盖每个设备的输出。我需要为每个输出创建一个唯一的文件,其中包含列表.txt“文件。如果有人能帮我,我真的很感激。在import paramiko

import time

import os

def disable_paging(remote_conn):

'''Disable paging on a Cisco router'''

remote_conn.send("terminal length 0\n")

time.sleep(1)

# Clear the buffer on the screen

output = remote_conn.recv(1000)

return output

#Create variables

f = open('list.txt')

filepath = ('test/tas.txt')

username = 'test'

password = 'test'

#Create a for loop

for i in f:

remote_conn_pre = paramiko.SSHClient()

remote_conn_pre.set_missing_host_key_policy(

paramiko.AutoAddPolicy())

remote_conn_pre.connect(i, username=username, password=password)

remote_conn = remote_conn_pre.invoke_shell()

output = remote_conn.recv(1000)

disable_paging(remote_conn)

# Now let's try to send the router a command

remote_conn.send("\n")

remote_conn.send("show int des\n")

# Wait for the command to complete

time.sleep(2)

output = remote_conn.recv(88880000)

# print output

if not os.path.exists(os.path.dirname(filepath)):

os.makedirs(os.path.dirname(filepath))

with open(filepath, "w") as f:

f.write(output)

f.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值