zoukankan      html  css  js  c++  java
  • How to fix 'sudo: no tty present and no askpass program'以及硬盘序列号的读写

    在调用system命令读写硬盘序列号的过程中遇到问题,报错如下:

    sudo: no tty present and no askpass program

    发现此问题是由于帐号并没有开启免密码导致的 。

    查找资料,解决方法如下:

    方法一:

    sudo isudo

    然后在最末尾添加:

    username ALL = NOPASSWD: /fullpath/to/command,/fullpath/to/othercommand
    
    eg:
    john ALL = NOPASSWD: /sbin/poweroff,/sbin/start,sbin/stop

    会允许用户John在进行poweroff,start,stop的时候不需要密码。这里我使用了最简单的方式,如下:

    john ALL = NOPASSWD: ALL

    添加完成以后就可以无须密码进行sudo操作了。(亲测可用

    方法二:

    1    打开sudoers

    vi /etc/sudoers

    2    添加免密码

    john ALL = NOPASSWD: ALL

    读取硬盘序列号的方法:

    if(system("sudo hdparm -I /dev/sda | grep Serial >> tmpGenuine.txt")>=0)
    {
            QFile *tmpFile = new QFile("tmpGenuine.txt");//临时存放文件
            if(tmpFile->open(QIODevice::ReadOnly|QIODevice::Text))
            {
                while(!tmpFile->atEnd())
                {
                    QString tmpLine;
                    tmpLine = tmpFile->readLine();
                    tmpLine.indexOf(rx2);
                    genuineNum += rx2.cap(1);
                }
                qDebug()<<"genuineNum:"<<genuineNum;
            }
            tmpFile->close();
    }
    else
    {
           qDebug()<<"can not get the Serial Num";
           return 0;
    }
  • 相关阅读:
    .Net中World转PDF
    asp.net开启多线程异步处理
    win8.1 安装kb2999226 一直提示 搜索更新
    MVC实例应用模式
    MVC模式
    23种设计模式
    Android 的Android Device Monitor 打不开出现 A error has occured
    xxx系统的可用性和易用性分析
    淘宝网的质量属性分析
    《架构漫谈》读后感
  • 原文地址:https://www.cnblogs.com/wanzaiyimeng/p/5895670.html
Copyright © 2011-2022 走看看