0

I'm trying to get a script together to ssh through a list of hosts and test if a certain process is running and then print out for each host whether or not the process is running. Here is an example:

from fabric.api import *
import getpass

env.user = user'
env.password = getpass.getpass(prompt='Password: ', stream=None)
env.hosts = ['localhost']


def uptime():
    run("uptime")


def getinfo():
    hostname = run("hostname")
    run("ps aux | grep -i apache") 

The problem here is that there are multiple processes named apache so it's outputting a huge list rather than just saying something to the effect of "Apache is running on host1" as it goes through the list of servers. How could I either use python to format the output this way or do it in Linux as a part of the actual shell command (ps aux | grep -i apache)?

  • Does this answer your question? [IOError: \[Errno 22\] invalid mode ('r') or filename: 'c:\\Python27\test.txt'](https://stackoverflow.com/questions/15598160/ioerror-errno-22-invalid-mode-r-or-filename-c-python27-test-txt) – Ekrem Dinçel Dec 29 '20 at 21:29

1 Answers1

0

I figured it out. The problem was that opening the file incorrectly, here's what actually did work.

from fabric.api import *
import getpass

env.user = 'user'
env.password = getpass.getpass(prompt='Password: ', stream=None)
env.hosts = ['localhost']

def uptime():
    run("uptime")

def hostname():
    run("hostname")

def getinfo():
    hostname = run("hostname")
    f = open("output.txt", "a")
    with hide('output'):
        var1 = run("ps aux | grep -i steam")
    if var1:
        print("Steam is running on " + hostname, file=f)
    elif var1:
        print("Steam is not running on " + hostname, file=f)
    
    f = open("output.txt", "a")
    with hide('output'):
        var1 = run("ps aux | grep -i pidgin")
    if var1:
        print("Pidgin is running on " + hostname, file=f)
    elif var1:
        print("Pidgin is not running on " + hostname, file=f)
    
    f = open("output.txt", "a")
    with hide('output'):
        var1 = run("ps aux | grep -i firefox")
    if var1:
        print("Firefox is running on " + hostname, file=f)
    elif var1:
        print("Firefox is not running on " + hostname, file=f)
    f.close()