[英]How to login to Linux with Python script?
我需要以用戶身份登錄才能運行 Python 腳本。 我需要執行一個命令,該命令不能由我想要擴展的用戶以外的其他用戶執行。
這是代碼。
!/usr/bin/python
import os, sys
daemon1=os.popen("sapcontrol -nr 00 -function GetProcessList | grep hdbpreprocessor | awk '{print $1}'").readline().strip()
Daemon that belong to SAP HANA
status1=os.popen("sapcontrol -nr 00 -function GetProcessList | grep hdbpreprocessor | awk '{print $4}'").readline().strip()
Actual process id
id1=os.popen("sapcontrol -nr 00 -function GetProcessList | grep hdbpreprocessor | awk '{print $11}'").readline().strip()
if (status1 == "GREEN," and daemon1 == "hdbpreprocessor,"):
print "Process %s it's running in process ID : %s - process status : %s." % (daemon1, id1, status1)
sys.exit(0)
if (status1 == "YELLOW," and daemon1 == "hdbpreprocessor,"):
print "WARNING - Process %s is %s it's going up" % (daemon1, status1)
sys.exit(1)
elif (status1 == "GRAY" and daemon1 == "hdbpreprocessor,"):
print "CRITICAL - Process %s is %s it's not going to go up" % (daemon1, status1)
sys.exit(2)
else:
print "UNKNOWN - %s is %s .We can not identify process status" % (daemon1, status1)
sys.exit(3)
但是為了能夠執行說 sapcontrol 的命令,我必須以用戶身份登錄,但我怎么能升級
由於您的示例只是調用 popen 來執行此操作。 我會通過 ssh 運行命令/腳本來解決這個問題。
請參閱: 如何使用 SSH 在遠程計算機上運行 shell 腳本?
設置 SSH 通過公鑰/私鑰自動登錄,這樣當您的本地 python 腳本運行時,不會提示您登錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.