方式1
命令行直接拿到sudo 权限
sudo su
该命令行下,后面的指令不需要输入密码了
方式2
python脚本控制,亲测有效
import platform,os,logging
import subprocess,pexpect
log = logging.getLogger(__name__)
def sudo_exec(cmdline,passwd):
osname = platform.system()
if osname == 'Linux':
prompt = r'\[sudo\] password for %s: ' % os.environ['USER']
elif osname == 'Darwin':
prompt = 'Password:'
else:
assert False,osname
child = pexpect.spawn(cmdline)
idx = child.expect([prompt,pexpect.EOF],@H_301_47@3)
if idx == @H_301_47@0: # if prompted for the sudo password
log.debug('sudo password was asked.')
child.sendline(passwd)
child.expect(pexpect.EOF)
#return child.before
cmd_line = "sudo ip link set can0 up"
password= "nvidia"
sudo_exec(cmd_line,password)
原文链接:https://www.f2er.com/ubuntu/352140.html