-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect_ssh.c
63 lines (62 loc) · 1.82 KB
/
connect_ssh.c
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
#include "examples_common.h"
// ssh连接
ssh_session connect_ssh(char *ip,char *user,char *port,char *pawd){
ssh_session session;
// 创建一个ssh会话
if((session=ssh_new()) == NULL){
fprintf(stderr, "创建ssh会话失败\n");
exit(1);
}
// 设置ssh会话相关信息
if (ip!=NULL && user!=NULL && port!=NULL)
{
// 设置ip
if (ssh_options_set(session,SSH_OPTIONS_HOST,ip) < 0)
{
ssh_free(session);
fprintf(stderr, "ssh会话设置ip失败\n");
return NULL;
}
// 设置用户
if (ssh_options_set(session,SSH_OPTIONS_USER,user) < 0)
{
ssh_free(session);
fprintf(stderr, "ssh会话设置user失败\n");
return NULL;
}
// 设置端口
if (ssh_options_set(session,SSH_OPTIONS_PORT_STR,port) < 0)
{
ssh_free(session);
fprintf(stderr, "ssh会话设置port失败\n");
return NULL;
}
}
// ssh连接远程主机
if (ssh_connect(session))
{
fprintf(stderr, "连接失败 : %s\n", ssh_get_error(session));
ssh_disconnect(session);
ssh_free(session);
return NULL;
}
// 新增knownhost
if (verify_knownhost(session)<0)
{
ssh_disconnect(session);
ssh_free(session);
fprintf(stderr, "knownhost失败\n");
return NULL;
}
// 验证
int auth=0;
if ((auth=authenticate_console(session,pawd))==SSH_AUTH_SUCCESS)
return session;
else if(auth==SSH_AUTH_DENIED)
fprintf(stderr,"Authentication failed\n");
else
fprintf(stderr,"Error while authenticating : %s\n",ssh_get_error(session));
ssh_disconnect(session);
ssh_free(session);
return NULL;
}