Thursday, October 28, 2021

[SOLVED] How to skip banner message when using ssh inside python subprocess

Issue

I understand that ssh -t -q can help to skip banner messages in terms of bash scripting. But how do I skip the banner message while using subprocess.Popen to connect the remote machine via ssh? The problem is that the banner message is captured as an error message and it shows as ERROR [ < banner message> ]

ssh = subprocess.Popen(["ssh","%s" % HOST, COMMAND],
                   shell=False,
                   stdout=subprocess.PIPE,
                   stderr=subprocess.PIPE)
result = ssh.stdout.readlines()
print "Executed on ", host
if result == [ ] :
        error = ssh.stderr.readlines()
        print >>sys.stderr, "ERROR: %s" % error
else:
        print result
    return

Solution

Use ' ssh -o LogLevel=error ' to avoid warnings and banners



Answered By - MarcoB