8
8
9
9
import yaml
10
10
import logging
11
+ import nmap
12
+ import paramiko
11
13
12
14
os .environ .setdefault ('DJANGO_SETTINGS_MODULE' , 'sandboxMP.settings' )
13
15
error_logger = logging .getLogger ('sandbox_error' )
@@ -72,3 +74,136 @@ def get_net_address(self):
72
74
"""
73
75
key = ['hosts' , 'net_address' ]
74
76
return self .get_conf_content (* key )
77
+
78
+
79
+ class SandboxScan (ConfigFileMixin ):
80
+
81
+ def basic_scan (self ):
82
+ """
83
+ Use ICMP discovery online hosts and return online hosts.
84
+ """
85
+ hosts = self .get_net_address ()
86
+ nm = nmap .PortScanner ()
87
+ nm .scan (hosts = hosts , arguments = '-n -sP -PE' )
88
+ online_hosts = nm .all_hosts ()
89
+ return online_hosts
90
+
91
+ def os_scan (self ):
92
+ """
93
+ Get the system type by nmap scan and return hosts list with os type.
94
+ """
95
+ hosts = self .get_net_address ()
96
+ nm = nmap .PortScanner ()
97
+ nm .scan (hosts = hosts , arguments = '-n sS -O' )
98
+ online_hosts = []
99
+ for host in nm .all_hosts ():
100
+ try :
101
+ os_type = nm [host ]['osmatch' ][0 ]['osclass' ][0 ]['osfamily' ]
102
+ except Exception :
103
+ os_type = 'unknown'
104
+ host_dict = {'host' : host , 'os' : os_type }
105
+ online_hosts .append (host_dict )
106
+ return online_hosts
107
+
108
+ def get_net_address (self ):
109
+ """
110
+ Return the hosts that will be used to scan.
111
+ Subclasses can override this to return any hosts.`
112
+ """
113
+ hosts_list = super ().get_net_address ()
114
+ hosts = ' ' .join (str (i ) for i in hosts_list )
115
+ return hosts
116
+
117
+
118
+ class LoginExecution (ConfigFileMixin ):
119
+
120
+ def login_execution (self , auth_type = 'password' , ** kwargs ):
121
+ """
122
+ Support two authentication modes: password or private_key, and auth_type default is password.
123
+ """
124
+ ssh = paramiko .SSHClient ()
125
+ ssh .set_missing_host_key_policy (paramiko .AutoAddPolicy ())
126
+ try :
127
+ if auth_type == 'password' :
128
+ ssh .connect (
129
+ kwargs ['hostname' ],
130
+ kwargs ['port' ],
131
+ kwargs ['username' ],
132
+ kwargs ['password' ],
133
+ timeout = 3 ,
134
+ )
135
+ kwargs ['auth_type' ] = 'password'
136
+ elif auth_type == 'private_key' :
137
+ kwargs ['auth_type' ] = 'private_key'
138
+ private_key = paramiko .RSAKey .from_private_key_file (kwargs ['private_key' ])
139
+ ssh .connect (
140
+ kwargs ['hostname' ],
141
+ kwargs ['port' ],
142
+ kwargs ['username' ],
143
+ private_key ,
144
+ timeout = 3 ,
145
+ )
146
+ kwargs ['status' ] = 'succeed'
147
+ kwargs ['error_message' ] = ''
148
+ commands = self .get_commands ()
149
+ for key , value in commands .items ():
150
+ stdin , stdout , stderr = ssh .exec_command (value , timeout = 5 )
151
+ result = str (stdout .read ()).strip ('b' ).strip ("'" ).strip ('\\ n' )
152
+ kwargs [key ] = result
153
+ except Exception as e :
154
+ msg = '%(exc)s hostname %(hostname)s' % {
155
+ 'exc' : e ,
156
+ 'hostname' : kwargs ['hostname' ]
157
+ }
158
+ error_logger .error (msg )
159
+ kwargs ['status' ] = 'failed'
160
+ kwargs ['error_message' ] = str (e )
161
+ return kwargs
162
+
163
+ def password_login_execution (self , ** kwargs ):
164
+ """
165
+ Login to the remote system with a password.
166
+ Kwargs is a dict containing hostname, port, username and password.
167
+ Example: kwargs = {'hostname': '172.16.3.101', 'port': 22, 'username': 'root', 'password': 'paw123'}
168
+ """
169
+ return self .login_execution (** kwargs )
170
+
171
+ def private_key_login_execution (self , ** kwargs ):
172
+ """
173
+ Login to the remote system with a private_key.
174
+ Kwargs is a dict containing hostname, port, username and private key.
175
+ Example:kwargs = {'hostname': '172.16.3.101', 'port': 22, 'username': 'root', 'private_key': '/root/.ssh/id_rsa'}
176
+ """
177
+ return self .login_execution (auth_type = 'private_key' , ** kwargs )
178
+
179
+ def get_auth_type (self ):
180
+ key = ['hosts' , 'auth_type' ]
181
+ return self .get_conf_content (* key )
182
+
183
+ def get_ssh_username (self ):
184
+ key = ['hosts' , 'ssh_username' ]
185
+ return self .get_conf_content (* key )
186
+
187
+ def get_ssh_port (self ):
188
+ key = ['hosts' , 'ssh_port' ]
189
+ return self .get_conf_content (* key )
190
+
191
+ def get_ssh_password (self ):
192
+ key = ['hosts' , 'ssh_password' ]
193
+ return self .get_conf_content (* key )
194
+
195
+ def get_ssh_private_key (self ):
196
+ key = ['hosts' , 'ssh_private_key' ]
197
+ return self .get_conf_content (* key )
198
+
199
+ def get_email (self ):
200
+ key = ['hosts' , 'email' ]
201
+ return self .get_conf_content (* key )
202
+
203
+ def get_send_email (self ):
204
+ key = ['hosts' , 'send_email' ]
205
+ return self .get_conf_content (* key )
206
+
207
+ def get_scan_type (self ):
208
+ key = ['hosts' , 'scan_type' ]
209
+ return self .get_conf_content (* key )
0 commit comments