-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh-to-putty.c
More file actions
99 lines (82 loc) · 1.93 KB
/
ssh-to-putty.c
File metadata and controls
99 lines (82 loc) · 1.93 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void usage(void) {
fprintf(stderr, "Usage: TODO\n");
}
char* get_putty_cmd(char* userinfo, char* c_params,
char* host, char* port) {
char* cmd = malloc(32768);
cmd = "\"C:\\Program Files\\PuTTY\\putty.exe\" -ssh ";
if(port) {
strncat(cmd, "-P ", 3);
strncat(cmd, port, strlen(port) );
strncat(cmd, " ", 1);
}
if(userinfo) {
strncat(cmd, userinfo, strlen(userinfo) );
strncat(cmd, "@", 1);
}
if(host) {
strncat(cmd, host, strlen(host) );
}
return cmd;
}
int main(int argc, char* argv[]) {
// sshURI = "ssh:" hier-part
// hier-part = "//" authority path-abempty
// authority = [ [ ssh-info ] "@" ] host [ ":" port ]
// host = <as specified in [RFC3986]>
// port = <as specified in [RFC3986]>
// path-abempty = <as specified in [RFC3986]>
// ssh-info = [ userinfo ] [";" c-param *("," c-param)]
// userinfo = <as specified in [RFC3986]>
// c-param = paramname "=" paramvalue
// paramname = *( ALPHA / DIGIT / "-" )
// paramvalue = *( ALPHA / DIGIT / "-" )
char* host = NULL;
char* port = NULL;
char* userinfo = NULL;
char* c_params = NULL;
char *p, *t1, *t2; // temp
char* cmd;
if( argc != 2 ) {
usage();
return 1;
}
p = argv[1];
if( strncmp(p, "ssh://", 6) != 0 ) {
fprintf(stderr, "Url not recognized as ssh:// url\n");
return 1;
}
p += 6;
t1 = p + strlen(p) - 1;
if( *t1 == '/' ) {
*t1 = '\0';
}
t1 = strstr(p, "@");
if( t1 != NULL ) {
// ssh-info part present
*t1 = '\0';
userinfo = p;
p = t1+1;
t2 = strstr(userinfo, ";");
if( t2 != NULL ) {
*t2 = '\0';
c_params = t2+1;
}
}
host = p;
t1 = strstr(p, ":");
if( t1 != NULL ) {
// port present
*t1 = '\0';
port = t1+1;
}
cmd = get_putty_cmd(userinfo, c_params, host, port);
fprintf(stderr, "Running: %s\n", cmd);
system(cmd);
//sleep(5000);
free(cmd);
return 0;
}