add more spacing
[personal-kdebase.git] / runtime / kioslave / sftp / tests / ksshprocesstest.cpp
blobbe4a8f47790dc0be089410ba9d56687c40c726fb
1 #include "ksshprocess.h"
2 #include <iostream>
4 using namespace std;
6 int main(int argc, char *argv[]) {
8 if( argc < 5 ) {
9 cout << "Usage: " << argv[0] <<
10 " <ssh path> <host> <username> <password>" << endl;
11 return 1;
14 KSshProcess ssh(argv[1]);
15 cout << ssh.version() << endl;
17 KSshProcess::SshOptList opts;
18 KSshProcess::SshOpt opt;
20 opt.opt = KSshProcess::SSH_PORT;
21 opt.num = 22;
22 opts.append(opt);
24 opt.opt = KSshProcess::SSH_HOST;
25 opt.str = QString(argv[2]);
26 opts.append(opt);
28 opt.opt = KSshProcess::SSH_USERNAME;
29 opt.str = QString(argv[3]);
30 opts.append(opt);
32 // opt.opt = KSshProcess::SSH_PASSWD;
33 // opt.str = QString(argv[4]);
34 // opts.append(opt);
36 if( !ssh.setOptions(opts) ) {
37 cout << "ksshprocesstest: setOptions failed" << endl;
38 return -1;
41 ssh.printArgs();
43 bool stop = false;
44 bool connected;
45 char buf[256];
46 char c;
47 while( !stop && !(connected = ssh.connect()) ) {
48 cout << "ksshprocesstest: Error num - " << ssh.error() << endl;
49 cout << "ksshprocesstest: Error msg - " << qPrintable(ssh.errorMsg()) << endl;
50 switch( ssh.error() ) {
51 case KSshProcess::ERR_NEED_PASSWD:
52 case KSshProcess::ERR_NEED_PASSPHRASE:
53 cout << "Password: ";
54 cin >> buf;
55 cout << "password is " << buf << endl;
56 ssh.setPassword(QString(buf));
57 break;
58 case KSshProcess::ERR_NEW_HOST_KEY:
59 case KSshProcess::ERR_DIFF_HOST_KEY:
60 cout << "Accept host key? (y/n): ";
61 cin >> c;
62 cout << "Answered " << c << endl;
63 ssh.acceptHostKey(c == 'y' ? true : false);
64 break;
65 case KSshProcess::ERR_AUTH_FAILED:
66 cout << "ksshprocesstest: auth failed." << endl;
67 stop = true;
68 break;
69 case KSshProcess::ERR_AUTH_FAILED_NEW_KEY:
70 cout << "ksshprocesstest: auth failed because of new key." << endl;
71 stop = true;
72 break;
73 case KSshProcess::ERR_AUTH_FAILED_DIFF_KEY:
74 cout << "ksshprocesstest: auth failed because of changed key." << endl;
75 stop = true;
76 break;
78 case KSshProcess::ERR_INTERACT:
79 case KSshProcess::ERR_INTERNAL:
80 case KSshProcess::ERR_UNKNOWN:
81 case KSshProcess::ERR_INVALID_STATE:
82 case KSshProcess::ERR_CANNOT_LAUNCH:
83 case KSshProcess::ERR_HOST_KEY_REJECTED:
84 cout << "ksshprocesstest: FATAL ERROR" << endl;
85 stop = true;
86 break;
91 if( connected ) {
92 cout << "ksshprocesstest: Successfully connected to " << argv[2] << endl;
94 else {
95 cout << "ksshprocesstest: Connect to " << argv[2] << " failed." << endl;