0

I have written this Python script that checks if a file exists in a web server. And if it does exist, it copies is in the "found" directory on the local machine. However, since the web server is running Linux, and the full file path has slashes in it, it shows some errors. I have tried to solve it with "os.path" module but have been unsuccessful in my attempts.

import requests
import sys
import re

regex = re.compile(r"header(.*) footer", re.DOTALL)

data = {
    "name":"user01",
    "password":"secret"
}

r = requests.post('http://10.10.10.10/posts', data=data)
file = re.search(regex, r.text)

filename = sys.argv[1]

if file.group(1) != 'None':
    with open('found/' + filename, 'w') as f:
        f.write(file.group(1))
        print("Found: ", filename)

I get the following error:

ERROR
Traceback (most recent call last):                                                                                                            
  File "/home/shit.py", line 18, in <module>                                                                           
    with open('found/' + filename, 'w') as f:                                                                                                 
FileNotFoundError: [Errno 2] No such file or directory: 'found//boot/grub/grub.cfg'                                                           
Traceback (most recent call last):                                                                                                            
  File "/home/shit.py", line 18, in <module>                                                                           
    with open('found/' + filename, 'w') as f:                                                                                                 
FileNotFoundError: [Errno 2] No such file or directory: 'found//etc/adduser.conf'                                                             
Traceback (most recent call last):                                                                                                            
  File "/home/shit.py", line 18, in <module>                                                                           
    with open('found/' + filename, 'w') as f:                                                                                                 
FileNotFoundError: [Errno 2] No such file or directory: 'found//etc/apache2/apache2.conf'

I have come up with a workaround. All I have to do is use:

filename = sys.argv[1].replace("/", "_")

It saves the files with an "_" instead of "/". For example, it saves "/etc/passwd" as "_etc_passwd".

Stevey
  • 33
  • 5

0 Answers0