B
B
Batignolles2014-03-07 23:53:23
linux
Batignolles, 2014-03-07 23:53:23

How to make the program automatically send ip to nmap?

Good day) I am writing a program in C, using the system () command I send commands to the command line. It is known that such a command will display my IP:

ifconfig | grep inet | grep -v inet6 | grep -v 127.0.0.1 | cut -d: -f2 | awk '{printf $1}'


Next, I need to execute the command nmap -sP X/24- where instead of X - the IP obtained above.
Question: how to make the program itself substitute it into the nmap -sP X/24 command after finding the IP address?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
B
BALLIA_HATALLIA, 2014-03-08
@Batignolles

#include <stdlib.h>


int main()
{
   system("nmap -sP `ifconfig eth0 | grep inet | grep -v inet6 | grep -v 127.0.0.1 | cut -d: -f2 | awk '{printf $1}'`/24");
   return 0;
}

The bash request differs from yours by specifying an interface for ifconfig.
[email protected] bash_c # cat test3.c
#include <stdlib.h>

int main()
{
   system("nmap -sP `ifconfig eth0 | grep inet | grep -v inet6 | grep -v 127.0.0.1 | cut -d: -f2 | awk '{printf $1}'`/24");
   return 0;
}

[email protected] bash_c # gcc -o atest1 test3.c
[email protected] bash_c # ./atest1

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2014-03-08 02:21 MSK
Host 10.10.8.1 appears to be up.
MAC Address: 00:1B:21:17:0F:41 (Unknown)
Host 10.10.8.2 appears to be up.
MAC Address: 00:1B:21:17:0F:DE (Unknown)
..............................................

I
Ivan Starkov, 2014-03-07
@icelaba

example
X=1
echo $X
do it by analogy
or in more detail if you don't know how to put the output of the command into the variable
OUTPUT=$(ls -1)
echo "$OUTPUT"

B
BALLIA_HATALLIA, 2014-03-08
@BALLIA_HATALLIA

PS As a rule, it is better to wrap layers in ascending order (C in Bash and not vice versa).
C The code generates IP interfaces at the output.

[email protected] bash_c # cat test5.c
#include <stdio.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>

int main (int argc, const char * argv[]) {
    struct ifaddrs * ifAddrStruct=NULL;
    struct ifaddrs * ifa=NULL;
    void * tmpAddrPtr=NULL;

    getifaddrs(&ifAddrStruct);

    for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
        if (ifa ->ifa_addr->sa_family==AF_INET) { // check it is IP4
            // is a valid IP4 Address
            tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
            char addressBuffer[INET_ADDRSTRLEN];
            inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
            printf("%s_IPv4 %s\n", ifa->ifa_name, addressBuffer);
        } else if (ifa->ifa_addr->sa_family==AF_INET6) { // check it is IP6
            // is a valid IP6 Address
            tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
            char addressBuffer[INET6_ADDRSTRLEN];
            inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
            printf("%s_IPv6 %s\n", ifa->ifa_name, addressBuffer);
        }
    }
    if (ifAddrStruct!=NULL) freeifaddrs(ifAddrStruct);
    return 0;
}

Compiling:
Testing:
[email protected] bash_c # ./ztest1
lo_IPv4 127.0.0.1
eth0_IPv4 10.10.8.60
eth1_IPv4 10.10.4.60
lo_IPv6 ::1
eth0_IPv6 fe80::20c:29ff:feb2:da9

We wrap the C binary in bash indicating the desired interface:
[email protected] bash_c # cat zbash.sh
#!/bin/bash
nmap -sP `./ztest1|grep eth0_IPv4|awk {'print $2'}`/24

We get the end result identical to the answer above:
[email protected] bash_c # ./zbash.sh

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2014-03-08 03:06 MSK
Host 10.10.8.1 appears to be up.
MAC Address: 00:1B:21:17:0F:41 (Unknown)
Host 10.10.8.2 appears to be up.
MAC Address: 00:1B:21:17:0F:DE (Unknown)
..............................................

A
ArturProsto, 2014-03-08
@ArturProsto

not strong in C. but try to write the result to a file. after using C, read this ip into a variable and delete the file after itself.

@
@ntip, 2014-03-08
_

miner weapon x=)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question