Python Invokeshell Multiple commands Cisco
Python Invokeshell Multiple commands Cisco
I'm having issues with my script which reads from an ip from the text file. Process the IP through ssh into the device. Executes three show commands. The problem I'm having is I would think the double authentication, however when I modify the script with the authentication all it does is print the command on the line, but doesn't execute the command.
import paramiko
import getpass
import io
import os
import time
# *****Open Plain Text
#f = open ("chkpt.txt")
f = open ("ciscoasa.txt")
# *****Read & Store into Variable
ip =(f.read().splitlines())
f.close()
# *****Username Credentials
username = raw_input("Please Enter Username:")
# *****Password Options
password = getpass.getpass("Please Enter Password:")
# ***** Name of File
#name = raw_input("Please Name Your File:")
client=paramiko.SSHClient()
def connect_ssh(ip):
try:
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(ip, 22, username,
password,
look_for_keys=False, allow_agent=False)
print 'Connection Attempting to: '+(ip)
channel = client.get_transport().open_session()
channel.invoke_shell()
while channel.recv_ready():
channel.recv(1024)
channel.sendall("show asp table socket | include SSL|DTLS")
print channel.recv(1024)
channel.sendall("show processes | include Unicorn")
print channel.recv(1024)
channel.sendall("show version | include Version")
print channel.recv(1024)
#channel.sendall("cd..n")
#channel.sendall("pwdn")
#print channel.recv(1024)
#remote.send('enable')
#time.sleep(1)
#output1 = remote.recv(65535)
#print (output1)
#remote.send(password)
#time.sleep(1)
#output2 = remote.recv(65535)
#print (output2)
#remote.sendall('show version')
#time.sleep(5)
#output3 = remote.recv(9999000)
#print (output3)
print 'waiting'
print ' '
print ' '
# ***** Create File & Write Bianry to the File *****
with io.FileIO("fPython.txt", "a") as file:
file.write(output)
except Exception, e:
print '*** Caught exception: %s: %s' % (e.__class__, e)
try:
client.close()
except:
pass
# *****Create Loop through input.txt
for i in ip:
print connect_ssh(i)
Thank You
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I see commented-out code to send "cd" and "pwd". Do those work? Have you tried terminating your commands with a newline or carriage return?
– Kenster
yesterday