Mark Shoebeck
Mark Shoebeck

Reputation: 17

Need help on simple C command line argument

I don't really know how to explain this but here's my problem:

I am trying to make my program accept command line arguments and then run a program via. the Linux command line (CentOS 6).

Heres the main function:

int main(int argc, char *argv[])

I am trying to run a Linux program, here's the code:

system("nmap -sT -p 19 1.1.1.* -oG - | grep 19/open > temp");

I want to replace '1.1.1.*' with the first argument I input into my C program, Ex:

system("nmap -sT -p 19 ", (argv[1]) "-oG - | grep 19/open > temp");

I have tried multiple ways and none seemed to work.

To sum it up, i'm trying to take the first argument I input into my program and use it in replace of the '1.1.1.*' in the system function. I have no idea on how to do this, I'm new to C programming. Thank you all replies are appreciated.

Upvotes: 1

Views: 203

Answers (2)

pramod
pramod

Reputation: 2318

Just use the following syntax to accept command line arguments in Linux. ./program arg1 arg2

Upvotes: 0

Shafik Yaghmour
Shafik Yaghmour

Reputation: 158599

snprintf is the safest way to do this, this is a simple example without any checking of argc etc...:

#include <string.h> 
#include <stdio.h>

int main(int argc, char *argv[]) 
{
    char buf[200] ;
    char str1[] = "nmap -sT -p 19  ";
    char str2[] = " -oG - | grep 19/open > temp";

    snprintf(buf, 200, "%s%s%s", str1, argv[1], str2);
    printf( "%s\n", buf ) ;;
}

Upvotes: 1

Related Questions