How to run python script in PyCharm with sudo privileges?

0 votes

I have written a macchanger script in PyCharm and I am using sudo command. When I run the script, the script pauses, and the Ubuntu terminal asks me for sudo password. I have 3 lines with sudo command and I will have to enter the password every time. Is there a way I can run the script with sudo password without the system asking me for password?

This is my code:

import subprocess["sudo","ifconfig","ens33","down"])["sudo","ifconfig","ens33","hw","ether","00:11:22:33:44:55"])["sudo","ifconfig","ens33","up"])
Feb 6 in Cyber Security & Ethical Hacking by Jishan

1 answer to this question.

0 votes

When you run a command with sudo, by default, the system will ask for a password. To avoid this, you will have to change the python interpreter used by PyCharm from default to a custom interpreter. Don't worry, you don't have to build a new interpreter, you will just have to create an intermediate. The solution to solve this is as follows:

First, open a terminal and run this command:

$ sudo visudo -f /etc/sudoers.d/python

Then enter the following line in this file (replace <username><hostname> with the username and hostname of your system respectively):

<username> <hostname> = (root) NOPASSWD: <path/to/python>

Replace <path/to/python> with the path where Python is installed (usually /usr/bin/python) and save this file.

Now you will have to create a sudo shell script. Run the following command in the terminal:

$ nano

and enter the following lines in it:

sudo /usr/bin/python "$@"

Save and exit and change the permission of this file:

$ chmod +x

Now go to PyCharm, go to File > Settings > Project Interpreter. Click on the settings icon and click on the green plus icon to add interpreter. Browse to the directory where you have saved the and select it. Save changes and exit. Now you won't be asked for passwords.

answered Feb 6 by Omkar
• 68,480 points

Related Questions In Cyber Security & Ethical Hacking

0 votes
1 answer
+1 vote
0 answers

How can I encryption/decryption in Rijndael using python

I found this ,but does not ...READ MORE

Sep 28 in Cyber Security & Ethical Hacking by Ahmed
• 310 points
0 votes
1 answer

SIOCSIFFLAGS: Operation not permitted

You need sudo permissions to make changes to ...READ MORE

answered Feb 6 in Cyber Security & Ethical Hacking by Omkar
• 68,480 points
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 425 views
+1 vote
1 answer

Not able to use nmap in python.

nmap module doesn’t have PortScanner attribute. The ...READ MORE

answered Jan 28 in Cyber Security & Ethical Hacking by Omkar
• 68,480 points
0 votes
1 answer

How to find IP address of nodes in my network?

The IP address of the nodes connected ...READ MORE

answered Feb 9 in Cyber Security & Ethical Hacking by Omkar
• 68,480 points