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;
    }
  • 相关阅读:
    ZEat
    BingWallpaper
    ZBreak
    C语言块内变量回收问题
    Android 隐藏RadoiButton左边按钮
    php文件管理与基础功能的实现
    Ajax写分页查询(实现不刷新页面)
    jquery写日期选择器
    ajax三级联动下拉菜单
    ajax bookstrap美化网页,并实现页面的加载,删除与查看详情
  • 原文地址:https://www.cnblogs.com/wanzaiyimeng/p/5895670.html
Copyright © 2011-2022 走看看