-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathexec_cmd.py
More file actions
60 lines (54 loc) · 2.16 KB
/
exec_cmd.py
File metadata and controls
60 lines (54 loc) · 2.16 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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# author : firefoxbug
# E-Mail : wanghuafire@gmail.com
# Blog : www.firefoxbug.net
"""
execute commands on remote hosts
"""
import os
import time
import socket
import paramiko
from report_log import ReportLog
class ExecCMD(object):
@classmethod
def exec_cmd(self,exec_cmd):
try :
# print exec_cmd['ip'],exec_cmd['passwd']
# print "\n"
print "\nConnecting %s..."%exec_cmd['ip']
log_time = time.strftime("%Y-%m-%d %H:%M:%S")
socket.setdefaulttimeout(2)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(exec_cmd['ip'],exec_cmd['port'],exec_cmd['username'],exec_cmd['passwd'])
print "Connected %s Successfully..."%exec_cmd['ip']
stdin, stdout, stderr = ssh.exec_command(exec_cmd['cmd2exec'])
err_message = stderr.read()
if err_message:
print err_message
ReportLog.send_results(True,"[INFO %s]: execute [%s] on %s"%(log_time,exec_cmd['cmd2exec'],exec_cmd['ip']),des_str=err_message)
return True
ok_message = stdout.read()
ReportLog.send_results(True,"[INFO %s]: execute [%s] on %s"%(log_time,exec_cmd['cmd2exec'],exec_cmd['ip']),des_str=str(ok_message))
ssh.close()
except socket.timeout:
ReportLog.send_results(False,"[ERROR %s]: execute [%s] on %s"%(log_time,exec_cmd['cmd2exec'],exec_cmd['ip']),des_str="%s Connection timed out"%(exec_cmd['ip']))
return False
except socket.error:
ReportLog.send_results(False,"[ERROR %s]: execute [%s] on %s"%(log_time,exec_cmd['cmd2exec'],exec_cmd['ip']),des_str="%s Connection timed out"%(exec_cmd['ip']))
return False
except paramiko.AuthenticationException:
ReportLog.send_results(False,"[ERROR %s]: execute [%s] on %s"%(log_time,exec_cmd['cmd2exec'],exec_cmd['ip']),des_str="[IP=%s Password=%s] Authentication failured"%(exec_cmd['ip'],exec_cmd['passwd']))
return False
return True
exec_cmd = {}
exec_cmd['ip'] = "42.21.117.13"
exec_cmd['username'] = "root"
exec_cmd['passwd'] = "zooboa.com"
exec_cmd['port'] = 22
exec_cmd['cmd2exec'] = "ls ...."
if __name__ == '__main__':
result = ExecCMD.exec_cmd(exec_cmd)
# ReportLog.send_results(True,"YES: execute [%s]"%(exec_cmd['cmd2exec']))