0

I want to use python achieve following process:

[zz@bts01 ~]$ cd /opt/cdma-msc/
[zz@bts01 cdma-msc]$ ./sccli
SoftCore for CDMA CLI (c) Quortus 2010
RAN> show system
System Configuration
  Software version:      V1.31
  System name:           RAN
  System location:
  Shutdown code:
  Emergency call dest:
  Current date/time:     Tue Feb 27 14:27:41 2018
  System uptime:         20h 33m
  Auto-provisioning:     Enabled
RAN> exit
Bye.
[zz@bts01 cdma-msc]$

Please see above, I want to use python to call this /opt/cdma-msc/rancli process, which will open a secondary shell, and I want to capture the output of command "show license" in that shell. How can I achieve this in python? I tried with subprocess Popen, and was able to call the shell, but cannot enter anything into it. Anyone have some thought?

ran = subprocess.Popen(['/opt/cdma-msc/sccli'], shell = True, stdout = subprocess.PIPE)
hnb = subprocess.Popen(['show system'],stdin=ran.stdout )

above is the python module/command I tried, apparently the second line didn't take the output from first, casue it is calling another shell.

Weiqi
  • 5
  • 2
  • Show the code which didn't work so we can see where you got it wrong. – tripleee Apr 05 '18 at 16:14
  • I add two command I tried with python subprocess module, I am not sure am I on the right track. – Weiqi Apr 05 '18 at 17:05
  • The "shell" created by the CLI is not a "command shell" in the sense you imagine. It's just an interactive REPL, not a process which communicates with the calling shell's process. – tripleee Apr 05 '18 at 17:57

1 Answers1

1

You want the command to be the input to the subprocess you created, not the name of a new subprocess.

If you have a new enough Python, try

output = subprocess.run(['/opt/cdma-msc/sccli'],
    stdout = subprocess.PIPE,
    input='show system',
    check=True, universal_newlines=True).stdout

The result returned by run is an object which encapsulates the result code and status of the subprocess as well as the output from it. Its stdout member contains the text produced on standard output.

Unfortunately, run was only introduced in Python 3.5 but it's enough of an improvement that you might want to not support older versions of Python. If you have to support older versions, the equivalent code is considerably clunkier:

p = subprocess.Popen(['/opt/cdma-msc/sccli'],
    stdin=subprocess.PIPE, stdout=subprocess.PIPE)
stdout, stderr = p.communicate('show system')

Here, stdout is simply a string with the output.

You should generally avoid raw Popen() unless the higher-level wrappers absolutely cannot do what you want. There is also obviously no need for shell=True when there are no wildcards, pipelines, rediections, or even argument parsing (you already broke the command line into a list of strings).

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Maybe see also https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess – tripleee Apr 05 '18 at 17:18
  • Hi tripleee, for some reason I cannot update the python to latest. I only got python2.7 on this machine. Is there a alternative for above command in python2.7? – Weiqi Apr 05 '18 at 17:44
  • Updated for 2.7 as well, code from memory but you shoud easily find similar examples if there are typos or I forgot a parameter. – tripleee Apr 05 '18 at 17:52
  • Maybe also check if the CLI accepts command-line arguments so you don't need to talk to its `stdin` at all in batch mode; that's just a complication really. – tripleee Apr 05 '18 at 17:58
  • Hi Triplee, thank you so much for your help. Actually I just found I am using python2.6.6 instead of 2.7, but I upgrade it to 2.7 and still got some errors. – Weiqi Apr 05 '18 at 19:20
  • Traceback (most recent call last): File "hnbtest.py", line 25, in stdin=subprocess.PIPE, stdout = subprocess.PIPE) File "/opt/rh/python27/root/usr/lib64/python2.7/subprocess.py", line 211, in check_output raise ValueError('stdout argument not allowed, it will be overridden.') ValueError: stdout argument not allowed, it will be overridden. – Weiqi Apr 05 '18 at 19:20
  • Sorry I haven't had the time to get back to you properly today. I *think* you can fix it by changing `check_output` to `Popen` but I'll try to verify when I have more time. – tripleee Apr 06 '18 at 18:34
  • @Weiqi Updated now, sorry for the long delay. – tripleee Apr 10 '18 at 08:18
  • 1
    Thank you for helping me! Appreciate! @tripleee – Weiqi Apr 11 '18 at 17:49