0

I have to run a ssh command in a separate process (so by means of execlp) to connect the running machine to another machine in the same local network. The thing is, I have to establish the hostname entered is valid so the ssh connection succeeds.

Since, execlp replaces the calling process' image on a successful command run (which will be the case with ssh), there is, to the best of knowledge, no way of knowing in the calling process whether the ssh connection was successfully set up.

Hence, the sole solution to this incovenient behavior way I can think up is to assert the given hostname of the machine to connect to is valid. How can/should I about that?

(A valid hostname is simply one that exists and, of course, is reachable, be it an IP adress or an alias)

WIlopu
  • 47
  • 7
  • Define “hostname is valid”. Does it mean it resolves an A record in DNS? AAAA record? Both? If the user has an alias for the given hostname in their `.ssh/config`, should that be taken into account? How about system-level SSH config? If it’s just an IP need it be valid IPv4? IPv6? Or does it also need to be reachable? – Andrew Marshall Dec 28 '15 at 03:32
  • Is your process forking and running ssh in a child process, or are you trying to exec ssh (and end your original program) only if you know ssh won't fail? – Dmitri Dec 28 '15 at 03:35
  • @AndrewMarshall I edited the OP message. @Dmitri I fork `ssh` (with `popen() because I need to write to `ssh`'s input stream) but I'd rather make sure it doesn't fail beforhand; this I spare myself post-error treatments. – WIlopu Dec 28 '15 at 03:41
  • can you show your code? And then you should check the man pages for the return values – Ryan Dec 28 '15 at 03:43
  • You now need to define “reachable”. Is ICMP acceptable? Or should it use the port SSH will use (note that SSH takes into account multiple configuration files and command arguments to determine this)? Or some other method? Note also that the hostname given to SSH may not resolve normally, but only because it is listed as a `Host` entry in the config. – Andrew Marshall Dec 28 '15 at 03:45
  • @self: The return values are of no use in this case, because in case `ssh` fails, the process running it (the one I forked from the main program) ends as soon as it returns, leaving me no way of getting the return values of the `ssh` call. – WIlopu Dec 28 '15 at 03:45
  • @AndrewMarshall Sorry, but I am not at all into networking stuff this early into my education path (for now). So by "reachable", I mean any hostname/IP address that won't cause `ssh` to return "Could not resolve hostname *hostname*. Name or service not known." – WIlopu Dec 28 '15 at 03:48
  • 2
    If you are indeed using a fork then exec to run the ssh process, you could use waitpid (with the WNOHANG option) to inspect see if the ssh process has exited with an error status. – darklion Dec 28 '15 at 03:48
  • look into libssh2 to implement the protocol as a client, then you can get really deep into it. – Ryan Dec 28 '15 at 03:52
  • 1
    @darklion: In order to spare myself all the pipe setting, I use `popen()` to run `ssh`. So yes, if there is no other way, I might have to go down the less lazy path. But what am I supposed to check for in the `status` `waitpid()` sets? – WIlopu Dec 28 '15 at 03:54
  • There are lots of reasons why ssh might fail to connect, or disconnect early after making a connection. Focusing on whether the hostname is valid is just covering one of those many reasons. Beyond that, this question is really vague. You should edit your question to include some code which illustrates what the ssh connection is supposed to accomplish--Are you communicating through the ssh link? Running a remote command? Creating a TCP forward? – Kenster Dec 28 '15 at 19:34
  • @Wllopu: ssh returns 255 is something goes wrong (otherwise the status of the remote command). There area bunch of macros which test the return value of wait/waitpid to cover all bases. Otherwise it is much like Kenster said: it depends on what you're trying to accomplish with the ssh command. – darklion Dec 30 '15 at 02:46

1 Answers1

0

In order to spare myself all the pipe setting, I use popen() to run ssh. So yes, if there is no other way, I might have to go down the less lazy path. … waitpid()

You don't have to go down the less lazy path. man popen:

The pclose() function waits for the associated process to terminate and returns the exit status of the command as returned by wait4(2).

Armali
  • 18,255
  • 14
  • 57
  • 171