Automated SSH with C++

Hi. This time, I have a rather big goal. This program that I am creating will be passed out to 2 of my most trusted friends. What I'd like to do in C++ is firstly log in to my SSH server, without my friend knowing the password. Essentially, the program will fill in my password for whoever is using this program. Then I'd like to execute a program on the remote server, note that this file requires user input. Then, once the program on the remote server is done executing, I'd like for it to immediately disconnect from the SSH server. Would this be possible? I'm fine with system calls. Speed is not of the essence here, it is merely something a few friends would like to us. Time, however, is of the essence. I can do a
System("ssh myusername@serverip");
although I can't get it to automatically input my password. I also can't figure out how to execute my program on the remote machine. This program requires navigating to /Home/MyUsername/Server, then doing ./program. The program is also written in C++. Can I do all of this in one program?
You can do all this with C++ (indeed, you can do anything your system is capable of). It's not trivial. I think you might be a lot better off with some kind of simple scripting language for this. Use the right tool for the job.
Use the right tool for the job.


+1. I always advocate this. People tend to like to stick to one tool (language), and make it work for everything. Yea, you can hammer a nail with a screwdriver, but is that really the best tool for the situation?

Expect is a scripting language that was made for this situation. It's pretty simple, might check that out.
Would this be possible?

No. If your program knows the password, then the user can extract it from the program, no matter how much trouble you go through to prevent this.

The right approach would be to write a server program that takes the input, executes the program and sends back the output (if any).
I have looked at Expect, although Here's the thing: I need it to run the process, then as soon as that process terminates, it exits the SSH session. How would I do that? I have the following expect script:
1
2
3
4
5
6
7
#!/usr/bin/expect -f
spawn ssh jonny@myserver
expect "*password:*"
send -- "mypass\r"
send -- "cd ~/server\r"
send -- "./adduser\r"
interact;

And I don't really care if the user extracts the password from the program. As stated in my original post, this will be passed out to 2 of my most trusted friends, and even if they do screw with it, firstly they know nothing about computers, and also I don't really care what happens to that computer anymore. Now, basically, this program asks for a "Username", then looks in the list of usernames to see if that username has been taken. If it has, you must enter a new one until you get one that's not taken. Then you enter a password, and once you do that the ssh session should end. I don't know how to end it, however, because if I did write a program that takes the input, it wouldn't be able to tell if the "username" has been picked already. Essentially, my goal is to have a program that connects to my server without prompting for a password (what I have is good enough), then execute that program directly from the server, and close the connection once that program finishes executing. Of course, I am a complete newbie to Expect, can anyone please help me out?
I need it to run the process, then as soon as that process terminates, it exits the SSH session. How would I do that?
 
ssh userid@host "cd /path/; ./progname"
Firstly, is this in Expect? Or is this in C++? If it's in C++, which library do I have to look in? This seems to be something I could run with system(), although I cannot get it to automatically login.
That's standard use of ssh from the command line.

It gets even better because it preserves stdin and stdout. For example, ever get anoyed at scp not copying file attributes properly?

You can copy a directory correctly using:
 
ssh userid@host "cd /parent/; tar -cf - directory" | tar -xvf -
That uses ssh to connect to host as userid move to directory parent then create a tar archive to stdout of directory directory. That stream get's passed back over ssh to the local stdout, which is redirected to tar that deserialised the stream to a local copy of directory.
Yes, I got that part, and that is very useful. It works fine for now, but over time I'd like for it to login without entering my password. Thanks for everything so far.
Password-less logins with OpenSSH
http://www.debian-administration.org/articles/152
Topic archived. No new replies allowed.