简体   繁体   中英

python3 sh library: Getting no output and no error

I'm trying to use the sh library for Python3 to run a command as another user. But I'm not getting any output and the command ran appears correct. Anybody know how to get this to work?

Edit: It does return an error code of 1.

Code:

passwd = input('Enter pass:')
passwd = r'{0}\n'.format(passwd)

my_user = sh.sudo.bake('-S', '-u', 'www-data', _in=passwd)
try:
    a = my_user.ls('/home/dev/xxxxxx/html/')
    print(a)
except Exception as e:
    print('\n\n{0}'.format(e))

Output:

dev@node:~/scripts$ ./backup.py
Enter pass:xxxxxxx

  RAN: /usr/bin/sudo -S -u www-data ls /home/dev/xxxxxxx/html/

  STDOUT:

  STDERR:
[sudo] password for dev:

dev@node:~/scripts$

Manually Ran:

dev@node:~/scripts$ /usr/bin/sudo -S -u www-data ls /home/dev/xxxxxxx/html/
[sudo] password for dev:
index.php        wp-admin              wp-config-sample.php  wp-links-opml.php  wp-settings.php
license.txt      wp-blog-header.php    wp-content            wp-load.php        wp-signup.php
readme.html      wp-comments-post.php  wp-cron.php           wp-login.php       wp-trackback.php
wp-activate.php  wp-config.php         wp-includes           wp-mail.php        xmlrpc.php
dev@node:~/scripts$

I ended up not using sh to change user. Instead I adjusted permissions or groups of the system and ran sudo -u $user ./script.py. Where the $user has rights to the files used in the script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM