指定蟒蛇參數如果調用蟒蛇帶參數,即如何除零參數
python script.ps arg1, arg2, arg2
sys.argv中[0]是script.ps,腳本
我如何引用的名稱並保存除第0個參數以外的python腳本的所有參數的值。
這裏是我的腳本
import sys, subprocess, socket, string
import wmi, win32api, win32con
for args in [item.strip('sender-ip=') for item in sys.argv[1:]]:
userIP = args
userloggedon = ""
# perform system lookup of IP address
userIP = "\\\\" + userIP
pst = subprocess.Popen(
["D:\pstools\psloggedon.exe", "-l", "-x", userIP],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
)
out, error = pst.communicate()
userLoggedOn = out.split('\n')[1].strip()
print 'userId={}'.format(userLoggedOn)
這個腳本完美的作品,如果我發送器IP是唯一的參數,即
python script.ps sender-ip=10.10.10.10
,但如果我調用它
python script.ps [email protected], sender-ip=10.10.10.10
或
python script.ps [email protected] sender-ip=10.10.10.10
或
python script.ps "[email protected], sender-ip=10.10.10.10"
你的想法......
這是行不通的,因爲它不能提取發送器IP的IP地址。
我期望的輸出是
userId=DOMAIN\username
,而不是我得到
userId=
或
userId=
userId=DOMAIN\username
你或許應該看看['argparse'(https://docs.python.org/dev/library/argparse.html)。 – jonrsharpe
你調用腳本的方式爲什麼不使用kwargs呢? –
如果我的答案解決了您的問題,請接受它,以便可以關閉問題。 –