-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh2server.cpp
More file actions
133 lines (129 loc) · 2.75 KB
/
ssh2server.cpp
File metadata and controls
133 lines (129 loc) · 2.75 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cstring>
#define DEFAULT_PATH "/var/saved_ssh"
#define MAX_DATA 50
using namespace std;
int top=-1;
class server
{
public:
int num=0;
string addr="";
string account="";
string password="";
string remarks="";
void list()
{
cout<<"\033[36m["<<num<<"]"<<endl
<<"ip :"<<addr<<endl
<<"account :"<<account<<endl
<<"password :\""<<password<<'\"'<<endl
<<"remarks :"<<remarks<<"\033[0m"<<endl;
}
void connect()
{
string s="";
s=s+"sshpass -p \""+password+"\" ssh "+account+"@"+addr;
system((char*)s.data());
}
};
server serverinfo[MAX_DATA];
void init()
{
ifstream fi;
fi.open(DEFAULT_PATH,ios::in);
while(fi>>serverinfo[++top].addr&&serverinfo[top].addr!="EOF")
{
serverinfo[top].num=top;
fi>>serverinfo[top].account>>serverinfo[top].password;
getline(fi,serverinfo[top].remarks);
getline(fi,serverinfo[top].remarks);
}
top--;
}
void del()
{
int num;
cout<<"\033[34mSelect a server :\033[0m";
cin>>num;
cout<<"\033[36m-------Del info--------"<<endl;
serverinfo[num].list();
cout<<"Are you sure?(y/n)";
string ack;
cin>>ack;
if(ack[0]=='y'||ack[0]=='Y')
{
cout<<"\033[32mAccepted\033[0m"<<endl;
memcpy(&serverinfo[num],&serverinfo[num+1],sizeof(serverinfo)*(top-num));
top--;
}
else cout<<"\033[31mCanceled\033[0m"<<endl;
}
void add()
{
char ip[4];
cout<<"\033[34m--------New Server-------\nip :\033[0m";
cin>>serverinfo[++top].addr;
cout<<"\033[34mLogin Account :\033[0m";
cin>>serverinfo[top].account;
cout<<"\033[34mPassword :\033[0m";
cin>>serverinfo[top].password;
cout<<"\033[34mRemarks :\033[0m";
getchar();
getline(cin,serverinfo[top].remarks);
serverinfo[top].list();
cout<<"Are you sure?(y/n)";
string ack;
cin>>ack;
if(ack[0]=='y'||ack[0]=='Y') cout<<"\033[32mAccepted\033[0m"<<endl;
else cout<<"\033[31mCanceled\033[0m"<<endl,top--;
}
void save()
{
ofstream fi;
fi.open(DEFAULT_PATH,ios::out);
for(int i=0;i<=top;i++)
{
fi<<serverinfo[i].addr<<" "<<serverinfo[i].account<<" "<<serverinfo[i].password<<endl<<serverinfo[i].remarks<<endl;
}
fi<<"EOF";
}
int main(int argc,char** argv)
{
init();
if(argc==2&&argv[1][0]=='l')
{
for(int i=0;i<=top;i++)
{
serverinfo[i].list();
}
}
else if(argc==2&&argv[1][0]>='0'&&argv[1][0]<='9')
{
int num=0;
for(char *i=argv[1];*i!='\0';i++)
{
num*=10;
num+=*i-'0';
}
if(num>top||num<0)
{
cout<<"No such server"<<endl;
exit(0);
}
serverinfo[num].connect();
}
else if(argc==2&&argv[1][0]=='a') add();
else if(argc==2&&argv[1][0]=='d') del();
else
{
cout<<"Usage: ssh2server [No./a/l]"<<endl
<<"a: add a new server"<<endl
<<"d: delete selected server"<<endl
<<"l: list all saved server-info"<<endl;
}
save();
}