Telnet C++ or SSH Program on Launch - c++

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.

Related

Connect to Wi-Fi programmatically in C ++ / Linux

I am creating a program which connects to the WIFI via the network's name and password. I'm able to do it with the system call of the command nmcli dev wifi connect <mySSID> password <myPassword>, but I'm not sure that this is the best way of doing this.
For example, how can I handle the incorrect password case? Checking the CERR of the command output isn't really the solution.
I would like to avoid using the system calls. Is there a way of doing this with usual programming, and preferably not very difficult?

Telnet server within C++ app

I am writing an application to run on a robot. Currently, it is headless, but I want to be able to telnet directly to the application with no authentication and access a shell that I will write.
Is this possible? Would it be practical or are there much easier solutions?
It is entirely possible.
However, if you are using Linux, you may just as well just let your application do it's I/O to the terminal, and use telnet to log in. If you set up a user to use your application as the "login-shell", it will allow direct access to that user called "robot" (for example) (and you can set it to have no password too) - then just do telnet -l robot machine port.
This would save you the effort of writing your own telnet client, and give you almost identical functionality.
If you're using a custom shell, why would you need telnet? Your shell can have a daemon component to listen on given port and then hand over the interaction to whatever REPL your shell would implement.

Run Linux command remotely from Window based application

I want to run Linux command remotely from Window based Qt C++ application programmatically. What is the simpliest way to do it?
You need some sort of server on the Linux machine and your Windows machine will be a client. I'd say the easiest way would be just make a php script to run your command and drop it in your www root and have your Windows machine fetch that URL.
At the end of the day, without knowing what your requirements are with regard to security and with regard to what kind of commands you'll be running is, it's very difficult to give a definitive answer to this question.
Simply connect to telnet server on the linux using sockets, and send the commands.
This actually requires very little code. Check the Java version here:
Sending telnet commands and reading the response with Java
You can do similarly with Qt/C++ as well.
Simple server-side program witch will handle the requests and then using ex system() function will be this "remotely" part of solution.
And on client-side simple text field handled by function witch will be able to connect to server to send command run request.
The most important thing in this solution will be to take care about security.
One way to do it is, to have a client-server model, the server resides in linux and client can be your computer. That way you can send commands to the server and have its output thrown at you. That's one way I think of this problem.
Use UPnP to get past the firewall(or use NAT traversal or UDP/TCP hole punching). Otherwise (without forwarding the port) it would be impossible to reach the server.
The second is to write your own RSH and SSH utility. (or use putty or other pre-existing software)
You could use Plink if you are on Windows whatever version. If you can run PuTTY, then you can run PLink. PuTTY Plink Documentation. Using that you could use the executable, and automate things. Otherwise, if you're looking for a specific programming language, they'd still be dependent upon some SSH Library. If you're writing your own installer, you could include the PLink.exe in your installer, and distribute it with your application.
From the documentation page:
Z:\sysosd>plink login.example.com 'echo "Hello World"'

C++: Talk to and receive data from MySQL Server

Either this this question isn't asked often (possible), everyone knows how to do it (doubtful), or I don't know how to search for it (likely).
I want to get my C++ program talking to my MySQL server and able to run commands on it. I know MySQL has connector code available, but 1) it seems like much more than I need and 2) I just cannot seem to get it to work anyway.
I want to be able to test the program on my computer, so remote access would be necessary. I do have SSH for the server.
My final deployment will be able to run on the server itself.
Executing and receiving/parsing output from MySQL databases is quite easy to do by hand. I can't however, figure out which way to go to get my program to do this.
I figure I should be able to:
1. ssh into the web server (with password)
2. access the MySQL database
3. execute statements and retrieve their output
I've read a lot about fork() and popen(), but due to their read-only or write-only limitations (unless I misunderstand), I just don't know where to go. Obviously I'd just take out the SSH step when the time comes to deploy it on the server. Can somebody give me some direction on this, or is this just not possible (doubtful).
It is our own dedicated server so we have full reign with ssh and WHM, but I'd rather minimize the "intrusion" since I don't know enough about all the software on there if I broke something. I just want to communicate [directly] with the MySQL database.
Thanks so much! You guys are awesome!
If you have SSH access, then you're way overcomplicating this.
Step 1: Set up an SSH tunnel:
ssh -L 3306:localhost:3306 user#your.host.com
Step 2: Use the local port like normal (i.e. just refer to localhost)
mysql -u root -p < somescript.sql
However since you're using C++, you really should use a proper library (e.g. MySQL++). Educate yourself on SSH tunnels, they make life so much easier once you know about them.

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.