【发布时间】:2014-07-13 16:16:15
【问题描述】:
我正在编写一个程序,它基本上登录到远程服务器(Linux)并执行一些命令。为此,我需要从命令行获取“服务器 IP”、“用户名”和“密码”等输入。为此使用argparse 模块。我的代码如下所示:
import sys
import argparse
import getpass
import re
from time import gmtime, strftime
import paramiko
try:
import interactive
except ImportError:
from . import interactive
def do_connect(esxi):
"""Connect to host in the esxi host"""
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='DEV Installer App')
parser.add_argument("--esxiIP", help="Mention the Esxi Host IP", required=True)
parser.add_argument("--userName", help="Enter the UserName of the Host", required=True)
parser.add_argument("--password", help="Enter the Password", required=True)
args = parser.parse_args()
我在这里要做的是,我有一个连接到远程服务器的功能,并在main() 功能中使用argparse 获取输入。我需要将这些输入参数传递给do_connect 函数。我怎样才能做到这一点?
【问题讨论】:
-
self上的参数do_connect()在这里看起来不合适;它不是一种方法,您根本没有使用该参数。
标签: python function main argparse paramiko