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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:39:44+00:00 2026-05-16T06:39:44+00:00

The code below runs grep in one machine through SSH and prints the results:

  • 0

The code below runs grep in one machine through SSH and prints the results:

import sys, os, string
import paramiko

cmd = "grep -h 'king' /opt/data/horror_20100810*"

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('10.10.3.10', username='xy', password='xy')
stdin, stdout, stderr = ssh.exec_command(cmd)
stdin.write('xy\n')
stdin.flush()

print stdout.readlines()

How can I grep five machines all at once (so that I don’t have major delay), than put all that in five variables and print them all out.

  • 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-05-16T06:39:45+00:00Added an answer on May 16, 2026 at 6:39 am

    You’ll need to put the calls into separate threads (or processes, but that would be overkill) which in turn requires the code to be in a function (which is a good idea anyway: don’t have substantial code at a module’s top level).

    For example:

    import sys, os, string, threading
    import paramiko
    
    cmd = "grep -h 'king' /opt/data/horror_20100810*"
    
    outlock = threading.Lock()
    
    def workon(host):
    
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(host, username='xy', password='xy')
        stdin, stdout, stderr = ssh.exec_command(cmd)
        stdin.write('xy\n')
        stdin.flush()
    
        with outlock:
            print stdout.readlines()
    
    def main():
        hosts = ['10.10.3.10', '10.10.4.12', '10.10.2.15', ] # etc
        threads = []
        for h in hosts:
            t = threading.Thread(target=workon, args=(h,))
            t.start()
            threads.append(t)
        for t in threads:
            t.join()
    
    main()
    

    If you had many more than five hosts, I would recommend using instead a “thread pool” architecture and a queue of work units. But, for just five, it’s simpler to stick to the “dedicated thread” model (especially since there is no thread pool in the standard library, so you’d need a third party package like threadpool… or a lot of subtle custom code of your own of course;-).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below gets the username/password and runs it thru the backend.php script. <?php
This is my code below. The program runs fine. I don't get any exceptions.
below i have a code that runs in most of my simple programs ..
I am creating a dynamic array inside a function. The code (posted below) runs
I'm trying to extend one of my WebApps using IronPython, unfortunately the code below
When the link below is clicked <a class=end href=#>End</a> The below code runs which
The code below errors out (undefined method `org_id' for nil:NilClass). When the query runs
This code below runs the destinations area on this my homepage. However it repeats
This is very strange! The below code runs correctly when F5'ed in VS2010 but
Below is the output of my page after it runs the below code: I'm

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.