Community discussions

MikroTik App
 
Artnet44
just joined
Topic Author
Posts: 7
Joined: Tue Jan 12, 2021 5:56 pm

Paramiko - Regenerate ssh key / Python Automation

Wed Aug 10, 2022 6:52 pm

Hello,

I was wondering if anyone has a workaround for dealing with "confirmation" in routeros while using paramiko to execute commands?
I built a simple tool to quickly run through multiple units and restrict services + set strong crypto to yes and increase host-key-size.

The problem is this script seems to fail with: 'ip ssh regenerate-host-key' due to requiring the user to say "Y/N" in a second part of the command.
Here is a redacted piece of the current code:

Edit: Added comments in green if anyone wants to run the code to check.

import paramiko
import os
import time
import logging

ip = r'Absolute path to iplist.txt'
iplist=[line.strip() for line in open('iplist.txt filename')
print(iplist)

def EsxCli(ipaddress):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
client.connect(hostname = ipaddress , port='SSH port', username='login username',password='login password',look_for_keys=False)
stdin,stdout,stderr = client.exec_command('system print identity')
output_read = stdout.read()
print("Securing: ")
print(output_read)

stdin,stdout,stderr = client.exec_command('ip ssh set allow-none-crypto=no strong-crypto=yes host-key-size=4096')
stdin,stdout,stderr = client.exec_command('ip ssh regenerate-host-key')

#Code stops working here, have tried variations of combining "Y" to the regenerate-host-key command but this does not seem to work.
def StopVsfwd():
for j in iplist:
print(j)
EsxCli(j)
StopVsfwd()
Last edited by Artnet44 on Wed Aug 10, 2022 7:05 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Paramiko - Regenerate ssh key / Python Automation  [SOLVED]

Wed Aug 10, 2022 7:02 pm

stdin,stdout,stderr = client.exec_command('[:execute "/ip ssh regenerate-host-key"]')
 
Artnet44
just joined
Topic Author
Posts: 7
Joined: Tue Jan 12, 2021 5:56 pm

Re: Paramiko - Regenerate ssh key / Python Automation

Wed Aug 10, 2022 7:13 pm

stdin,stdout,stderr = client.exec_command('[:execute "/ip ssh regenerate-host-key"]')
wow that was quick...
Thanks a lot rextended for sharing your knowledge! :)

Who is online

Users browsing this forum: adimihaix, Ellaham and 30 guests