Handling error cases with scp and sshpass - c

I am trying to implement command line interface command for file transfer and that will call internally
sshpass -p "password" scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -r user#remote-machine:/home/QA.txt /home/faadmin/
Here error handling is not happening properly , when I am running this command using system().if in case route not there for file transfer ,or file not found errors some time they blocking the execution of CLI command.so I have check return values after executing the above Linux command not showing other than 0 and 1.how can I get other return values?

You should use popen() because system() return value tell you if the command has been executed or not. E.G.:
#include <stdio.h>
void main(void)
{
FILE *output = NULL;
char text[2048];
char cmd[256];
sprintf(cmd, "%s", "sshpass -p \"password\" scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -r user#remote-machine:/home/QA.txt /home/faadmin/");
output = popen(cmd, "r");
while(fgets(text, 1024, output) != NULL)
printf("%s", text);
pclose(output);
}

Related

How to use a created Linux Kernel character driver from the commandline

I've been following the tutorial on Linux Kernel programming over here: http://www.tldp.org/LDP/lkmpg/2.6/html/index.html
I've gotten to the section that is dedicated to "character device drivers" and while I've gotten it to compile, it will not function on the described case:
"Called when a process writes to dev file: echo "hi" > /dev/chardev"
I've tried several Linux console commands such as:
echo "hi" > sudo /dev/chardev/
and
sudo sh -c 'printf "hi" > sudo /dev/chardev/'
I'm running my code on a Raspberry Pi 3 B+
When I run the first command I will get nothing in return, and nothing is added to /var/logs/messages
When I run the second command I get:
sh:printf: I/O error
Full code over at: http://www.tldp.org/LDP/lkmpg/2.6/html/x569.html
I've modified the code with my snippet below.
/*
* Called when a process writes to dev file: echo "hi" > /dev/chardev
*/
static ssize_t
device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
{
printk(KERN_INFO "%s\n", buff);
return -EINVAL;
}
What I'm expecting to happen is when I use echo "hi" > sudo /dev/chardev that in my /var/logs/messages a line will appear that simply says "hi".
echo "hi" > /dev/chardev
This is ok.
echo "hi" > sudo /dev/chardev/
This is invalid. This will echo hi /dev/chardev/ and write that to file named sudo. And don't /dev/chardev/, it't not a directory, it's a file, it's /dev/chardev (without the / on the end).
sudo sh -c 'printf "hi" > sudo /dev/chardev/'
Same error as above.
If you want to append to a file using sudo, use tee, as in echo hi | sudo tee /dev/chardev. Or if you have to sudo sh -c 'echo "hi" > /dev/chardev'.

How to terminate the grep pipe after first match

Im trying to execute the following code in my C program but it seems that the -m1 switch is unable to terminate the pipe. As a result the program keeps reading the entire 16 GB file. Any help would be appreciable.
char *cmd=NULL;
cmd = malloc (200);
if (cmd != NULL) {
strcpy (cmd, "sudo hexdump -v -e '");
strcat(cmd,"\"");
strcat(cmd,"%010_ad |");
strcat(cmd,"\"");
strcat(cmd," 100000/1 ");
strcat(cmd,"\"");
strcat(cmd,"%_p");
strcat(cmd,"\"\"");
strcat(cmd,"|\\n");
strcat(cmd,"\"");
strcat(cmd,"' -s 2437150492 /run/SDdownload.dd | grep -m 1 -E -o ");
strcat(cmd,"\"");
strcat(cmd,"single_install Secure cloud storage and file sharing");
strcat(cmd,"\"");
strcat(cmd,">./files/Sync.com/installation.txt");
printf("Command -> %s\n",cmd);
system(cmd);

run xterm and show file content using c

I want to open terminal and cat a file (show its content) using c;
i got error from code below:
char * com = "xterm cat /home/user/Desktop/file.c";
system(com);
or even:
char * com = "/usr/bin/xterm /bin/cat /home/user/Desktop/file.c";
system(com);
thanks for any help;
You could change your command to:
xterm -e less file.c

Using commands of .exe files in c language

a few months back I wrote a batch (windows batch file .bat) code that fetches some .exe files and use their commands to do different things. e.g encoding audio, video etc...
Now I want the same thing but want to do it in C language.
set var = "Video"ffmpeg -i %var%.mkv -f wav -| neroAacEnc -ignorelength -lc -q 0.4 -if - -of %var%-Audio.aac
This code works fine in windows batch file (given that I have specified files in the same folder.)
But now I want to do this via C language. Well I know using
system("ffmpeg -i Video.mkv -f wav -| neroAacEnc -ignorelength -lc -q 0.4 -if - -of Video-Audio.aac");
will work for C language, but the drawback is I can't use variable while exploiting ffmpeg's and neroAacEnc's commands / parameters.
So is there anyway to get around it?
(Also, I'm gonna use other .exe files as well like x264.exe and mkvmerge.exe, so i'd appreciate it if someone could tell me how to use external .exe files parameters freely in C language.)
char *var="Video"
char cmd[512];
sprintf(cmd, "ffmpeg -i Video.mkv -f wav -| neroAacEnc -ignorelength -lc -q 0.4 -if - -of %s-Audio.aac", var);
system(cmd);
you can make more than 1 variable
char *var="Video"
char *app="ffmpeg";
char cmd[512];
sprintf(cmd, "%s -i Video.mkv -f wav -| neroAacEnc -ignorelength -lc -q 0.4 -if - -of %s-Audio.aac", app, var);
system(cmd);
Try this:
char var[50];
char command[256];
sprintf(var,"%s","Video");
sprintf(command,"ffmpeg -i %s.mkv -f wav -| neroAacEnc -ignorelength -lc -q 0.4 -if - -of %s-Audio.aac",var,var);
system(command);
Use snprintf. It is safer alternative to sprintf.
/* Construct command */
#define MAX_CMD_LEN 32
char command[MAX_CMD_LEN];
const char * param1 = "abc";
int param2 = 6;
int len = snprintf(command, MAX_CMD_LEN, "ffmpeg %s %d", param1, param2);
/* Run command if all went well */
if(len > 0 && len < MAX_CMD_LEN) {
system(command); /* Runs 'ffmpeg abc 6' */
}
else {
/* Command didn't fit our buffer */
}

Plink passing arguments

plink user#10.220.60.xx -t '/home/user/test/testpgm'
I'm able to run the below program which resides on a Linux machine from a windows machine using the above plink cmd.
#include<stdio.h>
int main(int argc,char *argv[])
{
int i;
char buf[30];
printf("Test Pgm \n");
printf("No of Arguments=%d\n",argc);
printf("Enter a string:");
fflush(stdout);
gets(buf);
printf("Input str:%s \n",buf);
return 0;
}
gcc test.c -o testpgm
Question: How to pass command line arguments to this function?
I tried
plink user#10.220.60.xx -t '/home/user/test/testpgm arg1'
bash: /home/user/test/testpgm arg1: No such file or directory
The shell treats strings inside quotes as a single word, which means that plink tries to execute the program /home/user/test/testpgm arg1. Obviously this won't work.
What you have to do is very simple: Skip the quotes!
$ plink user#10.220.60.xx -t /home/user/test/testpgm arg1
I tried
plink user#10.220.60.xx /home/user/test/testpgm arg1
Works fine.

Resources