How to use sockets to send user and password to a devboard, using SSH?

余生长醉 提交于 2020-06-17 09:15:47

问题


I am using sockets to connect to Coral devboard. I want to connect to the devboard and execute a classification script. Whenever I execute my code (lines below) , it shows "In [3]:" for around 10 seconds as if waiting for something. Then "In [4]" appears. What is it happening? I already sent the user and password.

This is my code:

import socket

TCP_IP = '172.16.1.11'
TCP_PORT = 22
BUFFER_SIZE = 1024


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))

s.send(b'login')
print(s.recv(BUFFER_SIZE).decode())
s.send(b'mendel')
print(s.recv(BUFFER_SIZE).decode())

s.send(b'Password')
print(s.recv(BUFFER_SIZE).decode())
s.send(b'password')
print(s.recv(BUFFER_SIZE).decode())

THIS IS WHAT IS SHOWN IN THE CONSOLE

In [3]: runfile('C:/Users/Coral/Classification/demo_socket.py', wdir='C:/Users/Coral/Classification')
SSH-2.0-OpenSSH_7.9p1 Debian-10+deb10u2




In [4]:

EDIT

When I am using:

ssh mendel@172.16.1.11

I am already talking to the SSH server. Whay can't I just write the login and password using python instead of writing it with my fingers?


回答1:


I solved it using "paramiko".

import paramiko

#Server's data
IP = '172.16.2.47'
PORT = 22
USER = 'mendel'
PASSWORD = 'mendel'

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname = IP, port=PORT, username = USER, password = PASSWORD)


stdin, stdout, stderr = ssh.exec_command('hostname -I')
output = stdout.readlines()
type(output)
print('\n'.join(output))
'''




来源:https://stackoverflow.com/questions/62388913/how-to-use-sockets-to-send-user-and-password-to-a-devboard-using-ssh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!