-1

I’m trying to automate ssh connection. In this session i want to send some commands to log on into another subproces.

import paramiko
import os
import time 


ip = 'a'
port = 22
username = 'b' 
password = 'c'

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,port,username,password)

channel = ssh.invoke_shell()

out = channel.recv(9999)

channel.send('net-client\n')
channel.send('connect\n')
channel.send('yes\n')
channel.send('password\n')
channel.send('help\n')

while not channel.recv_ready():
    time.sleep(3)

out = channel.recv(9999)
print(out.decode("ascii"))

Manual preparation:

  • $(prompt from ssh connection)]
  • $ net-client (when i write this command, it open client installed on machine)
  • & connect (I have another prompt because i'm in subprocces, I write 'connect')
  • Are you sure you want to continue connecting (yes/no)? yes (write 'yes) Interactive SSH Authentication Type your password:
  • Password: 'password' (i write password)
  • & (now i'm connected in subproces)

Actual result: I have correct answer after "net-client' but when I write next command (in this case 'connect'), this will be written on ssh-connection not i subproces...

I want to continue send commands in this subproces( so put command like connect, password etc.) but my script can't handle this operation.

This my actual print from script:

$
net-client
"Correct answer to the command"
& connect
yes
password

help
4

1 回答 1

0

最后我找到了我的答案。

解决方案很简单:空格 '\n' 只是移动到新行。在输出中,我看到了我已发送但缺少“按 Enter”的命令。需要将其更改为 '\r' - 回车,然后所有命令都被进程“接受”。

于 2019-01-18T09:32:06.597 回答