-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathExpecter.m
108 lines (73 loc) · 1.58 KB
/
Expecter.m
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
99
100
101
102
103
104
105
106
107
108
//
// Expecter.m
// data_m
//
// Created by Mike Fluff on 09.02.11.
// Copyright 2011 Altell ltd. All rights reserved.
//
#import "Expecter.h"
@implementation Expecter
//FILE *fp;
void endup()
{
fprintf(fp,"\n");
exp_fexpectl(fp,
exp_glob,"[edit]",0,
exp_end);
}
void timedout()
{
fprintf(stderr,"timed outn");
exit(-1);
}
void commit()
{
fprintf(fp,"commit");
endup();
}
-(int) initWithHost:(char *)host port:(int) p user:(char *)user password:(char *)pass {
hostname = host;
port = p;
username = user;
password = pass;
exp_loguser = 0; // don't echo to terminal
exp_timeout = 3600; // arbitrary, you may change it for each interaction
if (NULL == (fd = exp_spawnl("/usr/bin/ssh","/usr/bin/ssh","-l",username,"-o UserKnownHostsFile=/dev/null","-o StrictHostKeyChecking=no",host,NULL))) {
perror("ftp");
exit(-1);
}
if (NULL == (fp = fdopen(fd,"r+"))) // mainly to be able to use printf instead of write...
return(0);
setbuf(fp,(char *)0);
if (EXP_TIMEOUT == exp_fexpectl(fp,
exp_glob,"word:",0,
exp_end)) {
timedout();
}
fprintf(fp,username);
fprintf(fp,"\n");
exp_fexpectl(fp,
exp_glob,"vyatta:",0,
exp_end) ;
fprintf(fp,"configure");
endup();
return 0;
}
-(int) execCommand: (NSString *)commandline
{
[self execParamCommand:commandline parameter:nil];
}
-(int) execParamCommand: (NSString *)commandline parameter:(id)param;
{
if(param==nil)
{
char *cmd;
cmd = [commandline UTF8String];
printf("%s",cmd);
fprintf(fp,cmd);
endup();
commit();
}
return 0;
}
@end