0

I have a C++ module whose basic work is to check if a directory exists in the remote machine or not. I am using the system() call for the same .

ssh user@remote-machine  [ -d  /remote_dir/test] 

This works fine giving the result, but intermittently the test fails (the test directory is always there).

Now what might the reason and how to check this.

  1. The test succeeds but gets failure reason (system() call unreliable).
  2. Might be some network issue between the systems. If yes how to check this?

Thanks in advance.

SKP

cbuchart
  • 10,847
  • 9
  • 53
  • 93
user3160866
  • 367
  • 2
  • 9
  • Possible duplicate of [Checking if a directory exists in Unix (system call)](http://stackoverflow.com/questions/3828192/checking-if-a-directory-exists-in-unix-system-call) – didiz Apr 24 '17 at 06:07
  • You cannot tell a network failure from directory not existing. If you don't care, just do nothing. – n. m. could be an AI Apr 24 '17 at 06:13
  • Actually After checking the directory exists or not I do transfer some files to the directory – user3160866 Apr 24 '17 at 06:39

1 Answers1

0
  1. Make sure that your connection is ok for specified ssh port:

    $ telnet remote-machine 22
    

It should show connection messages, otherwise nothing (means that SSH not allowed to connect by IP firwall on local or remote machine).

  1. If not first, than check credentials to start the "system()" command. Because if you use system() call from the console from user "user1", but module started from user "user2" in the Linux system. In this case credentials to access remote server without password will fail because you made, in example, ssh keys for "user1" and not for "user2" - check it in the /home/(username)/.ssh. For debug if it so, use

    ...system('ls -l ~/.ssh');
    

And if no files then you should make connection keys for user who starts this module.

Alex
  • 1,297
  • 1
  • 16
  • 12
  • I periodically do the check(module is running without any interruption) , it works fine and running it for several days . Sometime only it fails , I mean it works fine but sometimes in periodic check it fails. So wanted to know why it happens sometimes only . Do I need to check anything in that period – user3160866 Apr 24 '17 at 06:32
  • yes, if you see that it happens check the port access immediately by telnet (your_server_ip) 22 if it fails or long time connection means that problems could be in connection otherwise it could be if you some deals work long time on the remote server – Alex Apr 24 '17 at 06:39