下面是python函数,每个函数都会在大多数python函数中使用相同的重复行(超过200个函数)。
因此我计划将其作为模块编写并将其作为模块导入或任何其他建议都可以。
以下是每个函数中出现的重复行。
CASE ipsum.states
WHEN 'complete' THEN 'done'
WHEN 'error' THEN 'failure'
WHEN 'begin' THEN 'sending'
ELSE 'not started'
END as state
第二个功能示例:
xw.Range('A1').formula = SUM(1,3) -> want to retain this formula
在上面的函数中,是由另一个脚本调用。 注意:下面的行不是每次都在重复使用。
def abc(username, password, host, a, b, c, d, e):
# VARIABLES THAT NEED CHANGED
# Create instance of SSHClient object
remote_conn_pre = paramiko.SSHClient()
# Automatically add untrusted hosts (make sure okay for security policy in your environment)
remote_conn_pre.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
# initiate SSH connection
remote_conn_pre.connect(host, username=username, password=password, look_for_keys=False, allow_agent=False)
print "SSH connection established to %s" % host
# Use invoke_shell to establish an 'interactive session'
remote_conn = remote_conn_pre.invoke_shell()
# Send some commands and get the output
和每个用函数写入不同参数的函数(参数个数不同)。示例如下
def xyz(username, password, host, a, b):
# VARIABLES THAT NEED CHANGED
# Create instance of SSHClient object
remote_conn_pre = paramiko.SSHClient()
# Automatically add untrusted hosts (make sure okay for security policy in your environment)
remote_conn_pre.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
# initiate SSH connection
remote_conn_pre.connect(host, username=username, password=password, look_for_keys=False, allow_agent=False)
print "SSH connection established to %s" % host
# Use invoke_shell to establish an 'interactive session'
remote_conn = remote_conn_pre.invoke_shell()
# Send some commands and get the output
以上函数用于建立ssh连接使用paramiko。
答案 0 :(得分:0)
您可能只需将其作为单个函数,并将命令的名称和args一起传递给函数。像这样:
def run_remote(username, password, host, command, *command_args):
# Create instance of SSHClient object
remote_conn = paramiko.SSHClient()
# Automatically add untrusted hosts (make sure okay for security policy in your environment)
remote_conn.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
# initiate SSH connection
remote_conn.connect(host,
username=username,
password=password,
look_for_keys=False,
allow_agent=False)
print "SSH connection established to %s" % host
# execute a remote command and return stdin, stdout, stderr
args_as_str = ' '.join([pipes.quote(arg) for arg in command_args])
return remote_conn.exec_command("{} {}".format(command, args_as_str)