execute shell command with sudo in c program - c

What is the best way to execute this command: "sudo cat /var/log/auth.log | grep Accepted" inside my C program ?
I tried to use:
sprintf(command_result,"sudo cat /var/log/auth.log | grep Accepted");
But it didn't work obviously.

You cannot execute the command with sprintf() you need system() atleast
fix:
sprintf(command_result, "sudo cat /var/log/auth.log | grep Accepted");
system(command_result);

you can execute the command with pipe
FILE *fp;
fp=popen(command_result,"r");
and then you can read the command output from the pipe fp like you read from files with fgets() or fread()...
BTW you can not execute sudo command if the password is required in the sudo command

Related

reading latest from sftp server

I have a requirement to download the latest file from sftp server.I have written the below code in shell script but am not able to download the file.
After retrieving the file am getting the below Error
Invalid command
Please help me how to download the file.
#!/bin/sh
HOST='xx.xx.xx.nxx'
USER='xx'
PASSWD='xx'
sftp $USER#$HOST <<EOF
cd /inbound
file=$(ls -ltr *.xml | tail -1 | awk '{print $NF}')
get $file
EOF
You are trying to run shell commands in sftp, but sftp is not a shell. The command ls happens to exist in sftp, but not $(), tail, or awk. To see this, just type sftp $USER#$HOST to open a sftp session and type help to get all of the available commands.
So what you need to do is to execute the shell commands using ssh to get the filename. So something like this:
file=$(ssh $USER#$HOST "ls -ltr /inbound/*.xml" | tail -1 | awk '{print $NF}')
This execute the command ls -ltr /inbound/*.xml remotely on the server. The output of that is then processed by your shell script locally. Or maybe more efficiently by doing the processing on the server:
file=$(ssh $USER#$HOST "ls -ltr /inbound/*.xml | tail -1 | awk '{print \$NF}'")
Now the shell variable file contains the name of the newest file. Then you can download that file with sftp as
sftp $USER#$HOST:$file .

How do I run a terminal command at the same time as executing a C program from terminal

I have a C program executable. I will run a terminal command at the same time as I run the program and wonder how I would do that?
./program | ps -l -u ${USER} | grep info
I put in my username for USER but I get:
Usage: ps [options]
Try 'ps --help ' or 'ps --help
' for additional help text.
For more details see ps(1).
Don't know what I do wrong?
answering your question as stated in header you want to run
./program & ps ...
You probably want to run your ./program in the background as you grep the output from ps:
./program & ps -l -u ${USER} | grep info
the & puts ./program in the background where as | is used to pipe the output of one command to another

Is it possible to redirect stdout to two places in C?

I've been stuck on this for a while now, is it possible to redirect stdout to two different places? I am writing my own shell for practice, and it can currently run commands like ps aux | wc -l or ps aux | wc -l > output.file. However, when I try to run ps aux > file.out | wc -l, the second command does not receive the input from the first.
In the last example, the first command would be run in a child process that would output to one end of the pipe. The logic is similar to what follows:
close(stdout);
dup2(fd[1], STDOUT_FILENO);
//If a file output is also found
filewriter = open(...);
dup2(filewriter, STDOUT_FILENO);
//Execute the command
Normal UNIX shells don't work with that syntax either. UNIX (and some other OSs) provides the tee[1] command to send output to a file and also stdout.
Example:
ps aux | tee file.out | wc -l
[1] See http://en.wikipedia.org/wiki/Tee_(command)
The tee command does just that in UNIX. To see how to do it in straight C, why not look at tee's source code?

Copy output of the program to the screen and also to the file

I have written client-server application in C. I run the server in linux.
I need to do simple logs of what server do. I print it to the screen.
Please how can I copy output of the screen also to the file.
Thx
You can use the tee command as:
./server_program | tee server.log
You can use the tee command:
$ ./program | tee log.txt

Watching new entries on Linux Syslog from a C program

I want to write a program that monitors syslog and performs an action when PPP authentication fails.
I think "tail -f /var/log/syslog" could help, but I'm not sure how to use it... probably using pipes?
I have found something similar written in bash, but I'm not sure how to implement it in C.
This is the bash method:
First create a named pipe using mkfifo:
$ mkfifo -p /home/mezgani/syslog.pipe
Make syslog.conf to points to this file:
*.info |/home/mezgani/syslog.pipe
Restart syslog:
$ sudo pkill -HUP syslogd
Create processing script that read the pipe
$ cat > foo
#!/bin/bash
cat /home/mezgani/syslog.pipe | while read input
do
# some stuff
echo ${input}
# ….
done
Finally I could found the solution!!
The solution was using named pipes!
First, I need to create a named pipe:
mkfifo /pipe
Then, I feed the pipe with the log info:
tail -f /var/log/syslog > /pipe
And then, I read the pipe from the C program using OPEN
int pipefd;
pipefd = open("/tmp/myFIFO", O_WRONLY);
Try to use inotify function. Using it you can monitor if a file or directory has changed.

Resources