1

I have the following codes using pexpect in linux it is ok.

import pexpect 

child = pexpect.spawn('ssh test@ip' % (susername, ip) , encoding='utf-8')
child.logfile = open("{}/{}.txt".format(folder, ip),"w")

when have tried to change to wexpect to be used in windows but the logfile is empty my goal is to write everything the wexpect spawn into a logfile from start to end

i have tried

import wexpect

logging =""
child = wexpect.spawn('ssh -c aes256-cbc %s@%s' % (username, ip) , encoding='utf-8' , logfile=logging)
print(logging)
child.logfile = open("{}/{}.txt".format(folder, ip),"w")

but print is empty as well.

All other wexpect functions seems to be working fine, I am able to use .sendline, .expect function.

checking if anyone know how to get the logging to work don't seems to have many documentation on it

enzo
  • 9,861
  • 3
  • 15
  • 38
Andy
  • 11
  • 3

1 Answers1

0

I looked at the wexpect repo, and the only code that handles the logfile argument is in the legacy module (the read_nonblocking() and send() methods in the spawn_windows class), so it doesn't looks like logfile works right now.

I tried pexpect.popen_spawn.PopenSpawn, but I had SSH problems. May I suggest you try paramiko?

NOTE

  • Tested on Windows 11, using Python 3.9 and Parmiko 2.9
  • Added aes256-cbc to my remote VM's sshd_config to match your connection. BTW, you can place multiple ciphers in the Transport tuple, as long as they're on both devices (I use Get-TlsCipherSuite | Format-Table -Property CipherSuite, Name on Windows).
import paramiko

ip_address = "192.168.x.xxx"
username = "stack"
password = "**********"

list_of_commands = ["which ssh", "date", "whoami", ]

paramiko.Transport._preferred_ciphers = ("aes256-cbc", )
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip_address, username=username, password=password)
for c in list_of_commands:
    print("Command:", c)
    stdin, stdout, stderr = ssh.exec_command(c)
    print("Output:\n", stdout.read().decode())
print("Script complete. Have a nice day.")

Output:

Command: which ssh
Output:
 /usr/bin/ssh

Command: date
Output:
 Tue 11 Jan 2022 06:36:34 PM EST

Command: whoami
Output:
 stack

Script complete. Have a nice day.
Rob G
  • 673
  • 3
  • 10
  • the reason for using pexpect or wexpect module, i will need to detect regex return string before entering the next command. eg: show tech will take a long time to finish running before entering the next command thanks for the help to recommend paramiko – Andy Jan 12 '22 at 13:37