You are not logged in.
Pages: 1
Hi! :)
I do have following problem and spent already quite a lot of time with it, also searching the internet, but pittily with no result :(
I need to use sftp with automatically passing the password and commands. For this I use expect - at the moment as script called by a c++ program. Now I want to optimize this "work around" using <expect.h> directly in my program.
A filepointer is created correctly using exp_popen, also the pattern match using exp_expectv works correctly. The only thing, which does not work, is writing the password. With fwrite I get the error message "Illegal seek". Somehow I do not find the error / the solution. Some hints or help would be great! :)
Thank you very much in advance!
Bea
And here's an example code of what I am doing:
#include <string>
#include <iostream>
#include <expect.h>
using namespace std;
int main(int argc, char** argv)
{
exp_is_debugging = 1;
exp_loguser = 0;
exp_timeout = 10; // timeout in seconds
// open sftp program
FILE* fp = exp_popen( "sftp [email protected]" );
if ( fp == NULL ) return 1;
cout << "Process Id: " << exp_pid << endl;
// "password:" is expected
struct exp_case expectCase;
expectCase.pattern = "password:";
expectCase.re = NULL;
expectCase.type = exp_glob;
expectCase.value = 0; // value to be returned upon match
// get the file descriptor
int fd = fileno( fp );
if ( exp_expectv( fd, &expectCase ) != 0 )
{
cerr << expectCase.pattern << " does not match." << endl;
return 1;
}
else
{
cout << expectCase.pattern << " found." << endl;
char* buffer[] = { "test" };
// enter the password automatically
fwrite( buffer, sizeof( buffer[0] ), 1, fp );
perror( "fwrite()" );
}
return 0;
}
Offline
sftp -b <batchfile> [email protected]
gethostbyintuition() is still a dream of mine
-- quoted from bash
Offline
Hi biologz,
thank you for your answer! :)
Well, the batchfile is working fine. Maybe I did not describe my problem clearly enough:
I need to download files via sftp. This should work automatically. Due to writing the file contents into a database, I wrote a C++ program calling an expect-script (for downloading), parsing the downloaded files and writing to database.
I start the script with a system call. The problem is that I do not know if the script stops e.g. due to connection failure or due to successful download.
I have built a workaround with a "valid"-file to find out if download was successful as well as a watchdog in case sftp does not return. I hoped, that there is a better solution. :-/
Offline
Well i'm still not sure why you're using a C program for that. I would only use shell script instead of C which is not needed for your problem (at least from what i understand).
Anyway maybe someone will have a better understanding than me or a different point of view.
But really if you have to download a file with SFTP then parse it and update a database, i would write a shell script using expect, then i'll still use shell script for parsing (with a program like awk) and then i'd you mysql to fill in the database.
Lol i realize this answer don't satisfy you so just wait for someone else to answer. The bosses of the forum will arrive soon i think, there are attracted by the smell of new posts like a lion is attracted by fresh meat ;-)
gethostbyintuition() is still a dream of mine
-- quoted from bash
Offline
Hi RobSeace,
I did not know libcurl before. It seems that only the newest version libcurl4 supports sftp, which is still experimental. But it seems worth to try :)
Concerning the expect - version:
Thanks for the hint with the perror - it was a copy-paste-thing to this test file :-/
Pittily the match does not work with
...
if ( exp_fexpectv( fp, &expectCase ) != 0 ) ...
It's crashing with a Segmentation fault. Maybe I miss something important :confused:
Thank you for your help!
Offline
I am trying to use libcurl to write a c code which does a sftp in batch mode but with password authentication, is there any way i can pass password on my own without asking it from user and also can someone please throw some light on how to use curl_easy_setopt() function to use it for my case.
Offline
I think you could either set the username and password right in the URL using
standard syntax (eg: "sftp://user:[email protected]/"), or via CURLOPT_USERPWD...
So, eg:
curl_easy_setopt (curlhandle, CURLOPT_URL, "sftp://user:[email protected]/");
/* ...or... */
curl_easy_setopt (curlhandle, CURLOPT_USERPWD, "user:Pass");
Offline
Thanks for the reply. I am not sure that whether sftp supports this sort password specifying in URL it self. I will try the other one. thanks again.
Offline
Pages: 1