使用 (cin) 用户输入将其粘贴到 std::system 中,并在另一个终端中运行带有输入的命令

Use the (cin) user input to paste it in std::system and run a command with the input in another terminal

本文关键字:输入 另一个 终端 运行 命令 行带 system cin 用户 使用 std      更新时间:2023-10-16

使用 (cin( 用户输入将其粘贴到 std::system 中,并在另一个终端中使用输入运行命令

int main() {
string ip;
cout << "IP to scan: ";
cin >> ip;
std::system(" nmap .... ")
return 0;
}

所以基本上我希望字符串 ip 在 gnome ternimal 中使用,这样我就可以对用户键入的 ip 进行例如 nmap 扫描

这可以使用字符串格式轻松完成:

int main() // ; << Note this is wrong!
{
string ip;
cout << "IP to scan: ";
cin >> ip;
std::ostringstream os;
os << "nmap " << ip;
std::system(os.str().c_str());
// return 0; isn't necessary    
}

要在不同的终端窗口中运行该命令,您必须使用system()调用终端程序,正如 scheff 在他们的评论中提到的那样

os << "gterm -e "nmap " << ip """;
std::system(os.str().c_str());