Python Forum
SSH connections - 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: SSH connections (/thread-3732.html)



SSH connections - Will - Jun-18-2017

Hi all,
im almost new in python.

I tried to get a connection to a cisco device and execute some commands, all fine here.

But my problem is that when I tried to do a second connection over ssh to a secondary device from the first one, it is not working .


my pc ---> ssh to device one ---> ssh to device two.

The reason that I need to do it like this is because the secondary device has an ACL to allow only traffic from connections came from device one.


What you suggest to get this done ?
What I need to do to get and execute commands into the secondary device?

thanks,
regards.

my code here



RE: SSH connections - sparkz_alot - Jun-18-2017

(Jun-18-2017, 06:11 AM)Will Wrote: ...when I tried to do a second connection over ssh

What have you tried? What do you get versus what you expect?  
Quote:... it is not working
is not very helpful.  In order to improve your odds on receiving an answer, you need to post any relevant code, the output and any error codes in their entirety (all between their respective code tags. Refer to the Help Document on how to properly post and advise on how to ask a question to better ensure a response.


RE: SSH connections - Will - Jun-19-2017

Sorry you are right, I forgot to put my code:

I tried this code an sometimes is working and you can get the result, but sometimes is not working, I can't understand how it is working sometimes and others not, I was playing with the clear buffers and timeouts and its appears to be affected to all the execution.
I don't knwo if this type of code is correct or not to jump from one device to another "both router cisco" in this case.

Thanks for your help.


jumpserver = {'device_type' : 'cisco_ios','ip' : ipNoc,'username':userNoc,'password' : passNoc}

for x in range(1,len(first_column)-1):
host=first_column[x].value
print("")
print (" SSH Session Established with " + ipNoc + " ....")
net_connect = ConnectHandler(**jumpserver)
print("")
print (" Interactive Session Established with " + ipNoc + " ....")
print ("")
net_connect.clear_buffer()
output=net_connect.find_prompt()
print("Trying to connecting from " + output + " to " + host +".test.net")
net_connect.clear_buffer()
net_connect.settimeout(None)
net_connect.write_channel("ssh -l "+ userAtt + " " + host +".test.net")
print("")
print (" SSH Session Established with " + host + " ....")
time.sleep(4)
net_connect.send_command('password' + "\n")
print("")
print (" Interactive Session Established with " + host + " ....")
print ("")
net_connect.clear_buffer()
net_connect.read_channel()
redispatch(net_connect, device_type='cisco_ios')
output=net_connect.find_prompt()
print(output)
output=net_connect.send_command('sh ver')
print(output)
time.sleep(timeout_modifier)


RE: SSH connections - sparkz_alot - Jun-19-2017

You still failed to post your code between the proper code tags. As it is, it looks as if all your indentation in wrong, leaving it a guessing game as to where the 'for' loop ends, if it ever ends. Again, refer to the Help Document for instructions on how to properly post your code, output and errors