1

How to comfirm automatically when execute a command with python script in centos?

For example,
there is a command below,it has a confirmation step:

[root@vagrant-prod ~]# php artisan key:generate
**************************************
*     Application In Production!     *
**************************************

 Do you really wish to run this command? (yes/no) [no]:
 > yes

Now I want to execute above command via a python script:
auto.py

#!/usr/bin/python3
import os


def regenerate_key():
    os.system('cd /var/www/laravel_blog && php artisan key:generate')

if __name__ == "__main__":
    regenerate_key()

Executing above auto.py

[root@vagrant-prod ~]# python3 auto.py

It also need to input yes manually,I want to comfirm it with above python script,how to do it?

update:

[root@vagrant-prod laravel_blog]# yes yes | php artisan key:generate
**************************************
*     Application In Production!     *
**************************************

Command Cancelled!

One yes or two yess have the same result.

zwl1619
  • 4,002
  • 14
  • 54
  • 110

1 Answers1

1

You can use the yes command like so:

#!/usr/bin/python3
import os


def regenerate_key():
    os.system('cd /var/www/laravel_blog && yes yes | php artisan key:generate')

if __name__ == "__main__":
    regenerate_key()

The command yes yes just echos "yes" to the terminal for the duration of the other command. You can confirm this behavior by just typing yes yes into a terminal. Remember that Ctrl+C stops command execution. So it will enter yes to the prompt and you're gold!

Steampunkery
  • 3,839
  • 2
  • 19
  • 28