方式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],3)
if idx == 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)