IndexError:使用python连接到SFTP服务器时列出索引超出范围

审计报

我写了一个 python 脚本来使用公钥认证连接到 SFTP 服务器。代码从另一个名为“配置文件”的文本文件中获取服务器凭据,其格式为:

IP,用户名

IP2,用户名2

当配置文件包含超过 5 个 IP 和用户名时,它会引发错误(见下文)。虽然它不会影响输出但是错误的原因是什么以及如何解决它或者有什么更好的方法来做到这一点

我的代码:

import os
import pysftp
import socket
import paramiko
import time
import os.path
import shutil
import pysftp
import csv
from pathlib import Path
from stat import S_IMODE, S_ISDIR, S_ISREG

cnopts = pysftp.CnOpts()
cnopts.hostkeys=None

import os

privatekeyfile = os.path.expanduser("C:\\Users\\Rohan\\.ssh\\cool.prv")
mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)


config_file_path = "config15.txt"     
file = open(config_file_path, 'r')
reader = csv.reader(file)
all_rows = [row for row in reader]
for line in all_rows:
    server_ip = line[0]
    username = line[1]
    with pysftp.Connection(host=server_ip, username=username, private_key=mykey, cnopts=cnopts) as sftp:
        r = (socket.gethostbyaddr(server_ip))
        print("connection successful with ", r)

输出和错误:(如果配置文件中有 10 个 IP)

connection successful with  ('lootera', [], ['127.0.0.1'])
connection successful with  ('lootera', [], ['127.0.0.1'])
connection successful with  ('lootera', [], ['127.0.0.1'])
connection successful with  ('lootera', [], ['127.0.0.1'])
connection successful with  ('lootera', [], ['127.0.0.1'])
connection successful with  ('lootera', [], ['127.0.0.1'])
connection successful with  ('lootera', [], ['127.0.0.1'])
connection successful with  ('lootera', [], ['127.0.0.1'])
connection successful with  ('lootera', [], ['127.0.0.1'])
Traceback (most recent call last):
  File "C:/Users/Rohan/PycharmProjects/untitled1/m.py", line 30, in <module>
    server_ip = line[0]
IndexError: list index out of range
connection successful with  ('lootera', [], ['127.0.0.1'])
DSC

由于我们不知道配置文件,我们无法判断其中是否有错误。但是,您可以只检查一行是否包含两个值。这样你就可以避免IndexError

for line in all_rows:
    if len(line) != 2:
        continue
    server_ip = line[0]
    username = line[1]

还:

reader = csv.reader(file)
all_rows = [row for row in reader]
for line in all_rows:

相当低效。为什么不缩短呢?

reader = csv.reader(file)
for line in reader:

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

IndexError:使用python列出超出范围的索引

Python 错误:IndexError:使用 Selenium 抓取时列出索引超出范围

列出python Web服务器中的索引超出范围错误

IndexError:写入文件时列出索引超出范围

如何修复 IndexError:抓取时列出索引超出范围

IndexError:检查矩阵内容时列出索引超出范围

IndexError:在python中列出索引超出范围

IndexError:在python列表中列出索引超出范围

Python:IndexError:修改代码后列出索引超出范围

IndexError:使用 moto 列出超出范围的索引

IndexError:使用glob(),rsplit()列出索引超出范围

IndexError:使用pip列出超出范围的索引

Selenium Python-IndexError:遍历表时列出超出范围的索引

IndexError:尝试在python中为minesweeper建立网格时列出索引超出范围

Python IndexError:在vcf中查找SNP时列出索引超出范围

IndexError:在 Python 中运行 KMeans 时列出索引超出范围

IndexError:未找到某些内容时在python中列出索引超出范围

Python IndexError:使用CSV文件列出超出范围的索引

Python:IndexError:使用randint函数列出超出范围的索引

MySQL Python连接器IndexError:字节数组索引超出范围

IndexError:使用元组时列表索引超出范围

使用 python-gitlab 列出所有项目时列出索引超出范围

IndexError:列表索引超出范围(使用 python 的网络爬虫)

使用 split 方法时列出索引超出范围错误

IndexError:列表索引超出范围(Python)

IndexError:元组索引超出范围----- Python

Python:IndexError:元组索引超出范围

移动时列出索引超出范围

Python IndexError:使用列表作为可迭代对象时,列表索引超出范围