简体   繁体   English

python命令行参数并将其作为用户名和密码在脚本中传递

[英]python command line argument and pass it as username and password in script

I am trying to write python script for logging into one of my server and do some check .我正在尝试编写 python 脚本来登录我的一台服务器并进行一些检查。 For that I want pass my url ,username and password to script .为此,我想将我的 url、用户名和密码传递给脚本。 something like script.py 1.1.1.2 admin admin .类似于 script.py 1.1.1.2 admin admin 。 i want to use IP , USERNAME and PASSWORD in my script .I can use it as print IP ??我想在我的脚本中使用IPUSERNAMEPASSWORD 。我可以将它用作打印 IP 吗? With below approach am getting credential error使用以下方法时出现凭据错误

>   File "/usr/lib/python2.6/site-packages/redfish/rest/v1.py", line
> 1064, in login
>     raise InvalidCredentialsError(delay) redfish.rest.v1.InvalidCredentialsError: 10

Here is my script这是我的脚本

import sys
from _restobject import RestObject
IP = sys.argv[1]
USERNAME = sys.argv[2]
PASSWORD = sys.argv[3]


def ex3_change_bios_setting(restobj, bios_property, property_value, \
                                                              ):

    sys.stdout.write("\nEXAMPLE 3: Change a BIOS setting\n")
    instances = restobj.search_for_type("HpBios.")

    for instance in instances:
        body = {bios_property: property_value}
        response = restobj.rest_patch(instance["href"], body, \
                                            )
        restobj.error_handler(response)

if __name__ == "__main__":

    iLO_https_url = "https://"+ IP +""
    iLO_account = PASSWORD 
    iLO_password = USERNAME

    #Create a REST object
    REST_OBJ = RestObject(iLO_https_url, iLO_account, iLO_password)
    ex3_change_bios_setting(REST_OBJ, "InternalSDCardSlot", "Disabled")

You can concatenate the string with hardcode value by + operator in python.您可以通过python中的+运算符将字符串与硬编码值连接起来。 Try this code .试试这个代码。 It works fine.它工作正常。

IP = sys.argv[1]
USERNAME = sys.argv[2]
PASSWORD = sys.argv[3]

iLO_https_url = "https://"+ IP +""
iLO_account = PASSWORD
iLO_password = USERNAME

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将目录作为命令行参数传递给 python 脚本 - Pass directory to python script as command line argument 将列表作为命令行参数传递给Python脚本 - Pass a list to a Python script as a command line argument Python 脚本 - 如何从命令行参数将密码作为变量传递 - Python script - how to pass password as variable from command line arguments 在python中使用管道将字符串列表传递给脚本中的命令行参数 - Pass a list of strings to a command line argument within a script with pipe in python 如何将字典作为命令行参数传递给 Python 脚本? - How to pass dictionary as command line argument to Python script? 如何将列表作为命令行参数传递给 python 脚本 - how to pass a list as a command line argument to a python script 将列表 object 作为命令行参数传递给 python 脚本 - pass a list object as a command line argument to a python script 将变量从python脚本传递给C程序(命令行参数) - Pass variable from python script to C program(command line argument) 如何从 docker run 命令将命令行参数传递给 python 脚本? - How to pass command line argument to python script from docker run command? PYTHON:如何将星号 (*) 作为命令行参数传递 - PYTHON: How to pass asterisk (*) as command line argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM