import sys, socket
import pickle
+import paramiko
+import os
from Util import *
-MSGLEN = 1024
-HOST = "127.0.0.1"
-PORT = 10001
-
-class MySocket:
- def __init__(self, sock=None):
- if sock is None:
- self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- else:
- self.sock = sock
+from XMLParser import *
+paramiko.util.log_to_file('/tmp/paramiko.log')
- def connect(self, host, port):
- self.sock.connect((host, port))
- print "connected to %s %s"%(host, port)
+MSGLEN = 1024
- def send_msg(self, msg):
+class Commander:
+ def __init__(self):
+ self.sshc = paramiko.SSHClient()
+ self.sshc.load_system_host_keys()
+ self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+
+ def start_daemon(hostname, username, password, comm):
+ self.sshc.connect(hostname=hostname, username=username, password=password)
+ stdin, stdout, stderr = self.sshc.exec_command(comm)
+ self.sshc.close()
+
+
+ def send(self, msg):
totalsent = 0
while totalsent < len(msg):
sent = self.sock.send(msg[totalsent:])
totalsent = totalsent + sent
def send_command(self, msg_type, config_data):
- self.send_msg(msg_type)
+ self.send(msg_type)
response = self.recv_msg()
if response == ACK_MSG:
- print "aici"
-
- self.send_dict(config_data)
- print "am trimis"
+ self.send_msg(pickle.dumps(config_data))
response = self.recv_msg()
return response
- def recv_msg(self):
- msg = ''
- chunk = self.sock.recv(MSGLEN)
- if chunk == '':
- raise RuntimeError, "socket connection broken"
- msg = msg + chunk
- print msg
- return msg
+ #~ def recv_msg(self):
+ #~ msg = ''
+ #~ chunk = self.sock.recv(MSGLEN)
+ #~ if chunk == '':
+ #~ raise RuntimeError, "socket connection broken"
+ #~ msg = msg + chunk
+ #~ print msg
+ #~ return msg
- # send a pickled dictionary
- def send_dict(self, data):
- dumped_data = pickle.dumps(data)
- self.send_msg(dumped_data)
+class TestCommander:
+ def __init__(self):
+ nodes = Nodes("nodes.xml");
+ swarm = Swarm("swarm.xml");
if __name__ == "__main__":
s = MySocket()
s.connect(HOST, PORT)
- # test 1
- #s.send_msg("hello")
- # test 2
- #dd = {'a':1, 'b':2}
- #s.send_dict(dd)
- #s.recv_msg()
# test config
config_data = [{CLIENT:"tribler", FILE:"Tribler/Tools/cmdline.py",