LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-20-2012, 07:21 AM   #16
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,507

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331

Quote:
Originally Posted by akshay_satish View Post
Code:
       ret = posix_spawnp(
            &pid,
            argv[0], 
            NULL, NULL,
            argv,
            NULL);                      //After this i perform a waitpid();
The code above seems to suggest that you have having your program, which name is stored in argv[0], launch itself again.

Did you define your own argv array to contain a reference to the scp command and associated arguments?
 
Old 11-20-2012, 07:25 AM   #17
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Original Poster
Rep: Reputation: Disabled
yes, i used my own argv array which is why I thought I could use strerror() and thought posix_spawnp will give me the correct status:
SCP Return Codes
0
Operation was successful
1
General error in file copy
2
Destination is not directory, but it should be
3
Maximum symlink level exceeded
4
Connecting to host failed.
5
Connection broken
6
File does not exist
7
No permission to access file.
8
General error in sftp protocol
9
File transfer protocol mismatch
10
No file matches a given criteria
65
Host not allowed to connect
66
General error in ssh protocol
67
Key exchange failed
68
Reserved
69
MAC error
70
Compression error
71
Service not available
72
Protocol version not supported
73
Host key not verifiable
74
Connection failed
75
Disconnected by application
76
Too many connections
77
Authentication cancelled by user
78
No more authentication methods available
79
Invalid user name

Code:
  argv[0] = "scp";
   argv[1] = "-q";
    argv[2] = "-o StrictHostKeychecking=no";
    argv[3] = "some_path";
    argv[4] = dst_path;
    argv[5] = NULL;
 
Old 11-20-2012, 08:14 AM   #18
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,507

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Again, according to the man-page of scp, 0 is returned on success, and a number > 0 is returned on the occurrence of an error.

I do not know where you obtained the list of status values; it does not appear on the man-page for 'scp', and I have taken the effort to check on Linux, HP-UX, and a Mac. Are you on a different OS?
 
Old 11-20-2012, 08:17 AM   #19
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dwhitney67 View Post
Again, according to the man-page of scp, 0 is returned on success, and a number > 0 is returned on the occurrence of an error.

I do not know where you obtained the list of status values; it does not appear on the man-page for 'scp', and I have taken the effort to check on Linux, HP-UX, and a Mac. Are you on a different OS?
whoops i am sorry, actually i did the same exercise as you performed and couldnt get the error status. I am using linux..i found this on google.. sorry i should have mentioned the source, my bad!! So with my implementation , you dont recommend using strerror??
 
Old 11-20-2012, 08:39 AM   #20
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,507

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Quote:
Originally Posted by akshay_satish View Post
whoops i am sorry, actually i did the same exercise as you performed and couldnt get the error status. I am using linux..i found this on google.. sorry i should have mentioned the source, my bad!! So with my implementation , you dont recommend using strerror??
I recommend using popen(); use it to read any output that 'scp' may return. For example:
Code:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>

int main(int argc, char** argv)
{
    const char* scp_cmd = "/usr/bin/scp";
    const char* scp_src = (argc > 1 ? argv[1] : "/etc/hosts");
    const char* scp_dst = "localhost:";

    char cmd[1024] = {0};

    snprintf(cmd, sizeof(cmd), "%s -q -o StrictHostKeychecking=no %s %s 2>&1", scp_cmd, scp_src, scp_dst);

    FILE* cmd_fp = popen(cmd, "r");

    if (cmd_fp)
    {
        char output[256] = {0};

        fgets(output, sizeof(output), cmd_fp);

        if (strlen(output) == 0)
        {
           fprintf(stdout, "Transfer completed successfully.\n");
        }
        else
        {
            fprintf(stderr, "Error: %s", output);
        }

        pclose(cmd_fp);
    }
    else
    {
        fprintf(stderr, "Command failed; reason is %s [%d]\n", strerror(errno), errno);
        return errno;
    }

    return 0;
}
 
Old 11-20-2012, 09:05 AM   #21
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dwhitney67 View Post
I recommend using popen(); use it to read any output that 'scp' may return. For example:
Code:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>

int main(int argc, char** argv)
{
    const char* scp_cmd = "/usr/bin/scp";
    const char* scp_src = (argc > 1 ? argv[1] : "/etc/hosts");
    const char* scp_dst = "localhost:";

    char cmd[1024] = {0};

    snprintf(cmd, sizeof(cmd), "%s -q -o StrictHostKeychecking=no %s %s 2>&1", scp_cmd, scp_src, scp_dst);

    FILE* cmd_fp = popen(cmd, "r");

    if (cmd_fp)
    {
        char output[256] = {0};

        fgets(output, sizeof(output), cmd_fp);

        if (strlen(output) == 0)
        {
           fprintf(stdout, "Transfer completed successfully.\n");
        }
        else
        {
            fprintf(stderr, "Error: %s", output);
        }

        pclose(cmd_fp);
    }
    else
    {
        fprintf(stderr, "Command failed; reason is %s [%d]\n", strerror(errno), errno);
        return errno;
    }

    return 0;
}
this is great..thanks a lot!!.... Appreciated.

Last edited by akshay_satish; 11-20-2012 at 12:48 PM.
 
Old 11-20-2012, 12:56 PM   #22
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by akshay_satish View Post
this is great..thanks a lot!!.... Appreciated.
I understand why my code doesnt work clearly after running your example. Yours works like a charm mate... thanks!! but just a thought:
I am giving a wrong scp command on purpose (this is how it would be stored internally in "cmd" per your code):
Code:
scp -p  -o StrictHostKeychecking=no aks@something.com:/test/tmp /tmp
This is actually giving me the print saying Transfer completed successfully!! I am wondering how..
coudl you pls explain the below 2 lines pls:
Code:
fgets(output, sizeof(output), cmd_fp);

        if (strlen(output) == 0)
So this is code doesnt work for me for an invalid scp syntax. Just a little new to this concept. would really appreciate your help.. m 80% done

Last edited by akshay_satish; 11-20-2012 at 12:57 PM.
 
Old 11-20-2012, 01:46 PM   #23
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,507

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Quote:
Originally Posted by akshay_satish View Post
I understand why my code doesnt work clearly after running your example. Yours works like a charm mate... thanks!! but just a thought:
I am giving a wrong scp command on purpose (this is how it would be stored internally in "cmd" per your code):
Code:
scp -p  -o StrictHostKeychecking=no aks@something.com:/test/tmp /tmp
This is actually giving me the print saying Transfer completed successfully!! I am wondering how..
This is the error I get when running the command on my system:
Code:
/usr/bin/scp -p -o StrictHostKeychecking=no aks@something.com:/test/tmp /tmp
ssh: Could not resolve hostname something.com: Name or service not known
If I modify the code I provided earlier to adjust the scp_src and the scp_dst, I get the following error:
Code:
Error: ssh: Could not resolve hostname something.com: Name or service not known
Quote:
Originally Posted by akshay_satish View Post
coudl you pls explain the below 2 lines pls:
Code:
fgets(output, sizeof(output), cmd_fp);

        if (strlen(output) == 0)
fgets() reads from the stream cmd_fp, up to a certain amount of characters. If there is nothing to read, then it is assumed the command (that is, scp) succeeded. This of course only works if scp is passed the -q option.

P.S. Here's the modified code I tested with:
Code:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>

int main(int argc, char** argv)
{
    const char* scp_cmd = "/usr/bin/scp";
/*
    const char* scp_src = (argc > 1 ? argv[1] : "/etc/hosts");
    const char* scp_dst = "localhost:";
*/
    const char* scp_src = "aks@something.com:/test/tmp";
    const char* scp_dst = "/tmp";

    char cmd[1024] = {0};

    snprintf(cmd, sizeof(cmd), "%s -p -q -o StrictHostKeychecking=no %s %s 2>&1", scp_cmd, scp_src, scp_dst);

    fprintf(stdout, "Executing the following command:\n%s\n", cmd);

    FILE* cmd_fp = popen(cmd, "r");

    if (cmd_fp)
    {
        char output[256] = {0};

        fgets(output, sizeof(output), cmd_fp);

        if (strlen(output) == 0)
        {
           fprintf(stdout, "Transfer completed successfully.\n");
        }
        else
        {
            fprintf(stderr, "Error: %s", output);
        }

        pclose(cmd_fp);
    }
    else
    {
        fprintf(stderr, "Command failed; reason is %s [%d]\n", strerror(errno), errno);
        return errno;
    }

    return 0;
}
-----------

EDIT: I'm wagering that you forgot to place this at the end of your command buffer in your code: "2>&1". This directs standard-error to standard-out, thus enabling the fgets() to do its job.

Last edited by dwhitney67; 11-20-2012 at 01:50 PM.
 
Old 11-20-2012, 07:26 PM   #24
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Original Poster
Rep: Reputation: Disabled
Hi dwhitney, i see you have replied but I am able to view only one page on this thread.. has this been lost or something?
 
Old 11-21-2012, 09:23 AM   #25
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,507

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Quote:
Originally Posted by akshay_satish View Post
Hi dwhitney, i see you have replied but I am able to view only one page on this thread.. has this been lost or something?
It has been restored. Apparently LQ had DB problems yesterday.
 
Old 11-23-2012, 04:13 AM   #26
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dwhitney67 View Post
It has been restored. Apparently LQ had DB problems yesterday.
thanks a lot.. i've acquired a lot of knowledge from u.. i hope u dont mind some of my trivial doubts.. thanks again DWhitney!!cheers.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script to get name of file, delete original file, rename blank file chrisgti Linux - General 11 09-15-2012 02:49 AM
Mencoder adds the extension of the original file to the output file ex: file.flv.mp4 linuxlicious Linux - General 2 04-17-2012 02:22 PM
Mencoder adds the extension of the original file to the output file ex: file.flv.mp4 linuxlicious Linux - General 1 04-15-2012 04:07 AM
How to play a media file/ video file/mp3 file recorded in harddisk/cd-rom arindam Linux - Newbie 2 09-05-2003 10:31 AM


All times are GMT -5. The time now is 04:44 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration