-2

How do I use b value in 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)
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Welcome to StackOverflow. Please be more specific as to what you want. Just what output do you want? Showing an example input (including stating the "current time" for that execution) and the desired example output would help. – Rory Daulton Nov 03 '18 at 11:21
  • Your code is not valid Python 3 (the `print` function call needs parentheses). Are you sure you are not actually using Python 2? If you are only just getting started with Python, definitely use Python 3. – tripleee Jul 07 '19 at 09:13

1 Answers1

1

If I interpreted correctly, where you have %b is where you would like the value of the variable to be placed? If so, one possible solution is to concatenate the string parameter with the variable as follows:

remote_conn.send("fsclish -c ' show alarm history filter-by specific-problem 70157 from-event-time " + b + " ' \n")
Omari Celestine
  • 1,405
  • 1
  • 11
  • 21