transfering file over ssh from c++

Started by
10 comments, last by corrington_j 18 years, 9 months ago
I am wondering how i can transfer files over ssh to our server either using c++ or from a command prompt. Currently i just do it manually with winscp3, but i want to automate the copying of certain files from my computer to the server. Thanks for the help.
Advertisement
The putty package has a commandline version of scp (pscp) that I usually use.
I don't know of any ssh libraries you could use but I suspect that you're better off invoking external programs programmatically anyway.
I recommend setting up a simple batch script to do the work, that's the kind of work they were designed to do. Automated password management might be painful though..
so you mean to use a dos batch to call pscp and login and transfer the files. Or does pscp have its own form of a batch file.
To do this in the past, I've used perl with Expect. This works probably only on unix systems, and is a little hacked together.
sub scp{my $experr;my $find;my $buf;my $rtn="";my $cli=shift;my $from=shift;my $to=shift;my $pass=shift;if (!($cli&$from&$to&$pass)){        return 0;}print $cli "scp -q -r $from $to\n";($rtn,$experr,$find,$buf,undef)=$cli->expect(60,"password:","Are you sure you want to continue connecting (yes/no)?");if (!$rtn){        return 0;}if ($rtn==2){        #Then the damned RSA key passing question is up        $cli->print ("yes\n");        ($rtn,$experr,$find,$buf,undef)=$cli->expect(20,"password:");        if (!$rtn){                return 0;        }}$cli->print ("$pass\n");($rtn,$experr,$find,$buf,undef)=$cli->expect(600, "-re","^.*[\#\>\$]");return $rtn;}
Quote:Original post by corrington_j
so you mean to use a dos batch to call pscp and login and transfer the files. Or does pscp have its own form of a batch file.
No, just a regular .bat file. Or your scripting language of choice really..

Uploading a directory (project) should be as easy as this:
pscp -p -r -pw password project user@hostname:project
If that's all you need then simply throw it in a batch file and execute it whenever necessary..
okay, i figured out how to transfer file, now i need to execute some commands, do i do that with putty?
Quote:Original post by corrington_j
okay, i figured out how to transfer file, now i need to execute some commands, do i do that with putty?
Try plink from the putty package. It's got a useful (-m) option for executing commands from a file.
ok, i figured out how to transfer the files, and connect with plink, but i cannot figure out how to execute commands with plink to build my code. heres what i am trying. The last two lines are where i need help. Thanks.

set PATH="C:\Program Files\putty";%PATH%

pscp -p -r -pw pass "C:\*.cpp" "C:\*.h" "C:\make.txt" dbadmin@Vgdb:/home/code/

plink -batch -l dbadmin -pw pass dbadmin@Vgdb
plink cd /home/dbadmin/Code/ImageRecon
plink make -f make.txt
Quote:Original post by corrington_j
plink -batch -l dbadmin -pw pass dbadmin@Vgdb
plink cd /home/dbadmin/Code/ImageRecon
plink make -f make.txt
You can't repeatedly call plink and expect it to keep the connection, everything has do be done in a single invokation.
So either combine both commands with a ";" (plink expects to see them as the last thing on the line, after the connection parameters), or send them through a separate script file with the -m option.
I tried this, but it won't connect now.

plink -batch -l dbadmin -pw pass dbadmin@Vgdb;
cd /home/dbadmin/Code/ImageRecon;
make -f make.txt

This topic is closed to new replies.

Advertisement