Python Forum
first paramiko ssh script getting getting stuck.. while run through crontab - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: first paramiko ssh script getting getting stuck.. while run through crontab (/thread-9082.html)



first paramiko ssh script getting getting stuck.. while run through crontab - anna - Mar-20-2018

Hi All,

My first ssh script to collect output, output file is almost 100000 lines, its runing ok through prompt however
while runing through crontab, its creating multiple processes.

contrab entry

Quote:/bin/sh -c (cd /home/anna/scripts/; /usr/bin/python /home/anna/scripts/bulkdn.py >/dev/null 2>&1)

import paramiko
import time
import re
import os
def disable_paging(remote_conn):
    remote_conn.send("environment no more\n")
    time.sleep(1)

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

    return output


if __name__ == '__main__':


    # VARIABLES THAT NEED CHANGED
    ip = '10.14.17.138'
    username = 'anna'
    password = 'test123'

    # 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(ip, username=username, password=password, look_for_keys=False, allow_agent=False)
    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"

    # Turn off paging
    disable_paging(remote_conn)

    # Now let's try to send the router a command
    remote_conn.send("\n")
    remote_conn.send("environment no more\n")
    #remote_conn.send("show version\n")
    remote_conn.send("show service id 1 ppp session \n")
    # Wait for the command to complete
    finalOutput = " "
    time.sleep(10)
    while True:
          output = remote_conn.recv(500)

          if "sessions:" in output:
             break
          else:
              finalOutput += output

    file = open('/home/anna/scripts/bulkdn.txt','w')
    file.write(finalOutput)
    file.close()
is there any issue in script?


RE: first paramiko ssh script getting getting stuck.. while run through crontab - DeaD_EyE - Mar-20-2018

How long does the run take? Maybe the crontab is executed when the old instance is still working.


RE: first paramiko ssh script getting getting stuck.. while run through crontab - anna - Mar-27-2018

Thanks, its completing max 5min, let me change crontab schedule