Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8805137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:47:11+00:00 2026-06-14T01:47:11+00:00

I am using pexpect to handle my telnet and ssh communication. I am also

  • 0

I am using pexpect to handle my telnet and ssh communication.
I am also writing all request/response in a logfile. using pexpect.logfile(filename).

I would like to have timestamps in the logfile as well.
I can not find it anywhere in the documentation! Does anyone have any idea how to implement this functionality?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:47:13+00:00Added an answer on June 14, 2026 at 1:47 am

    After some searching I found the below code which works for me!
    Take a look at the below code:

    import logging
    import pexpect
    import re
    
    # this is the method called by the pexpect object to log
    def _write(*args, **kwargs):
        content = args[0]
        # Ignore other params, pexpect only use one arg
        if content in [' ', '', '\n', '\r', '\r\n']:
            return # don't log empty lines
        for eol in ['\r\n', '\r', '\n']:
            # remove ending EOL, the logger will add it anyway
            content = re.sub('\%s$' % eol, '', content)
        return logger.info(content) # call the logger info method with the reworked content
    
    # our flush method
    def _doNothing():
        pass
    
    logger = logging.getLogger('foo')
    hdlr = logging.FileHandler('/bar.log')
    formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
    hdlr.setFormatter(formatter)
    logger.addHandler(hdlr)
    logger.setLevel(logging.INFO)
    
    # give the logger the methods required by pexpect
    logger.write = _write
    logger.flush = _doNothing
    
    p = pexpect.spawn('echo "hello world !"', logfile=logger)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a simple pexpect script to ssh to a remote machine and grab
Is there any way of writing pexpect like small program which can launch a
Using Jenkins or Hudson I would like to create a pipeline of builds with
Using Rails 3.2.0 with haml and sass: I Would like to link an external
am using pexpect ssh to write down a script for compilation, the ssh automation
I am trying to test if a file exists over SSH using pexpect. I
I'd like to start an interactive SSH terminal from a Python script without using
Using python-requests and python-magic, I would like to test the mime-type of a web
Using the Redis info command, I am able to get all the stats of
I am trying to use pexpect to ssh into a computer but I do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.