def connect_server(self, host): try: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect((host, self.PORT)) except socket.error, error : Log.logerr('socket failed: %d(%s)' % (error.errno, error.strerror)) return False
def new_conn_thread_func(conn, addr): print addr try: handler = ConnHandler(conn) handler.do_work() except Exception, error: Log.logerr('svr handler thread exception: %d(%s)' % (error.errno, error.strerror))
def do_cmds(self, msg): if (msg.startswith("recv")): return self.recv_file(msg) elif (msg.startswith("send")): local_file = msg.split('#')[1] return self.send_file(local_file, local_file, True) elif (msg.startswith("cmd")): cmd,shell = self.parse_cmd_args(msg) ctl = Command() if (shell == 'shell'): ret_info = ctl.run_sys_cmd(cmd) return self.send_msg(self.sock, Message.MSG_CTRL, ret_info) elif (shell == 'update'): args = sys.argv[:] args.insert(0, sys.executable) os.chdir(os._startup_cwd) msg = 'success' self.send_msg(self.sock, Message.MSG_CTRL, msg) self.sock.close() Log.logmsg("Update self:%s,%s" % (args, os._startup_cwd)) os.execv(sys.executable, args) #no return else: Log.logerr("Invalid cmd!%s" % msg) return False
def read_config(self, file_name): if not os.path.exists(file_name): Log.logerr("CONF:%s not exists,using default." % file_name) return False tree = xml.etree.ElementTree.ElementTree(file=file_name) root = tree.getroot() self.svr_conf_dict['listen'] = int(root.findall ('server/listen')[0].text) self.svr_conf_dict['backlog'] = int(root.findall ('server/backlog')[0].text) return True
def init_sock(self): try: port = self.conf.svr_conf_dict['listen'] back_log = self.conf.svr_conf_dict['backlog'] self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.set_close_exec(self.sock.fileno()) self.sock.bind(('', port)) self.sock.listen(back_log) except socket.error, error : Log.logerr('server init socket failed: %s(%s)' % (socket.error, error)) return False
def do_work(self): while (True): header = self.readn(self.sock, 8) if not header: return True version, machine, type, length = Message.parse_msg_header(header) if (version != Message.version or machine != Message.TYPE_HYDRA): Log.logerr("Server check message header failed!") return False msg = self.readn(self.sock, length) if (type == Message.MSG_CTRL): Log.logmsg("Server try execute cmd :%s" % msg) if self.do_cmds(msg): Log.logmsg("Server execute cmd :%s, success" % msg) return True else: Log.logmsg("Server execute cmd :%s, success" % msg) else: Log.logerr("unknown cmd:%d %s" % (type, msg)) return False
rc = False try: if (method == "get"): local_file = sys.argv[4] remote_file = sys.argv[3] rc = client.cli_get_file(local_file, remote_file) elif (method == "put"): local_file = sys.argv[3] remote_file = sys.argv[4] rc = client.cli_put_file(local_file, remote_file) elif (method == "cmd"): shell = sys.argv[3] if shell == "shell": cmd = ' '.join(sys.argv[4:]) rc = client.cli_cmd(cmd, shell) elif shell == "update": cmd = '' rc = client.cli_cmd(cmd, shell) else: print "invalid args", ' '.join(sys.argv[:]) else: print "invalid args:%s" % (' '.join(sys.argv[:])) except Exception, error: err_msg = 'exception: %d(%s)' % (error.errno, error.strerror) Log.logerr(err_msg) if not rc: sys.exit(1) else: sys.exit(0)
os.dup2(null.fileno(), sys.stdin.fileno()) os.dup2(null.fileno(), sys.stdout.fileno()) os.dup2(null.fileno(), sys.stderr.fileno()) return True def run_svr(): svr = Server() svr.run() if __name__ == "__main__": PIDFILE = "/var/run/agent.pid" WORKPATH = "/" os._startup_cwd = os.getcwd() if not g_conf.read_config(Config.CONF_FILE): Log.logerr("read config failed!") sys.exit(1) import getopt,sys try: opts,args = getopt.getopt(sys.argv[1:], "d", ["daemon",]) except getopt.GetoptError,e: print e sys.exit(1) for o,a in opts: if o in ("-d","--daemon"): if not createDaemon(PIDFILE, WORKPATH): Log.logerr("Daemon failed!") sys.exit(1) run_svr()