0

I want to copy CLI command output to table then save as data frame

 DD-NN-KK-OOLL(config)#display vlan 0000
 -------------------------------------------------------------------
 Index  Type   State   F S P    VPI  VCI   FLOWTYPE FLOWPARA   LABEL
 -------------------------------------------------------------------
 324  vdl    down    0 3 62   0    35    -        -            390
 452  vdl    down    0 3 62   -    -     -        -            390
 613  vdl    up      0 4 49   0    35    -        -            453
 614  vdl    up      0 4 49   -    -     -        -            453
 617  vdl    down    0 2 26   0    35    -        -            278
 618  vdl    down    0 2 26   -    -     -        -            278
 619  vdl    up      0 2 30   0    35    -        -            282
 620  vdl    up      0 2 30   -    -     -        -            282
 621  vdl    up      0 4 62   0    35    -        -            466
 622  vdl    up      0 4 62   -    -     -        -            466
 627  vdl    down    0 2 20   0    35    -        -            272
 628  vdl    down    0 2 20   -    -     -        -            272

The output I need

Type State F S P VPI VCI LABEL
vdl down 0 3 62 0 35 390
vdl down 0 3 62 - - 390
vdl up 0 4 49 0 35 453
vdl up 0 4 49 - - 453
vdl down 0 2 26 0 35 278
vdl down 0 3 62 - - 268
  • Does this answer your question? [Running shell command and capturing the output](https://stackoverflow.com/questions/4760215/running-shell-command-and-capturing-the-output) – tevemadar May 29 '23 at 11:12
  • The link is useful to capture the output, I need also to save as a table – asmaa mahmoud May 29 '23 at 11:33

1 Answers1

1

You can use :

import subprocess
from io import StringIO
​
s = subprocess.check_output(["xclip", "-o"]).decode("UTF-8") #change the command here
​
df = (
    pd.read_csv(StringIO(s), sep="\s\s*", skiprows=2, engine="python").loc[1:]
          #.drop(columns=["Index", "FLOWTYPE, "FLOWPARA"]) # is this optional ? If not, uncomment
)

​ Output :

print(df)

   Index Type State    F    S     P VPI VCI FLOWTYPE FLOWPARA  LABEL
1    324  vdl  down  0.0  3.0  62.0   0  35        -        -  390.0
2    452  vdl  down  0.0  3.0  62.0   -   -        -        -  390.0
3    613  vdl    up  0.0  4.0  49.0   0  35        -        -  453.0
4    614  vdl    up  0.0  4.0  49.0   -   -        -        -  453.0
5    617  vdl  down  0.0  2.0  26.0   0  35        -        -  278.0
..   ...  ...   ...  ...  ...   ...  ..  ..      ...      ...    ...
8    620  vdl    up  0.0  2.0  30.0   -   -        -        -  282.0
9    621  vdl    up  0.0  4.0  62.0   0  35        -        -  466.0
10   622  vdl    up  0.0  4.0  62.0   -   -        -        -  466.0
11   627  vdl  down  0.0  2.0  20.0   0  35        -        -  272.0
12   628  vdl  down  0.0  2.0  20.0   -   -        -        -  272.0

[12 rows x 11 columns]
Timeless
  • 22,580
  • 4
  • 12
  • 30