I am a beginner in terms of netmiko, I found out that I can use it to send commands via ssh to other Linux machines. I have done a lot of progress, but it is a bug that I can not understand, everything is going well, but when I trying to configure something is terminating the connection without finishing. I am doing this in parallel processing and as you can see here the fist error, terminating the connection without finishing:
Configure Apache
sudo su
root@beef-server:/home/beef#
sudo apt install -y software-properties-common 2>&1 | tee -a /var/log/remote_instalation/instalation.log
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
But sometimes is not going even to this part as you can see it in the second process.
The code is like this:
Main file, which is taking commands from the user:
if __name__ == '__main__':
print('1) File configuration \n2) Commands \n ')
choose = int(input('Please select the mode that you want to input the commands: '))
# print(choose)
if choose==1 :
filename = input('Please input the filename with the full path: ')
params = get_hosts(filename)
print(params)
with concurrent.futures.ProcessPoolExecutor() as executor:
try:
results = [executor.submit(default_instalation,params[param]) for param in params ]
except Exception as e:
print(e)
for result in results:
print(result)
The default instalation file:
from netmiko import linux
from .instalation.add_repositories import AddRepository
from .instalation.apache.install_apache import intall_apache
from .instalation.apache.configure_apache import AppacheConfiguration
from .instalation.rabbitmq.rabbitmq import rabbitmq
from .instalation.etcd.etcd import etcd
def default_instalation(param)->str:
device = linux.LinuxSSH(
ip=param['ip'],
username=param['username'],
password=param['password'],
secret=param['secret'],
device_type=param['device_type'],
verbose=True,
keepalive=1,
)
outputen = device.enable(cmd="sudo su",pattern="password")
print(outputen)
outputen = device.send_command("sudo mkdir /var/log/remote_instalation")
print(outputen)
output = device.send_command('set -e 2>&1 | tee -a /var/log/remote_instalation/instalation.log')
print(output)
add_repo = AddRepository(conn=device)
print(add_repo.add_repositories())
apache_conf = AppacheConfiguration(conn=device)
print(intall_apache(device))
print(apache_conf.configure_apache())
AddRepository class:
from netmiko import linux
class AddRepository:
def __init__(self, conn):
self.conn = conn
def add_repositories(self)->str:
print ("Adding repositories")
commands = ['sudo apt install -y software-properties-common 2>&1 | tee -a /var/log/remote_instalation/instalation.log',
'sudo add-apt-repository cloud-archive:antelope -y 2>&1 | tee -a /var/log/remote_instalation/instalation.log'
]
print(self.conn.send_config_set(commands))
return "[+] Repository has been added"
ApacheConfiguration class:
class AppacheConfiguration:
def __init__(self, conn):
self.conn = conn
def configure_apache(self)->str:
print("Configure Apache")
outputen = self.conn.enable(cmd="sudo su",pattern="password")
print(outputen)
commands = [
"""echo "ServerName controller" >>/etc/apache2/apache2.conf 2>&1 | tee -a /var/log/remote_instalation/instalation.log""",
"""sudo cat >/etc/apache2/sites-available/keystone.conf <<END
A lot of configuration that works
END
""",
"""a2ensite keystone.conf 2>&1 | tee -a /var/log/remote_instalation/instalation.log""",
]
print(self.conn.send_config_set(commands))
self.conn.send_command("""sudo systemctl restart apache2.service 2>&1 | tee -a /var/log/remote_instalation/instalation.log""",cmd_verify=False)
return ("[+] Apache has been configured")
Now the issue is the "a2ensite keystone.conf 2>&1 | tee -a /var/log/remote_instalation/instalation.log" is not even executed.
Do you have any suggestion what can I do in this situation?