Unreliable system() return value in C++ - c++

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.
The test succeeds but gets failure reason (system() call unreliable).
Might be some network issue between the systems. If yes how to check this?
Thanks in advance.
SKP

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).
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.

Related

Read debug output from journalctl from inside C++ code

enter code hereI have a C++ application running on an Embedded Linux device which uses systemd.
If I wish to stream the debug output of my services / processes, I SSH into the device and execute command journalctl -u <my_systemd_service>.service -f from the terminal, where <my_systemd_service> is my actual service name.
Now the issue is that during production, I'd like to have SSH disabled for security reasons. However, I would still like to be retrieve the debug output sometimes, triggered by sending an MQTT command to the device as the device is connected to an MQTT broker.
Is there a way to get my debug output from within the C++ code itself when the MQTT command is received?
I don't suppose using system() would be ideal. For example system("journalctl -u <my_systemd_service>.service -f") ? Of course this would also need to run in a separate thread if so. Any recommendations would be appreciated.
You can direct service stdout/stderr to a file then read it using std::fstream whenever you need. You might want to see this: https://stackoverflow.com/a/43830129/11277878

How to execute a command on remote server (second level) after logging on a remote server via phpseclib?

I have to login to a server from a remote server. I am able to login to remote server using phpseclib. After that I am able to login to next server from that but next command executes on firt server and not the second server. For example:
Login to server1.example.com via SSH
Login to remote-server.example.com using internal script from server1.
Execute 'ls'.
ls returns output from server1 rather remote-server.
Are you absolutely sure your script on server1 actually logs into remote-server (and does not immediately log out)? The only explanation I can think of is that the "ls" command is not really run on the remote server. If you share the script and exact commands, that could help figure it out. (Output of "script" from the whole exchange might also be helpful.)
Something like the following might also work for you:
ssh server1.example.com ssh remote-server.example.com ls
Depending on your remote command, you might also do something like:
ssh server1.example.com "ssh remote-server.example.com ls"
(The latter might be needed if there are, e.g., redirects involved that could otherwise be interpreted by your local shell.)
As you use phpseclib, that might handle the first ssh in the examples above. So you might perhaps use something like:
$ssh->exec("ssh remote-server.example.com ls")
Or if you are using public key authentication for the second step, maybe:
$ssh->exec("ssh -i ~/.ssh/keyfile remote-server.example.com ls")
There's a quick summary of how to run commands remotely with ssh at https://www.ssh.com/ssh/command/

ssh: connect to host heroku.com port 22: No route to host

Hi I in an attempt to connect to more than one heroku account I did a number of stuff that has led to this error. "ssh: connect to host heroku.com port 22: No route to host" I dont know what the problem is or how to solve it so I want to start all over with heroku and ssh keys on my machine. How to I delete the present configurations and start afresh?
A few things I would attempt to do:
Ensure that there is no quirky setup in your HOSTS (/etc/hosts) file that is causing confusion
Check your ~/.ssh directory and ensure that the keys you expect to be there ... are there.
Check your ~/.ssh/config file for any incorrect information
Ensure that your heroku SSH server is actually listening on port 22. A lot of people will change the default SSH port for security purposes.
Ensure that the private key that you're attempting to use is uploaded to the heroku server. Some SSH instances won't tell you that it's an invalid key for security purposes. Instead it will just give you the rejection message you mentioned.
If I think of any other diagnostics I'll be sure to update.
Best of luck.
Perhaps something is set oddly in your $HOME/.ssh/config file. You can try backing up this file and then removing it to see if it resolves your problem.

MATLAB: Verify if a SSH tunnel to AWS is open.

I'm using MATLAB to connect to a database hosted in AWS (using the database toolbox). In order to do that, I supply the URL of the database as a local port and create a SSH tunnel to the AWS host.
The issue is that this tunnel needs to be created in order for the code to run. If it is not, no error message is generated but MATLAB gets hung and needs to be killed. I would like to deploy this code to users who will not be able to troubleshoot if this tunnel is missing.
My question is: is there a way to check for a local port opening in MATLAB? How would I check if the tunnel is setup?
Since you are using the Database Toolbox, you might want to use the logintimeout function. As the documentation says:
Note If you do not specify a value for logintimeout and the MATLAB session cannot establish a database connection, your MATLAB
session may freeze.
And you would wrap your code inside a try/catch block
I am not familiar with Matlab's TCP objects, but there is a system command that executes a program, returning its exit code (see its documentation). So what would probably do the job is a small program or script (as portable as needed), that tries to connect to the local port.
Alternatively, the small program/script could actually open (or re-open) the tunnel and return 0 on success. (This possibly adds the problem of how Matlab handles forking processes, I don't know how it handles that.)
There probably is some way to do the check if open and re-open if not-housekeeping via Matlab, but I have no clue.

Telnet C++ or SSH Program on Launch

I am wondering if someone can provide me with a couple c++ functions that would allow me to send and receive data over a telnet port.
I've also heard that I can create a program, and have it run via SSH, this would be preferred just because of the security benefits of SSH, are there any samples out there?
Update:
What I want to do, is create a console program users could use remotely via SSH or telnet. I am a C++ programmer.
Update 2: I know I was vague, I am creating an inventory system that I want to be accessible to our employees via SSH or telnet. I will be using sockets, and will often display data to the user via (telnet or through ssh) and accept input from them. I will have to implement sockets, and send data and receive data I know, is there a library for this?
I am not quite sure what you are asking here.
You can execute a remote program via:
ssh <user>#<machine> "<command>"
(Provided you have prepared a passphrase-less pubkey authentication; you'd be asked for a password / passphrase otherwise. The very first invokation will also require user input as SSH wants to verify the fingerprint of the remote machine.)
You could then capture the output of that command line (which is equivalent to the output of the remote command, unless ssh itself belches an error) via the usual means.
I seriously dont believe you want to implement telnet and SSH protocol all by your handwritten code!! That would be too difficult for you.
What I think you want to do is to "Run a command remotely". is it?
If yes then you you would need to execute the command as told by DevSolar but you will need to generate SSH keygen to be installed so that it doesnt ask for password at the prompt.
Else write an expect script.