summaryrefslogtreecommitdiffstats
path: root/tdeioslave/sftp/ksshprocesstest.cpp
blob: 59dbf58c734e8bfd3edbd63919fb7eff90d1dbac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "ksshprocess.h"
#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {

    if( argc < 5 ) {
        cout << "Usage: " << argv[0] << 
            " <ssh path> <host> <username> <password>" << endl;
        return 1;
    }

    KSshProcess ssh(argv[1]);
    cout << ssh.version() << endl;
    
    KSshProcess::SshOptList opts;
    KSshProcess::SshOpt opt;

    opt.opt = KSshProcess::SSH_PORT;
    opt.num = 22;
    opts.append(opt);

    opt.opt = KSshProcess::SSH_HOST;
    opt.str = TQString(argv[2]);
    opts.append(opt);

    opt.opt = KSshProcess::SSH_USERNAME;
    opt.str = TQString(argv[3]);
    opts.append(opt);

//    opt.opt = KSshProcess::SSH_PASSWD;
//    opt.str = TQString(argv[4]);
//   opts.append(opt);

    if( !ssh.setOptions(opts) ) {
        cout << "ksshprocesstest: setOptions failed" << endl;
        return -1;
    }

    ssh.printArgs();

    bool stop = false;
    bool connected;
    char buf[256];
    char c;
    while( !stop && !(connected = ssh.connect()) ) {
        cout << "ksshprocesstest: Error num - " << ssh.error() << endl;
        cout << "ksshprocesstest: Error msg - " << ssh.errorMsg().latin1() << endl;
        switch( ssh.error() ) {
            case KSshProcess::ERR_NEED_PASSWD:
            case KSshProcess::ERR_NEED_PASSPHRASE:
                cout << "Password: ";
                cin >> buf;
                cout << "password is " << buf << endl;
                ssh.setPassword(TQString(buf));
                break;
            case KSshProcess::ERR_NEW_HOST_KEY:
            case KSshProcess::ERR_DIFF_HOST_KEY:
                cout << "Accept host key? (y/n): ";
                cin >> c;
                cout << "Answered " << c << endl;
                ssh.acceptHostKey(c == 'y' ? true : false);
                break;
            case KSshProcess::ERR_AUTH_FAILED:
                cout << "ksshprocesstest: auth failed." << endl;
                stop = true;
                break;
            case KSshProcess::ERR_AUTH_FAILED_NEW_KEY:
                cout << "ksshprocesstest: auth failed because of new key." << endl;
                stop = true;
                break;
            case KSshProcess::ERR_AUTH_FAILED_DIFF_KEY:
                cout << "ksshprocesstest: auth failed because of changed key." << endl;
                stop = true;
                break;
            
            case KSshProcess::ERR_INTERACT:
            case KSshProcess::ERR_INTERNAL:
            case KSshProcess::ERR_UNKNOWN:
            case KSshProcess::ERR_INVALID_STATE:
            case KSshProcess::ERR_CANNOT_LAUNCH:
            case KSshProcess::ERR_HOST_KEY_REJECTED:
                cout << "ksshprocesstest: FATAL ERROR" << endl;
                stop = true;
                break;
                
        }
    }

    if( connected ) {
        cout << "ksshprocesstest: Successfully connected to " << argv[2] << endl;
    }
    else {
        cout << "ksshprocesstest: Connect to " << argv[2] << " failed." << endl;
    }
    
}