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 8533133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:53:18+00:00 2026-06-11T09:53:18+00:00

Im trying to write a python script using the pexpect library. What im trying

  • 0

Im trying to write a python script using the pexpect library.

What im trying to do is as follows:

ID = 'User1'
cmdrun = 'A LINUX COMMAND'
sshChild = pexpect.spawn('ssh root@192.168.0.1')
sshOut = file('sshLog.txt','w')
sshChild.logfile = sshOut
sshChild.expect('Last login:*')
sshChild.expect('Please enter your login Id')
sshChild.sendline(ID + '\r')
sshChild.sendline('ssh 192.168.21.1\r')
sshChild.expect('Last login:*')
sshChild.expect('Please enter your login Id')
sshChild.sendline(ID + '\r')
sshChild.sendline(cmdrun + '\r')

Everything works as expected up until the last line, where i do the sendline for the cmdrun. This command is a custom tcpdump command, i want the command to run indefinitely until i do a sendctrl(‘C’) to kill it and then escape the ssh tunnel.

However unless i put an interact() after the sendline(cmdrun + ‘\r’) the output from the tcpdump does not get printed.

Is there anyway i can do this without interact as i do not want interactive terminal control. What i want is to be able to wait a period of time, capturing the tcpdump output, then kill the tcpdump along with the ssh tunnel.

  • 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-11T09:53:19+00:00Added an answer on June 11, 2026 at 9:53 am

    I have never used pexpect for a long running command. But you can try the following:

    Pexpect waits for completion of the command and provides you with the output.

    sshChild.sendline(cmdrun + '\r')
    

    Since this is long running command, it is never going to give you the output unless you run

    child.interact()
    

    Can you try to have this after executing the command with appropriate timeout so that you kill the long running command after certain time and capture the output obtained till then.

    sshChild.sendline(cmdrun + '\r')
    child.expect(pexpect.EOF, timeout=20)
    print child.before, child.after
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a python script which follows the common unix command line
I'm trying to write a Python script for searching out duplicate mp3/4 files using
I'm trying to write parsing script using python/scrapy. How can I remove [] and
I am trying to write a python script that will SSH to a server
I'm trying to write a notification script using python-dbus. How can I get properties
I'm trying to execute a Python script using the Linux crontab . I want
Im trying to write a script using python language(abc.py). I need to take few
I'm trying to write a Python script that uses a particular external application belonging
I am trying to write a python script that takes record data like this
I am trying to write a python script that will change my cwd to

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.