subprocess.call 是不能作为赋值的,需要用到 subprocess.check_output 函数,而且如果要引用赋值就必须使用subprocess.call(['echo',line])这种形式。

实例一、

对于纯字符串操作如下:

In [42]: import subprocess

In [101]: subprocess.call(['kubectl','get','nodes'])                                                                                 
NAME      STATUS    ROLES     AGE       VERSION
test2     Ready     node      2d        v1.11.0

In [42]: output=subprocess.check_output(["kubectl get nodes | grep test2 | awk '{print $1}'"], shell=True)                              

In [43]: name=output.decode('utf8').strip()                                                                                          

In [44]: print(line)                                                                                                                 
test2

In [83]:                                                                                              
test2

In [102]: subprocess.call(['kubectl','label','nodes',name,'node-role.kubernetes.io/node='])
 

实例二、

对于数字操作如下:

In [6]: import subprocess                                                                                                            

In [7]: output=subprocess.check_output(["head -c 16 /dev/urandom | od -An -t x | tr -d ' '"], shell=True)                            

In [8]: print(output)                                                                                                                
b'd3f4e95e05dfe34ea87217a55fb75bac\n'

In [9]: token=str(output.decode('utf8').strip()).strip('b')                                                                          

In [10]: print(token)                                                                                                                
d3f4e95e05dfe34ea87217a55fb75bac

 

相关文章:

  • 2022-12-23
  • 2021-08-13
  • 2022-01-02
  • 2021-08-04
  • 2021-07-07
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-21
  • 2022-12-23
  • 2022-02-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-13
相关资源
相似解决方案