Python Forum
Paramiko output printing issue - 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: Paramiko output printing issue (/thread-8072.html)



Paramiko output printing issue - anna - Feb-05-2018

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! ...



RE: Paramiko output printing issue - Gribouillis - Feb-05-2018

If you let the exception propagate instead of printing SSH Exception, it will give you more information on the line that caused the trouble.


RE: Paramiko output printing issue - anna - Feb-06-2018

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


RE: Paramiko output printing issue - anna - Feb-06-2018

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
===============================================================================