Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -rw-r--r-- 1 root root
- path = "/etc/hosts"
- fr = open(path,'r')
- b = fr.read()
- b = b+'something to write'
- fr.close()
- fw = open(path,'w')
- fw = os.system('echo %s|sudo -S python %s' % ('root', fw.write(b)))
- os.popen("sudo -S %s"%(open(path,'w')), 'w').write(admin_password)
- sudo chmod u+rwx /etc/hosts
- os.system("echo %s| sudo -S python etcedit.py %s" % ('rootpassword', 'host_name'))
- import os, subprocess
- import sys
- from sys import argv
- def etc_update(host_name, *args):
- path = "/etc/hosts"
- host_name = host_name[0]
- fw = open(path,'w')
- fw.write(host_name)
- etc_update(sys.argv[1:])
- import os
- import pexpect
- import sys
- import argparse
- def get_args():
- parser = argparse.ArgumentParser(description="Run as sudo!")
- parser.add_argument('-p', dest='privelege', action='store', help='also has rights')
- args = parser.parse_args()
- return vars(args)
- full_path = os.path.abspath(__file__)
- print("full_path = %s", full_path)
- if __name__ == '__main__':
- args = get_args()
- if args.get('privelege') == None:
- #check if it has sudo privelege and if not rerun with it.
- child = pexpect.spawn("sh", logfile = sys.stdout)
- child.sendline("sudo python %s -p root" % full_path)
- child.expect("assword", timeout = 100)
- child.logfile = None
- child.sendline("YOUR_PASSWORD")
- child.logfile_read = sys.stdout
- elif args.get('privelege') == 'root':
- #if it have root privelege do action
- path = "/etc/hosts"
- fr = open(path,'r')
- b = fr.read()
- b = b+'something to write'
- fr.close()
- fw = open(path,'w')
- fw.write(b)
- fw.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement