执行sudo命令时QProcess错误

时间:2019-08-10 13:28:09

标签: c++ linux qt qprocess

QProcess不接受用户密码输入的写方法。

Qt5中的QProcess类模块在通过管道传递回显“密码”时接受sudo命令,但是当直接使用sudo执行Linux命令并使用QProcess写入功能提供密码时,该模块不起作用。

这是可行的:

import UIKit    
import PlaygroundSupport    

PlaygroundPage.current.needsIndefiniteExecution = true

var str = "Hello, playground"

struct category : Codable {
    let success: Bool
    let list: [List]
    let slide: [String]
}

// MARK: - List
struct List: Codable {
    let id, categoryName: String
    let subcategory: [Subcategory]
}

// MARK: - Subcategory
struct Subcategory: Codable {
    let subCategoryID: SubCategoryID
    let subCategoryName, categoryID: String
    let banner: String

    enum CodingKeys: String, CodingKey {
        case subCategoryID = "sub_category_id"
        case subCategoryName = "sub_category_name"
        case categoryID = "category_id"
        case banner
    }
}

enum SubCategoryID: Codable {
case integer(Int)
case string(String)

init(from decoder: Decoder) throws {
    let container = try decoder.singleValueContainer()
    if let x = try? container.decode(Int.self) {
        self = .integer(x)
        return
    }
    if let x = try? container.decode(String.self) {
        self = .string(x)
        return
    }
    throw DecodingError.typeMismatch(SubCategoryID.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for SubCategoryID"))
}

func encode(to encoder: Encoder) throws {
    var container = encoder.singleValueContainer()
    switch self {
    case .integer(let x):
        try container.encode(x)
    case .string(let x):
        try container.encode(x)
    }

    let session = URLSession(configuration: .default)
    var datatask : URLSessionDataTask?
    let url = "http://comus.in/co/webservice/getCategoryList.php"
    var items = [URLQueryItem]()
    var myURL = URLComponents(string: url)
    let param = ["vendorId":"32"]
    for (key,value) in param {
        items.append(URLQueryItem(name: key, value: value))
    }
    myURL?.queryItems = items
    let request =  URLRequest(url: (myURL?.url)!)

    datatask = session.dataTask(with: request, completionHandler: {data, response, error in
        if error == nil {
            let receivedData = try? JSONSerialization.jsonObject(with: data!, options: []) as! [String: Any]
            print(receivedData!)
            let listings = try? JSONDecoder().decode(List.self , from: data)
            if let listings = listings{

            }
        }

    })
    datatask?.resume()
}

但是,如果使用write方法提供用户密码,则QProcess无法正常工作。

    QProcess echo_proc, useradd_proc;
    echo_proc.setStandardOutputProcess(&useradd_proc);
    echo_proc.start("echo " + user_password);
    useradd_proc.start("sudo -S useradd -s /bin/bash -d /home/" + user_name + " -p  $1$y9q3lX76$SWpK59xObYQB.8rIUwWbj0" + " " + user_name);
    echo_proc.waitForFinished(5000);
    useradd_proc.waitForFinished(5000);

下一个问题场景的输出:

useradd进程无法完成...

QProcess:在进程(“ bash”)仍在运行时被破坏。

0 个答案:

没有答案