Python Forum
continue if 'subprocess.call' failes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
continue if 'subprocess.call' failes
#1
Hello!
I'm pulling out logs from remote hosts to my server for processing.
The server has a faceless account and all the Hosts have it added as an admin(admin group).
Sometimes, a new Host has a bad configuration and it asks for UserName...
I thought I can use try/except to keep going if an error or cannot log in...
for some reason, the code just stops with the message:
"The password is invalid for..."
I thought "try/except' will make it keep going but it is not:

try :
	subprocess.call("net use U: /delete /yes")
	time.sleep(1)
	subprocess.call("net use U: "+"\\\\"+itm+"\\c$\\SMTS\\TDR_Logs")
	tr = time.localtime()
	cutr = time.strftime("%Y-%m-%d %I:%M:%S", tr)
	print(f" Dir mounted -->> {cutr} -- {host_path}")            
except OSError as st :
	print(f" Error --> {st}")
	pass

Any idea on how to ignore the message ""The password is invalid for...""?
Thank you!
Reply
#2
I'm surprised your subprocess call works at all. I think you should write it as
subprocess.call(["net", "use", "U:",  "/delete",  "/yes"])
Reply
#3
I'm not sure if it is the right way to write the code but It does work.
I tried your suggestion:
subprocess.call(["net", "use", "U:",  "/delete",  "/yes"])
It works too but I still have the same message:
"The password is invalid for..."
"Enter the user name for ...."
thank you
Reply
#4
I would probably not try to parse the output, but would just try a timeout. Give the command a timeout value and see if it will kill it after some time.

subprocess.call(["net",  "use",  "U:", f"\\\\{itm}\\c$\\SMTS\\TDR_Logs"], shell=True, timeout=10)
Then be able to handle the subprocess.TimeoutExpired exception and continue or whatever you want to do when the command doesn't succeed.

Your other choice is to not use run() but Popen.communicate() directly (since you want to see what is returned before the process exits). For this particular case, I don't think that's necessary (but would be faster since you don't have to wait for an arbitrary timeout).
Reply
#5
I just tried:
subprocess.call("net use U: "+"\\\\"+itm+"\\c$\\SMTS\\TDR_Logs", shell=True, timeout=10)
it still asks for the "The password is invalid for"

Then produces errors and exits.
Last error:

AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1473, in _wait
raise TimeoutExpired(self.args, timeout)
subprocess.TimeoutExpired: Command 'net use U: \\SomeITEM\c$\SMTS\TDR_Logs' timed out after 5 seconds

Thank you.
Reply
#6
Yes, it won't stop the process from asking. But you don't have to actually answer. If you don't want to see it ask, add capture_output=True to the options.

So something like:

import subprocess

try:
    subprocess.call("net use U: "+"\\\\"+itm+"\\c$\\SMTS\\TDR_Logs", shell=True, timeout=10, capture_output=True)
except subprocess.TimeoutExpired:
    print(f"Server {itm} took too long to mount.  Skipping...")
    continue #or break or return or whatever appropriate to go to the next server....
Reply
#7
Now it is a different error and it is complaining about the "capture_output)"

Error:
AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 349, in call
with Popen(*popenargs, **kwargs) as p:
TypeError: __init__() got an unexpected keyword argument 'capture_output'

Thak you!
Reply
#8
Quote:Changed in version 3.7: Added the text parameter, as a more understandable alias of universal_newlines. Added the capture_output parameter.

Looks like capture_output becomes available in 3.7. You should consider using a more current release.

For earlier versions I think you can just open stdout/stderr directly....

import subprocess
 
try:
    subprocess.call("net use U: "+"\\\\"+itm+"\\c$\\SMTS\\TDR_Logs", shell=True, timeout=10, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
except subprocess.TimeoutExpired:
    print(f"Server {itm} took too long to mount.  Skipping...")
    continue #or break or return or whatever appropriate to go to the next server....
Reply
#9
(Aug-25-2021, 11:04 PM)bowlofred Wrote: Looks like capture_output becomes available in 3.7. You should consider using a more current release.
I think the problem is that he us call() then there is no parameter capture_output .
capture_output only in run().
Reply
#10
(Aug-25-2021, 11:38 PM)snippsat Wrote: I think the problem is that he us call() then there is no parameter capture_output .
capture_output only in run().

Ah, thank you. To avoid changing the command string I used for testing, I just modified the string in the thread and didn't notice that I was testing with run() instead of call().

I don't think there's a benefit to using call, so changing it to run should work (depending on the version available).

import subprocess
 
try:
    subprocess.run("net use U: "+"\\\\"+itm+"\\c$\\SMTS\\TDR_Logs", shell=True, timeout=10, capture_output=True)
except subprocess.TimeoutExpired:
    print(f"Server {itm} took too long to mount.  Skipping...")
    continue #or break or return or whatever appropriate to go to the next server....
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  printing out the contents aftre subprocess.call() Rakshan 3 2,742 Jul-30-2021, 08:27 AM
Last Post: DeaD_EyE
  subprocess call cannot find the file specified RRR 6 16,541 Oct-15-2020, 11:29 AM
Last Post: RRR
  Why wont subprocess call work? steve_shambles 3 2,661 Apr-28-2020, 03:06 PM
Last Post: steve_shambles
  subprocess.call - Help ! sniper6 0 1,516 Nov-27-2019, 07:42 PM
Last Post: sniper6
  Mock call to subprocess.Popen failing with StopIteration tharpa 7 5,913 Nov-08-2019, 05:00 PM
Last Post: Gribouillis
  Echo call to VLC bash using subprocess.Popen on Linux LuukS001 17 9,673 Jan-07-2019, 03:58 AM
Last Post: LuukS001
  Why is subprocess.call command not working? zBernie 5 10,727 Nov-19-2018, 11:11 PM
Last Post: snippsat
  Help with subprocess..call oldcity 1 2,540 Sep-28-2018, 03:59 PM
Last Post: volcano63
  subprocess.call salmaankamil 1 2,697 Jul-27-2018, 06:50 PM
Last Post: woooee
  [inconsistent output] subprocess.call to run cmd commands to get Kerberos ticket Yelin 2 4,987 Jun-08-2018, 09:02 AM
Last Post: Yelin

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020