从 txt 文件中读取 IP 并连接到 python 中的 ftp
Reading IPs from txt file and connect to ftp in python
我正在尝试在 python 中编写一个脚本,它连接到我们所有的 ftps 并告诉我它们已启动并在连接时列出它们的目录。
我将尝试使用一个名为 "ips.txt" 的文件,我们所有的 ip 都在其中 - 每行一个和以下脚本:
import socket
import ftplib
username = "xxx"
password = "xxx"
for server in open("ips.txt", "r").readlines():
try:
ftp = ftplib.FTP(server)
welcome = ftp.getwelcome()
print (welcome)
try:
attempt = ftp.login(user=username, passwd=password)
success = ("[****] Working " + server + '\n')
print(success)
data = []
ftp.dir(data.append)
for lines in data:
print (lines)
except:
print (server, username, password)
pass
except:
print ("Timeout...")
但脚本似乎跳过了所有内容,只打印了 "Timeout..." :(
我是个python新手,请耐心等待
编辑:
删除外部 try/except 后,我得到了回溯:
Traceback (most recent call last):
File "ftp.py", line 12, in <module>
ftp = ftplib.FTP(server)
File "C:\Python3.5.1\lib\ftplib.py", line 118, in __init__
self.connect(host)
File "C:\Python3.5.1\lib\ftplib.py", line 153, in connect
source_address=self.source_address)
File "C:\Python3.5.1\lib\socket.py", line 693, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Python3.5.1\lib\socket.py", line 732, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
ips.txt 看起来像:
10.10.10.10
10.10.10.11
10.10.10.21
10.10.10.33
因此每个 IP 换行
根据您提供的文件,当您进行 readlines
调用时,您仍然保留每个 IP 末尾的换行符。这很可能就是您得到 gaierror
的原因。
在我这边复制,使用换行符,我的回溯产生:
>>> FTP('10.10.10.10\n')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ftplib.py", line 118, in __init__
self.connect(host)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ftplib.py", line 153, in connect
source_address=self.source_address)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socket.py", line 693, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socket.py", line 732, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
只需执行:
FTP(server.strip())
然后您将去掉 IP 末尾的 \n
,您至少应该调用 正确的 IP 地址。
或者,您 可以 尝试查看 splitlines
是否适合您,考虑到您正在处理单个 IP 地址列表,它可能是一个不错的选择.
splitlines 将为您删除针对 string
的换行符,因此您还需要在打开的对象上调用 read
。像这样:
for server in open("ips.txt", "r").read().splitlines():
我正在尝试在 python 中编写一个脚本,它连接到我们所有的 ftps 并告诉我它们已启动并在连接时列出它们的目录。
我将尝试使用一个名为 "ips.txt" 的文件,我们所有的 ip 都在其中 - 每行一个和以下脚本:
import socket
import ftplib
username = "xxx"
password = "xxx"
for server in open("ips.txt", "r").readlines():
try:
ftp = ftplib.FTP(server)
welcome = ftp.getwelcome()
print (welcome)
try:
attempt = ftp.login(user=username, passwd=password)
success = ("[****] Working " + server + '\n')
print(success)
data = []
ftp.dir(data.append)
for lines in data:
print (lines)
except:
print (server, username, password)
pass
except:
print ("Timeout...")
但脚本似乎跳过了所有内容,只打印了 "Timeout..." :(
我是个python新手,请耐心等待
编辑: 删除外部 try/except 后,我得到了回溯:
Traceback (most recent call last):
File "ftp.py", line 12, in <module>
ftp = ftplib.FTP(server)
File "C:\Python3.5.1\lib\ftplib.py", line 118, in __init__
self.connect(host)
File "C:\Python3.5.1\lib\ftplib.py", line 153, in connect
source_address=self.source_address)
File "C:\Python3.5.1\lib\socket.py", line 693, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Python3.5.1\lib\socket.py", line 732, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
ips.txt 看起来像:
10.10.10.10
10.10.10.11
10.10.10.21
10.10.10.33
因此每个 IP 换行
根据您提供的文件,当您进行 readlines
调用时,您仍然保留每个 IP 末尾的换行符。这很可能就是您得到 gaierror
的原因。
在我这边复制,使用换行符,我的回溯产生:
>>> FTP('10.10.10.10\n')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ftplib.py", line 118, in __init__
self.connect(host)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ftplib.py", line 153, in connect
source_address=self.source_address)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socket.py", line 693, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socket.py", line 732, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
只需执行:
FTP(server.strip())
然后您将去掉 IP 末尾的 \n
,您至少应该调用 正确的 IP 地址。
或者,您 可以 尝试查看 splitlines
是否适合您,考虑到您正在处理单个 IP 地址列表,它可能是一个不错的选择.
splitlines 将为您删除针对 string
的换行符,因此您还需要在打开的对象上调用 read
。像这样:
for server in open("ips.txt", "r").read().splitlines():