Update!
I’ve added some pauses between the ssh commands as the script was working faster than the devices, and hopefully made the comments better.
This is a simple script to change the password of a local account on all of our Cleversafe devices. The host file was created with a simple Bash command:
for i in {01..69}; do echo "clsf-slr0$i"; done >> /scripts/cleversafe_hosts
I might look into getting a dynamic list directly from the Manager via the CLI or some call depending on my time.
The script is below:
#!/usr/bin/env python
#
# Will work in Python >=2.6
#
# used the following as reference:
#
# http://www.opensource.apple.com/source/lldb/lldb-69/test/pexpect-2.4/pxssh.py
# http://stackoverflow.com/questions/15823011/python-pxssh-getting-an-password-refused-error-when-trying-to
-login-to-a-rem
# http://pexpect.sourceforge.net/pxssh.html#pxssh-expect
#
# The MOST important part of the script are the following lines:
# ssh_pxssh.PROMPT = current_host + '#'
# ssh_pxssh.login(current_host, usrnm, cur_pwd, auto_prompt_reset=False)
#
# I can not change the prompt on the remote device so the pxssh process would fail
# you have to set the "auto_prompt_reset" to false.
#
# You then have to set the PROMPT attribute to the pxssh.pxssh object
# though how is not made clear, trial an error found it.
#
# I've added a pause in the password reset part as it didn't always work
# as it was going too fast
#
# requires the following packages:
# python-pip (so I could install and use PuDB)
# pexpect
#
import os, sys
import getpass
import pty
import re
import pexpect, pxssh
import time
print "This script changes Cleversafe device account passwords"
user_file = raw_input('Please supply the source host file (full path):')
usrnm = raw_input('Enter username:')
cur_pwd = getpass.getpass('Enter current password:')
new_pwd = getpass.getpass('Enter new password:')
host_file = open(user_file, "r")
for lines in host_file:
current_host = re.sub('[\'\\n]', '', lines)
print '''Currently working on....''' + current_host
try:
ssh_pxssh = pxssh.pxssh()
ssh_pxssh.PROMPT = current_host + '#'
ssh_pxssh.login(current_host, usrnm, cur_pwd, auto_prompt_reset=False)
# ssh_pxssh.sendline('help') # test command
ssh_pxssh.sendline('password') # run a command
time.sleep(2) # pause 2 secs
ssh_pxssh.sendline(cur_pwd)
time.sleep(2) # pause 2 secs
ssh_pxssh.sendline(new_pwd)
time.sleep(2) # pause 2 secs
ssh_pxssh.sendline(new_pwd)
time.sleep(2) # pause 2 secs
ssh_pxssh.prompt() # match the prompt
print(ssh_pxssh.before) # print everything before the prompt.
ssh_pxssh.logout()
except pxssh.ExceptionPxssh as e:
print("pxssh failed on login.")
print(e)
host_file.close()
