-
Notifications
You must be signed in to change notification settings - Fork 344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Connect to device via existing paramiko.Channel #1326
Comments
@svg30 Could you please share the ncclient example for this usecase to validate? |
import paramiko
from ncclient.manager import connect_ssh
JH_IP = 'jump_host_ip'
JH_NAME = 'jump_host_user'
JH_PWD = 'jump_host_pwd'
USER_NAME = 'device_user_name'
USER_PWD = 'device_password'
DEV_IP = 'device_ip'
def get_new_channel_via_jump_host(ip, local_port):
vm = paramiko.SSHClient()
vm.set_missing_host_key_policy(paramiko.AutoAddPolicy())
vm.connect(JH_IP, username=JH_NAME, password=JH_PWD)
transport = vm.get_transport()
dest_addr = (ip, 22)
local_addr = ('127.0.0.1', local_port)
return transport.open_channel("direct-tcpip", dest_addr, local_addr)
def connect_to_device(ip):
local_port = 40000
channel = get_new_channel_via_jump_host(ip, local_port)
dev_connect_params = {
'host': '127.0.0.1',
'username': USER_NAME,
'password': USER_PWD,
'port': local_port,
'sock': channel,
'device_params': {'name':'junos'},
'hostkey_verify': False
}
return connect_ssh(**dev_connect_params)
def main():
connect = connect_to_device(DEV_IP)
result = connect.command('show version', format='text')
print(result)
if __name__ == "__main__":
main() |
@svg30 Did you get the chance to refer to the example and try it? |
@dineshbaburam91 |
The ncclient library has the ability to connect to the device using a previously created connection (paramiko.Channel) by passing the desired object in the sock parameter. This is necessary, for example, if you connect via a previously created SSH tunnel. PyEZ doesn't have that option. You need to add the ability to transfer the sock parameter and use it further when opening the connection
in Device.init() add
self._sock = kvargs.get("sock", None)
in Device.open()
...
self._conn = netconf_ssh.connect(
host=self._hostname,
port=self._port,
sock_fd=self._sock_fd,
username=self._auth_user,
password=self._auth_password,
hostkey_verify=False,
key_filename=self._ssh_private_key_file,
allow_agent=allow_agent,
look_for_keys=look_for_keys,
ssh_config=self._sshconf_lkup(),
timeout=self._conn_open_timeout,
sock=self._sock,
device_params={
"name": "junos",
"local": self.class.ON_JUNOS,
"use_filter": self._use_filter,
},
)
The text was updated successfully, but these errors were encountered: