rendered paste bodyThe C program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/signal.h>
void usage_and_die(int ret){
printf("usage: spawner program parameters\n");
exit(1);
}
char *get_real_name(char *str){
char *PATH, *path, *subtoken;
int fd, i=0, path_len;
if((fd=open(str, O_RDONLY)) != -1){
path=calloc(2+strlen(str), sizeof(char));
sprintf(path, "./%s", str);
close(fd);
return path;
}
PATH=getenv("PATH");
path_len=strlen(PATH);
if(PATH==NULL)
return NULL;
else {
while((subtoken = strtok(PATH+i, ":")) != NULL && i < path_len){
path=calloc(strlen(str)+1+strlen(subtoken), sizeof(char));
sprintf(path, "%s/%s", subtoken, str);
if((fd=open(path, O_RDONLY)) != -1){
close(fd);
return path;
}
else {
free(path);
i += strlen(subtoken) + 1;
}
}
}
return NULL;
}
int main(int argc, char **argv){
int status;
pid_t pid;
char *real_file;
if(argc<2)
usage_and_die(1);
if(!strcmp(argv[0], argv[1])){
printf("\nWhat? Trying to run myself inside of myself? Come on, I know you're better than this\n\n");
exit(1);
}
real_file=get_real_name(argv[1]);
if((pid = fork()) != 0) {
sleep(2);
waitpid(pid, &status, 0);
while (killpg(pid, 0) != -1)
usleep(0.5);
printf("Script %s returned: %s\n", real_file, WTERMSIG(status));
} else {
if(setpgid(getpid(), getpid()) == -1){
perror("setpgid");
exit(1);
}
if(real_file != NULL){
if(execv(real_file, argv+1) == -1){
perror("execv");
exit(1);
}
}
else {
printf("-spawner: %s: Command not found\n", argv[1]);
exit(1);
}
}
return 0;
}
#!/usr/bin/env bash
function pinging(){
SUBNET=${1}
LAST=${2}
PING="$(which ping) -c 1 -W 1"
${PING} ${SUBNET}.${LAST} > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo -e "${SUBNET}.${LAST} is up"
fi
}
if [ -z ${2} ]; then
for((x=1;x<255;x++)); do
${0} ${1} ${x} &
done
else
pinging ${1} ${2}
fi