-4

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

int main() {
    string ip;
    cout << "IP to scan: ";
    cin >> ip;

    std::system(" nmap .... ")

    return 0;
}

so basicly I want the string ip to be used in an gnome ternimal so I can do for example and nmap scan of the ip that the user typed

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Gilfoyle
  • 1
  • 3
  • 1
    its unclear what is the question. What happens when you just do what you want to do? Doesnt it work? – 463035818_is_not_an_ai Aug 20 '18 at 10:26
  • 1
    _run a command with the input in another terminal_ Then you have to call `gterm` in `system()`. You can provide the command `nmap` as command line argument e.g. `system("gterm -e \"nmap .... \"");` or `system("gterm -x nmap .... ")`. Found this in doc.: [gnome-terminal - Options](http://manpages.ubuntu.com/manpages/cosmic/man1/gnome-terminal.1.html#options) – Scheff's Cat Aug 20 '18 at 10:30

1 Answers1

1

That can be easily done using string formatting:

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    
}

To run that command in a different terminal window, you have to call the terminal program with system(), as scheff mentioned in their comment

os << "gterm -e \"nmap " << ip "\"";
std::system(os.str().c_str());
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190