-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitcoinControl.py
142 lines (118 loc) · 4.36 KB
/
bitcoinControl.py
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
133
134
135
136
137
138
139
140
141
142
'''
Created on Aug 22, 2011
@author: boby
'''
import paramiko ,os, getopt, sys, time, logging
from threading import Thread
from Encryption import Encryption
from Config import Config
config = Config().getConfig()
log = Config().getLogger('root')
class BTControl(Thread):
def __init__(self, host_name, user_name, password,command=''):
Thread.__init__(self)
self.host_name = host_name
self.user_name = user_name
self.__password = password
self.__port = 22 #default SSH __port
self.__chan = None
self.command = command
self.connection = None
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
def __str__(self) :
#return str(self.__dict__)
return str({"host_name":self.host_name, "user_name":self.user_name})
def __eq__(self, other) :
return self.__dict__ == other.__dict__
def run(self):
if not self.check_host():
return
self.login()
self.run_command(self.command)
self.logout()
def login(self):
"""Connect to a remote host and login.
"""
#self.ssh.load_system_host_keys()
#self.ssh.set_missing_host_key_policy(paramiko.WarningPolicy)
log.debug("Making connection to remote host: %s with user: %s" % (self.host_name, self.user_name))
self.connection = self.ssh.connect(self.host_name,self.__port, self.user_name, self.__password, timeout=5)
log.debug("Starting Shell!")
self.__chan = self.ssh.invoke_shell()
log.debug("Connection established!")
def run_command(self, command):
"""Run a command on the remote host.
@param command: Unix command
@return: Command output
@rtype: String
"""
log.debug("sending command: '%s' to host" % command)
self.__chan.send(command+'\n')
time.sleep(5)
return self.__chan.send_ready()
def check_host(self):
try:
log.debug("Making connection to remote host: %s with user: %s" % (self.host_name, self.user_name))
self.connection = self.ssh.connect(self.host_name,self.__port, self.user_name, self.__password, timeout=5)
log.debug("Starting Shell!")
self.__chan = self.ssh.invoke_shell()
self.ssh.close()
log.debug("Host is alive and running!")
except Exception:
log.error("A connection to the host cannot be established!!!")
return False
return True
def logout(self):
"""
Close the connection to the remote host.
"""
log.info("closing connection to host %s" % self.host_name)
self.ssh.close()
def usage():
print"This is a help utility for stopping or starting bitcoins on remote machines."
print "Usage: %s [flags] start/stop" % os.path.basename(__file__)
print "Flags available:"
print " -h help"
print " -d debug flag"
def main(argv):
try:
opts, args = getopt.getopt(argv, "hd", ["help", "debug"])
except getopt.GetoptError:
usage()
sys.exit(2)
global debug
debug=0
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt == '-d':
debug = 1
hash = "".join(args)
if debug==1:
log.setLevel(logging.DEBUG)
else:
log.setLevel(logging.INFO)
if len(hash)==0:
print "No command provided"
usage()
sys.exit(0)
return hash
def execute(hash):
computer_list = config["hosts"]
log.info("The command is: %s" % hash)
enc=Encryption()
if hash == "start":
command="bash /home/user/startpoc/sp1_as_user.sh"
elif hash == "stop":
#command="killall poclbm.py"
command="screen -r \n exit"
else:
usage()
sys.exit(1)
for host in computer_list:
RC=BTControl(host["name"], host["user"], enc.decrypt(host["pass"]), command)
RC.start()
if __name__ == "__main__":
execute(main(sys.argv[1:]))