简体   繁体   中英

How to login to Linux with Python script?

I need to log in with a user in order to run a Python script. I need to execute a command that cannot be executed by another user other than the one I want to scale.

This is the code.

!/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)

But to be able to execute the command that says sapcontrol I must be logged in with a user but how could I escalate

Since your examples just call popen to do this. I'd solve this by running the commands/script via ssh.

See: How to use SSH to run a shell script on a remote machine?

Or: https://unix.stackexchange.com/questions/474533/get-output-of-this-command-from-another-server-via-ssh

Set up set up SSH with automatic login via public/private key so that when your local python script runs you're not prompted to login.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM