def clear_ssh_tunnel(self, reason='timeout'): self.lock.acquire() error_flag = False if self.ssh_tunnel: proc_status = self.ssh_tunnel.poll() if proc_status is None: os.kill(self.ssh_tunnel.pid, signal.SIGUSR1) logger.info('ssh reverse tunnel closed. Reason: {reason}'.format(reason = reason)) elif proc_status: errors = ', '.join([line.strip() for line in self.ssh_tunnel.stderr]) if errors: logger.error('ssh exited with errors: {errors}'.format(errors=errors)) if SEND_FAULT_SMS and self.phone: pi_sms.send_sms(self.s, self.phone, 'SSH tunnel error. Log: {log}'.format(log=errors)) error_flag = True ssh_info = ', '.join([l.strip() for l in self.ssh_tunnel.stdout]) if ssh_info: logger.info('ssh event: {event}'.format(event=ssh_info)) if self.timer: self.timer.cancel() self.timer = None if ACK_SMS and self.phone and not error_flag: pi_sms.send_sms(self.s, self.phone, 'SSH tunnel closed') self.ssh_tunnel = None self.phone = None self.lock.release()
def clear_ssh_tunnel(self, reason='timeout'): self.lock.acquire() error_flag = False if self.ssh_tunnel: proc_status = self.ssh_tunnel.poll() if proc_status is None: os.kill(self.ssh_tunnel.pid, signal.SIGUSR1) logger.info( 'ssh reverse tunnel closed. Reason: {reason}'.format( reason=reason)) elif proc_status: errors = ', '.join( [line.strip() for line in self.ssh_tunnel.stderr]) if errors: logger.error('ssh exited with errors: {errors}'.format( errors=errors)) if SEND_FAULT_SMS and self.phone: pi_sms.send_sms( self.s, self.phone, 'SSH tunnel error. Log: {log}'.format(log=errors)) error_flag = True ssh_info = ', '.join([l.strip() for l in self.ssh_tunnel.stdout]) if ssh_info: logger.info('ssh event: {event}'.format(event=ssh_info)) if self.timer: self.timer.cancel() self.timer = None if ACK_SMS and self.phone and not error_flag: pi_sms.send_sms(self.s, self.phone, 'SSH tunnel closed') self.ssh_tunnel = None self.phone = None self.lock.release()
def open_reverse_ssh(self): if not self.ssh_tunnel: logger.info('Opened reverse SSH tunnel via {user}:{server}'.format(user=CLOUD_USER, server=CLOUD_SERVER)) self.ssh_tunnel = subprocess.Popen(['ssh', '-nNT', '-o', 'TCPKeepAlive=yes', '-R', '2222:localhost:22', CLOUD_USER + '@' + CLOUD_SERVER], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) if ACK_SMS and self.phone: time.sleep(10) if self.ssh_tunnel.poll() is None: pi_sms.send_sms(self.s, self.phone, 'SSH tunnel set up') self.timer = threading.Timer(SSH_PIPE_UP, self.clear_ssh_tunnel) self.timer.start()
def open_reverse_ssh(self): if not self.ssh_tunnel: logger.info('Opened reverse SSH tunnel via {user}:{server}'.format( user=CLOUD_USER, server=CLOUD_SERVER)) self.ssh_tunnel = subprocess.Popen([ 'ssh', '-nNT', '-o', 'TCPKeepAlive=yes', '-R', '2222:localhost:22', CLOUD_USER + '@' + CLOUD_SERVER ], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) if ACK_SMS and self.phone: time.sleep(10) if self.ssh_tunnel.poll() is None: pi_sms.send_sms(self.s, self.phone, 'SSH tunnel set up') self.timer = threading.Timer(SSH_PIPE_UP, self.clear_ssh_tunnel) self.timer.start()
def info(self): info = pi_sms.info(self.s) pi_sms.send_sms(self.s, self.phone, info)