0

I am trying to establish SSH connection to a remote server to execute some command. I require to take CLA in my final program, so I'm trying to execute from Command Prompt. A sample code is given

#include <stdio.h>
#define PATH_MAX 128

int main(void){

    FILE *fp;
    char path[PATH_MAX];

    fp = popen("ssh user@HOST \"command\"", "r");
    if (fp == NULL)
       /* Handle error */;

    while (fgets(path, PATH_MAX, fp) != NULL)
    printf("%s", path);
    fclose(fp);
    return 0;
}

The code execute fine using DevC++ but when I execute same from Command prompt it does not execute giving following message "'ssh' is not recognized as an internal or external command, operable program or batch file." I tried both system("some command") and popen(). Both work fine in DevC but give same message when executed from Command Prompt. I'm having MinGW installed gcc version 8.2.0 (MinGW.org GCC-8.2.0-3)

Tarick Welling
  • 3,119
  • 3
  • 19
  • 44
Obair
  • 23
  • 1
  • 5
  • This is not an issue with your program, but with the `%PATH%` environment variable. – Botje Sep 26 '19 at 07:14
  • 1
    Where is `ssh` installed? Is it in the Windows system `PATH`? – Some programmer dude Sep 26 '19 at 07:14
  • So the program is working fine and you're just asking for help with running ssh on Windows? How is that a programming question? – melpomene Sep 26 '19 at 07:15
  • Possible duplicate of ['git' is not recognized as an internal or external command](https://stackoverflow.com/questions/4492979/git-is-not-recognized-as-an-internal-or-external-command) – Botje Sep 26 '19 at 07:15
  • I have checked path variables and it exists already **C:\Windows\System32\OpenSSH\**. Infact if i directly go to command prompt and establish/run SSH connection, it works fine. Only trouble i am facing is while trying to do the same through C program from Command Prompt. – Obair Sep 26 '19 at 10:05
  • Thanks all, finally resolved the issue by adding path under System as well as User Variables. – Obair Sep 27 '19 at 10:29

2 Answers2

2

So it seems that even though the required path was present under System variables but, the program gets executed under User variables. So just by adding the same path (i.e; C:\Windows\System32\OpenSSH) under User Variables solved the issue.

Obair
  • 23
  • 1
  • 5
0

SSH.exe needs to be in the Windows PATH. When the program is running, it might not be running under your profile.

It is possibly running under a system profile that does not have the same PATH defined. You could have the program check and/or update the PATH or have it run under your profile specifically.

alexherm
  • 1,362
  • 2
  • 18
  • 31
  • The purpose of this forum is to provide resolution. Please add the solution or delete the post. – alexherm Sep 28 '19 at 11:56
  • Thanks all, finally resolved the issue by adding path under User Variables. So i guess the problem was the program getting executed under User profile rather than System profile. – Obair Sep 30 '19 at 11:10