def run_conn_log(): user = load_user_info() host = user.host name = user.name password = user.password prompt = '.+' try: client = paramiko.SSHClient() client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(hostname=host, username=name, password=password) interact = SSHClientInteraction(client, timeout=10, display=False) interact.expect(prompt) interact.send(user.cmd) interact.tail(line_prefix=host + ': ', timeout=65535) except KeyboardInterrupt: print('Ctrl+C interruption detected, stopping tail') except Exception: traceback.print_exc() finally: try: client.close() except: pass
def remote_tail(self, host, port, user, passwd, logfile, webuser, filter_text=None): # 创建一个可跨文件的全局变量,控制停止 try: self.client = paramiko.SSHClient() self.client.load_system_host_keys() self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.client.connect(hostname=host, port=port, username=user, password=passwd) interact = SSHClientInteraction(self.client, timeout=10, display=False) interact.expect('.*#.*') logfile = logfile.strip().replace('&&', '').replace('||', '').replace('|', '') self.send_message(webuser, '[INFO][%s@%s]开始监控日志' % (user, host)) redis = RedisObj() redis.set('remote_tail_' + str(webuser), self.client) if filter_text: filter_text_re = filter_text.strip().replace('&&', '').replace('||', '').replace('|', '') interact.send('tail -f %s|grep --color=never %s' % (logfile, filter_text_re)) else: interact.send('tail -f %s' % (logfile)) interact.tail(output_callback=lambda m: self.send_message(webuser, m), stop_callback=lambda x: self.get_is_stop(webuser)) except Exception as e: self.send_message(webuser, e) finally: redis = RedisObj() redis.set('remote_tail_' + str(webuser), '1') try: self.client.close() except Exception as e: self.send_message(webuser, e)
def stalkLog(self, log, dir, timeToStop): cmd = "cd " + dir + ";tail -f " + log interact = SSHClientInteraction(self.ssh, timeout=10, display=False) interact.send(cmd) #def tail(self, line_prefix=None, callback=None, # output_callback=None, stop_callback=lambda x: False, # timeout=None): interact.tail(None, self.process_tail, None, self.timer)
def test_channel(): client = ParamikoClient('config.ini', 'PPM101') client.connect() interact = SSHClientInteraction(client.client, timeout=10, display=False) interact.expect(re_strings='.*#.*') interact.send('echo ""') interact.send( 'tail -f /opt/ppm/ppm/QX_DEV_OPS_962/server/kintana/log/serverLog.txt') interact.tail(output_callback=lambda m: output_callback(m, interact), stop_callback=lambda x: get_is_stop(x), timeout=100)
def main(): # Set login credentials and the server prompt hostname = 'localhost' username = '******' password = '******' prompt = 'vagrant@paramiko-expect-dev:~\$\s+' # Use SSH client to login try: # Create a new SSH client object client = paramiko.SSHClient() # Set SSH key parameters to auto accept unknown hosts client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # Connect to the host client.connect(hostname=hostname, username=username, password=password) # Create a client interaction class which will interact with the host interact = SSHClientInteraction(client, timeout=10, display=False) interact.expect(prompt) # Send the tail command interact.send('tail -f /var/log/syslog') # Now let the class tail the file for us interact.tail(line_prefix=hostname + ': ') except KeyboardInterrupt: print('Ctrl+C interruption detected, stopping tail') except Exception: traceback.print_exc() finally: try: client.close() except: pass
def main(): # Set login credentials and the server prompt hostname = "localhost" username = "******" password = "******" prompt = "fots@fotsies-ubuntu-testlab:~\$ " # Use SSH client to login try: # Create a new SSH client object client = paramiko.SSHClient() # Set SSH key parameters to auto accept unknown hosts client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # Connect to the host client.connect(hostname=hostname, username=username, password=password) # Create a client interaction class which will interact with the host interact = SSHClientInteraction(client, timeout=10, display=False) interact.expect(prompt) # Send the tail command interact.send("tail -f /var/log/auth.log") # Now let the class tail the file for us interact.tail(line_prefix=hostname + ": ") except KeyboardInterrupt: print "Ctrl+C interruption detected, stopping tail" except Exception: traceback.print_exc() finally: try: client.close() except: pass
def main(): # Set login credentials and the server prompt hostname = 'localhost' username = '******' password = '******' prompt = r'vagrant@paramiko-expect-dev:~\$\s+' # Use SSH client to login try: # Create a new SSH client object client = paramiko.SSHClient() # Set SSH key parameters to auto accept unknown hosts client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # Connect to the host client.connect(hostname=hostname, username=username, password=password) # Create a client interaction class which will interact with the host interact = SSHClientInteraction(client, timeout=10, display=False) interact.expect(prompt) # Send the tail command interact.send('tail -f /var/log/syslog') # Now let the class tail the file for us interact.tail(line_prefix=hostname+': ') except KeyboardInterrupt: print('Ctrl+C interruption detected, stopping tail') except Exception: traceback.print_exc() finally: try: client.close() except Exception: pass