Forum Discussion

loveen_355250's avatar
loveen_355250
Icon for Nimbostratus rankNimbostratus
Jun 30, 2018

F5 root password change via python script issues

i am changing root password for mutiple f5's in my environment with a python script , somehow the script is not working as expected , i believe connection.recv is not able to get enough buffer here

 

script

import paramiko import threading import time import re import sys

 

def Cisco_smart_install(ip): username = 'admin' password = 'XXXXXXX' session = paramiko.SSHClient() session.set_missing_host_key_policy(paramiko.AutoAddPolicy()) session.connect(ip,username = username, password = password) connection = session.invoke_shell() time.sleep(3) connection.send('tmsh\n') time.sleep(2) connection.send('modify auth password root\n') time.sleep(20) connection.send('XXXXXXX\n') time.sleep(20) connection.send('XXXXXXX\n') time.sleep(3) connection.send('\n') time.sleep(4) connection.send('save sys config\n') time.sleep(4) router_output = connection.recv(65535) time.sleep(5) print router_output print " " print " " print " End of output " print " " print " " connection.send('quit\n') time.sleep(2) connection.send('exit\n')

 

this is the file from where i am reading ip address

thread1 = [] filename = 'ip_file.txt' file = open(filename,'r') x = file.read().splitlines() for host_name in x: th1 = threading.Thread(target = Cisco_smart_install,args = (host_name,)) th1.start() time.sleep(3) thread1.append(th1)

 

for th1 in thread1: th1.join()

 

file.close()

 

out put i am getting after script run

[admin@XXXXXXXX1:/S1-green-P:Active:Standalone] ~ tmsh admin@(XXXXXXXXX)(cfg-sync Standalone)(/S1-green-P:Active)(/Common)(tmos) modify auth password root changing password for root new password:

 

End of output

 

  • Anesh's avatar
    Anesh
    Icon for Cirrostratus rankCirrostratus

    i would suggest using the REST API available with F5 devices for automation, instead of using paramiko, you can use paramiko in use cases were a REST API is not availbe, for changing the root password an API is already availble you can refer to this

     

    Sample code below:

     

    >>> import requests
    >>> import json
    >>> requests.packages.urllib3.disable_warnings()
    >>> b = requests.session()
    >>> b.auth = ('admin', 'password')
    >>> b.verify = False
    >>> b.headers.update({'Content-Type':'application/json'})
    >>> payload = { }
    >>> payload['oldPassword'] = "password"
    >>> payload['newPassword'] = "password1"
    >>> b.post('https://192.168.190.130/mgmt/shared/authn/root', data=json.dumps(payload))