Python Forum
Paramiko output printing issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Paramiko output printing issue
#1
Here is my first ssh script, facing stdout printing issue
import socket
socket.setdefaulttimeout(5)
import paramiko
import sys
import threading
import os
import time
ip        = '10.124.117.12'
user      = 'ops'
passwd    = 'BN123'

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
   ssh.connect(ip,username=user,password=passwd, timeout=5)
   print "Connected to %s" % ip
   time.sleep(5)
   connection = ssh.invoke_shell()
   stdin, stdout, stderr = ssh.exec_command("show chassis\n")
   result = stdout.read().decode('ascii').strip("\n")
   ssh.close()
   print result
except paramiko.AuthenticationException:
       print "[-] Authentication Exception! ..."
except paramiko.SSHException:
       print "[-] SSH Exception! ..." 

Error as below

python DnCollector.py
Connected to 10.124.117.188
No handlers could be found for logger "paramiko.transport"
[-] SSH Exception! ...
Reply
#2
If you let the exception propagate instead of printing SSH Exception, it will give you more information on the line that caused the trouble.
Reply
#3
import socket
socket.setdefaulttimeout(5)
import paramiko
import sys
import threading
import os
import time
ip        = '10.124.117.12'
user      = 'ops'
passwd    = 'BN123'

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
   ssh.connect(ip,username=user,password=passwd, timeout=5)
   print "Connected to %s" % ip
   time.sleep(5)
   connection = ssh.invoke_shell()
   stdin, stdout, stderr = ssh.exec_command("show chassis\n")
   result = stdout.read().decode('ascii').strip("\n")
   ssh.close()
   print result
except (socket.error,paramiko.AuthenticationException,paramiko.SSHException) as message:
        print "ERROR: SSH connection to "+ip+" failed: " +str(message)
        sys.exit(1)
except paramiko.SSHException:
       print "[-] SSH Exception! ..."
showing as below error

Connected to 10.124.117.12
No handlers could be found for logger "paramiko.transport"
ERROR: SSH connection to 10.124.117.12 failed: (1, 'Administratively prohibited')
however I am able to telnet from command prompt


ssh -l ops 10.124.117.12

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! **** WARNING: UNAUTHORISED ACCESS PROHIBITED **** !
! You are about to log on to a proprietary system where access is provided, !
! by the Owner of the system, only to authorized users. !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

[email protected]'s password:

*A:BNG_01# logout
Reply
#4
import paramiko
import sys
import threading
import os
import time
ip        = '10.124.117.12'
user      = 'ops'
passwd    = 'NG123'
cmd = 'show chassis'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
   ssh.connect(ip,username=user,password=passwd, timeout=5,allow_agent=False)
   print "Connected to %s" % ip
   time.sleep(5)
   output = ''
   connection = ssh.invoke_shell()
   transport = ssh.get_transport()
   transport.set_keepalive(30)
   if connection.send_ready():
      #print ("Channel is ready")
      connection.send(cmd)
   else:
      raise("Channel not ready!")
   buff=''
   while not buff.endswith('# '): # Checking for returned prompt
         resp = connection.recv(4096)
         buff += resp
   print resp
   #stdin, stdout, stderr = ssh.exec_command(cmd)
   #result = stdout.read().decode('ascii').strip("\n")
   #paramiko.util.log_to_file("filename.log")
   #print(result)
   ssh.close()
   #print result
except (socket.error,paramiko.AuthenticationException,paramiko.SSHException) as message:
        print "ERROR: SSH connection to "+ip+" failed: " +str(message)
        sys.exit(1)

#except paramiko.AuthenticationException:
#       print "[-] Authentication Exception! ..."
except paramiko.SSHException:
       print "[-] SSH Exception! ..."
printing only below output
Connected to 10.124.117.12
*A:BNG_01#


expected output

===============================================================================
System Information
===============================================================================
Name : BNG_01
Type : 7753
Chassis Topology : Standalone
Location : Mumbai
Coordinates : (Not Specified)
CLLI code :
Number of slots : 12
Oper number of slots : 12
Number of ports : 44
Critical LED state : Off
Major LED state : Off
Minor LED state : Off
Over Temperature state : OK
Base MAC address : 40:08:7d:1f:88:01
Admin chassis mode : d
Oper chassis mode : d
Mixed mode : Disabled
Fabric Speed : 10 Gig

===============================================================================
Chassis Summary
===============================================================================
Chassis Role Status
-------------------------------------------------------------------------------
1 Standalone up
===============================================================================
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Output from Paramiko exec_command from channel pemca 0 2,517 Dec-03-2021, 12:29 PM
Last Post: pemca
  Printing output without print usage susmith552 1 1,625 Feb-07-2020, 07:52 PM
Last Post: Clunk_Head
  How do I stop this fast factorization program from printing beyond the 3rd output? Pleiades 6 3,777 Dec-07-2019, 08:43 PM
Last Post: Pleiades
  multithreading issue with output mr_byte31 4 3,147 Sep-11-2019, 12:04 PM
Last Post: stullis
  Output issue twinpiques 6 3,094 Jul-29-2019, 11:24 PM
Last Post: Yoriz
  re.finditer issue, output is blank anna 1 2,335 Feb-07-2019, 10:41 AM
Last Post: stranac
  ciscolib cdp output list printing support anna 3 3,283 Jul-25-2018, 12:18 PM
Last Post: buran
  easysnmp output printing help anna 0 2,774 Apr-03-2018, 09:19 AM
Last Post: anna
  Output not printing when execute scripts in threads anna 1 2,701 Mar-21-2018, 05:08 PM
Last Post: woooee
  telnet unexpected output printing anna 3 3,537 Jan-17-2018, 01:16 PM
Last Post: anna

Forum Jump:

User Panel Messages

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