-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject.py
More file actions
78 lines (57 loc) · 1.71 KB
/
Project.py
File metadata and controls
78 lines (57 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import sys
import os
import getpass
import subprocess
import pexpect
import fileinput
suPass = sys.argv[1]
user = getpass.getuser()
sudoCheck = "n"
#sudoCheck = raw_input("Have you configured sudo? y/n")
if sudoCheck == "n":
# suStart = getpass.getpass("Please Enter your root Password (It wont be stored)")
#python script location
def getScriptPath():
return os.path.dirname(os.path.realpath(sys.argv[0]))
location = getScriptPath()
# spawn a root shell with sudo or su depending on your linux
proc = pexpect.spawn("su")
proc.logfile = sys.stdout
# wait until the program finds the string password or Password
proc.expect("[Pp]assword")
# Send the password to waiting shell
proc.sendline(suPass)
# wait until command completed (# is a part of next prompt)
proc.expect("#")
#move working directory to script location
proc.sendline("cd " + location )
proc.expect("#")
#install sudo
proc.sendline("apt-get install sudo")
proc.expect("#")
#sudoers is protected against non root edits, script isn't running as root so chmod it.
proc.sendline("chmod 0777 sudoers")
proc.expect("#")
#add current user to sudoers
f = open('sudoers', 'r')
filedata = f.read()
f.close()
sudoChange = filedata.replace("<insert name here>", user)
f = open('sudoers','w')
f.write(sudoChange)
f.close()
#change sudoers permissions back to only root r/w
proc.sendline("chmod 0400 sudoers")
proc.expect("#")
#move sudoers to /etc
proc.sendline("scp sudoers /etc")
# remove root password from this script's memory
suPass = " "
#ensure working directory has not changed
proc.expect("#")
proc.sendline("cd " + location)
proc.expect("#")
proc.sendline("exit")
proc.expect("#")
proc.sendline("exit")
#move to next script (via Manager)