Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting admin password while copy file using shutil.copy?

I m using shutil.copy from python to copy a list of files. But when i copy the files to /usr/lib/ location, i m getting permission denied as i need to be an administrator to do that.

So How could i copy files with admin permission or how could i get the admin password from the user to copy the files?

Ideas would be appreciated

like image 295
katherine Avatar asked Sep 28 '10 09:09

katherine


3 Answers

Make the user run the script as an administrator:

sudo python-script.py

Unix already has authentication and password management. You don't need to write your own, and there will doubtless be security bugs if you try.

like image 170
Katriel Avatar answered Nov 14 '22 02:11

Katriel


To add to what katrielalex said: you can make the script run itself via sudo if you want. Here's a proof of concept:

import sys, os, subprocess

def do_root_stuff():
    print('Trying to list /root:')
    for filename in os.listdir('/root'):
        print(filename)

if __name__ == '__main__':
    print('Running as UID %d:' % os.geteuid())

    if os.geteuid() == 0:
        do_root_stuff()
    else:
        subprocess.check_call(['sudo', sys.executable] + sys.argv)
like image 29
EMP Avatar answered Nov 14 '22 03:11

EMP


Start your program with a user that is allowed to write there. For example login to root first (su) or run the script with sudo myscript.py.

like image 23
poke Avatar answered Nov 14 '22 01:11

poke