1

I want to connect to a device via SSH with Paramiko (python library) but my device needs 2 step connection as below picture:

enter image description here

here is my code :

import paramiko
ssh_client =paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname='10.240.235.167',username='cli', password='')


stdin, stdout, stderr = ssh_client.exec_command('admin')
stdin, stdout, stderr = ssh_client.exec_command('Admin123\n')
stdin.write('admin123\n')
stdin,stdout,stderr=ssh_client.exec_command('my command')

but it's not working, I want to know how to connect and store result of my command.

Morteza Hasanabadi
  • 212
  • 1
  • 2
  • 12
  • what is not working? Please share the error or stacktrace you have got. – Shashank V Jan 01 '20 at 14:16
  • UserWarning: implicit cast from 'char *' to a different pointer type: will be forbidden in the future (check that the types are as you expect; use an explicit ffi.cast() if they are correct) self._backend._ffi.from_buffer(data), len(data) [] – Morteza Hasanabadi Jan 01 '20 at 14:25
  • my problem is first I should login as cli and I don't know how to implement it with paramiko – Morteza Hasanabadi Jan 01 '20 at 14:51
  • ssh_client.connect(hostname='cli@10.240.235.167',username='admin', password='Admin123') this will bring: for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno 11003] getaddrinfo failed – Morteza Hasanabadi Jan 01 '20 at 14:51
  • 'cli@10.240.235.167` is not a hostname. Use the code from your question -- Your server possibly uses `None` authentication "type". Paramiko does not support that directly. See [Paramiko - Bad Authentication Type [Cisco SG-300 Switch\]](https://stackoverflow.com/q/53595644/850848). --- If you need more help, you will will have to provide us more information, including logs files (or both Paramiko and some SSH client) – Martin Prikryl Jan 01 '20 at 15:32
  • actually connection is ok after below line ssh_client.connect(hostname='10.240.235.167',username='cli', password='') but I don't know how to enter username and password after this line. – Morteza Hasanabadi Jan 01 '20 at 17:16
  • That's what my answer to the question linked in my first comment above shows. – Martin Prikryl Jan 01 '20 at 17:23
  • that's right @MartinPrikryl, but my result is still empty after this change ```python import paramiko ssh_client =paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh_client.connect(hostname='10.240.235.167',username='cli', password='') print ('running remote command') channel = ssh_client.invoke_shell() channel.send("admin" + "\n") channel.send("Admin123" + "\n") stdin,stdout,stderr=ssh_client.exec_command("show config common licenses" + "\n") print (stdout.readlines()) ``` result: running remote command [] – Morteza Hasanabadi Jan 01 '20 at 17:38
  • 1
    My answer also explains that you cannot use `exec_command` and shows that you need to send the command to the "shell". – Martin Prikryl Jan 01 '20 at 17:54
  • ok, now I'm use channel.send(command + "\n") , how about answer? should I use channel.recv()? how can I store the result in a variable? for example with telnetlib I was able to store result with below code. status = telnetlib.Telnet(HOST,23,5).read_all().decode('ascii') – Morteza Hasanabadi Jan 01 '20 at 18:04
  • 1
    I have added an example code to my answer. – Martin Prikryl Jan 01 '20 at 18:14
  • Yes, your example fixed my problem. just another question, when I use it in a multi thread way the results will be incomplete and seems that there is a problem with time.sleep(5). can I ask it from you in another subject? – Morteza Hasanabadi Jan 04 '20 at 08:46
  • Post a new question with [mcve]. – Martin Prikryl Jan 04 '20 at 16:15

0 Answers0