0

I am trying to automate the deployment of a linux server using python and paramiko. The problem is that the first time I connect to this custom Linux OS it ask for parameters for it's initial "installer".

You cannot do anything with the server until these parameters are set. The first parameter is to set a new password and you are prompted from the terminal like shown below.

*WARNING: Your password has expired.
You must change your password now and login again!
New password:

I can't figure out how to just send basic user input such as a new password. From my understanding the exec_command is actually expecting linux commands not just a string of characters to pass to the terminal.

How can I simply connect up and send a raw string to the terminal to do this?

This is what I've tried, but doesn't work:

import paramiko
ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect('my_host', username='admin', key_filename='My path to private key file')

stdin, stdout, stderr = ssh.exec_command('sleep 10')

stdin.write('NewPassword')
stdin.write('NewPassword')

ssh.close()
  • See https://stackoverflow.com/q/50749745/850848 + Though a question is whether you actually get those prompts on the "exec" channel at all. Or whether the "exec" channel works at all, before you answer those prompts in an interactive "shell" session. – Martin Prikryl Nov 13 '22 at 08:22
  • Thanks Martin. I was able to figure out how to receive feedback from the channel using the .recv method. this in turn led me to the solution by using the .send method after invoking the shell. – cypherlock1010 Nov 15 '22 at 01:50

1 Answers1

0

I was able to find a solution for this using the .invoke_shell method and the .send method. Once the channel was opened I used .send to pass my inputs and then used .recv to get my feedback and ensure the installer was proceeding. I simply repeated this for all of the inputs needed. I will convert this into a function now to streamline.

import paramiko
import time

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect('my_ip_address', username='admin', key_filename='Path to my private key file')

chan = ssh.invoke_shell()
time.sleep(5)
output = chan.recv(1000)
print(output)
chan.send('New_Password\n')
time.sleep(10)
output = chan.recv(1000)
print(output)