ubuntu--免密码执行脚本

前端之家收集整理的这篇文章主要介绍了ubuntu--免密码执行脚本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

方式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://stackoverflow.com/a/26096816/5834756

原文链接:https://www.f2er.com/ubuntu/352140.html

猜你在找的Ubuntu相关文章