0

I have a python script that I made to update and upgrade my Linux system, it is written in python but after I tell it to update it prompts me to enter a hidden password and I was wondering if there is a way to get Python to enter the hidden password for me. This is my code its simple and works I just need to know if there is a way to enter hidden passwords with python.

import os,sys
os.system("sudo apt-get update && sudo apt-get upgrade")
Forge
  • 23
  • 9
  • 3
    Why bother using Python? You can just run a bash script with the `apt-get` commands. – David Cain Mar 14 '14 at 14:03
  • 1
    stdlib: `getpass`. I'm pretty sure you didn't search anything... – michaelmeyer Mar 14 '14 at 14:03
  • Are you asking if you can get python to interrogate you for the password (`getpass`), or if you can get python to communicate a password to `sudo`? – Emmet Mar 14 '14 at 14:08
  • Doukremt I meant in the script I want to to automatically enter the password when it prompts me for one but since the password is hidden by default on Linux I have struggled with just a simple print("password here") – Forge Mar 14 '14 at 14:10
  • Emmet I am asking if I can get a Python to communicate a password to sudo. – Forge Mar 14 '14 at 14:10
  • @Forge No, that’s not what your question is saying. If you want to communicate with another process, check out the `subprocess` module. – poke Mar 14 '14 at 14:12
  • @Forge: then run your script with root privileges. – michaelmeyer Mar 14 '14 at 14:13
  • 1
    Don't automate password entry! If you want `sudo` to run without a password prompt, edit your `/etc/sudoers` file with `visudo` to grant your script permission to execute `apt-get update` and `apt-get upgrade`. – John Kugelman Mar 14 '14 at 14:14
  • Thanks you guys this all helped so much! – Forge Mar 14 '14 at 14:16

2 Answers2

1

You can use the pexpect module. Take a look at this tutorial.

bosnjak
  • 8,424
  • 2
  • 21
  • 47
0

There is a std function in python for doing this. See getpass

import getpass
password = getpass.getpass()
apparat
  • 1,930
  • 2
  • 21
  • 34
  • Why did this get downvoted? – ditkin Mar 14 '14 at 14:13
  • Don't understand the downvote either. The author says "... I just need to know if there is a way to enter hidden passwords with python". That's what getpass does. – apparat Mar 14 '14 at 14:15