Python Forum
using paramiko to see resualt
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using paramiko to see resualt
#1
Hello,
I'm trying to connect using SSH and send some commands
how can I see the result of the command ?
for example
 
answer = ssh.exec_command('ls Documents/ ')
print (answer)

I get
(<paramiko.ChannelFile from <paramiko.Channel 1 (open) window=2097152 -> <paramiko.Transport at 0xa267a440 (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>, <paramiko.ChannelFile from <paramiko.Channel 1 (open) window=2097152 -> <paramiko.Transport at 0xa267a440 (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>, <paramiko.ChannelFile from <paramiko.Channel 1 (open) window=2097152 -> <paramiko.Transport at 0xa267a440 (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>)
what do I need to in order to see the command replay ? (which is in this case - a list of the files inside the folder )

Thanks ,
Reply
#2
How about reading the exec_command()'s documentation to see what it returns?
Reply
#3
for some reason I thought printing is enough and not read line in the stdout

thank you!
Reply
#4
Answer to wrong topic or the op has changed something?
I don't know what happened :-D


The answer is related to: How to get the IP-Address from Windows.


The easiest approach to obtain the IP-Address is:
import socket


def get_ip():
    return socket.gethostbyname(socket.gethostname())


print(get_ip())
pexpect does not support Windows.
You could use wexpect.

I don't know if this works. I think the better approach is to use subprocess if you want to parse it by yourself.

Easier is to use netifaces.
But this does not work out of the box. A compiler is required.
You could download an unofficial compiled binary here: https://www.lfd.uci.edu/~gohlke/pythonlibs/#netifaces
This is actually (2022/12) only precompiled for Python 3.4 - 3.9

Much easier and platform independent is psutil.
import socket

import psutil


def get_ipv4():
    result = []
    for name, properties in psutil.net_if_addrs().items():
        for property in properties:
            if property.family == socket.AF_INET and property.ptp is None:
                result.append((name, property.address))

    return result


if __name__ == "__main__":
    for name, ip in get_ipv4():
        print(name, "->", ip)
The property.ptp is None if it is not a VPN connection or something else ptp based.
The names of the interfaces are not unique, and they also depend on language settings.

A network interface card could have more than one ip. This is why properties is a list. The objects in properties (the values of the resulting dict) are custom objects, where you can access the attributes like address, broadcast, .. etc.

To get more details if the interface is wireless or not, pywin32 or ctypes must be used.
If your Windows Desktop has only one NIC, you're lucky and do not require this detail.
If you know the name of the interface, you could filter for it. But then this code does not work, if you change the name.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
thank you
I can use this in another app I'm building

Thanks!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020