Python Forum
first paramiko ssh script getting getting stuck.. while run through crontab
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
first paramiko ssh script getting getting stuck.. while run through crontab
#1
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?
Reply
#2
How long does the run take? Maybe the crontab is executed when the old instance is still working.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Thanks, its completing max 5min, let me change crontab schedule
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Architecture question for website with python crontab script rockie12us 1 1,735 Aug-09-2021, 10:07 AM
Last Post: Larz60+
  run a health check script on cloud server through paramiko amritjsr 4 3,245 Jul-21-2020, 02:30 AM
Last Post: amritjsr
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,296 May-28-2020, 05:27 PM
Last Post: micseydel
  python-crontab - How to use @reboot micksulley 2 3,184 Mar-03-2020, 08:06 PM
Last Post: micksulley
  python crontab remove_all(comment="Minute*") vvarrior 1 2,776 Aug-06-2018, 12:39 AM
Last Post: Larz60+
  subprocess Popen not working via crontab nitin23c 8 12,083 Mar-21-2017, 05:39 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020