如何在python中的函数中使用varibale

时间:2018-11-03 10:45:59

标签: python python-3.x

我想在b中使用remote_conn.send值。谁能帮忙使用它?

import paramiko
import time

import datetime

a=datetime.datetime.now() - datetime.timedelta(minutes=15)

b= a.strftime("%y-%m-%d.%H:%M:%S")

print b

def disable_paging(remote_conn):
    '''Disable paging on a Cisco router'''

    remote_conn.send("terminal length 0\n")
    time.sleep(1)

    # Clear the buffer on the screen
    output = remote_conn.recv(1000000)

    return output

AMGW28='/home/SIVA/843cc/MGW28aaa.txt'

MGW28='10.145.96.172'


if __name__ == '__main__':

 def  AMG(HOST,filenametoSave):

    # VARIABLES THAT NEED CHANGED
    ip = 'HOST`'
    username = 'root'
    password = 'Encrypt@25login'

    # 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)
    print "SSH connection established to %s" % ip

    # Use invoke_shell to establish an 'interactive session'
    remote_conn = remote_conn_pre.invoke_shell()
    print "Interactive SSH session established"

    # Strip the initial router prompt
    #output = remote_conn.recv(1000)

    # See what we have
    #print output

    # Turn off paging
    #disable_paging(remote_conn)

    # Now let's try to send the router a command


    a=datetime.datetime.now() - datetime.timedelta(minutes=15)

    b= a.strftime("%y-%m-%d.%H:%M:%S")

    print b

   remote_conn.send("fsclish -c ' show alarm history filter-by specific-problem 70157 from-event-time %b ' \n")


    # Wait for the command to complete
    time.sleep(1)

    output = remote_conn.recv(5000000)
    print output


    f=open(filenametoSave,'a+')
    f.write(output)
    f.close()




AMG(MGW28,AMGW28)

1 个答案:

答案 0 :(得分:1)

如果我的解释正确,那么您想要放置变量值的地方是%b?如果是这样,一种可能的解决方案是按如下所示将string参数与变量连接:

remote_conn.send("fsclish -c ' show alarm history filter-by specific-problem 70157 from-event-time " + b + " ' \n")