I have a perl one liner that appends a string at the end of a particular line and it works when I run the one liner on the host. For instance, I'm looking to append 'boy857sr xxxxxxx' at the end of a line that starts with 'AllowUsers'
perl -pi.bak -e 's/^(AllowUsers.*)/\1 boy857sr xxxxxxx/g;' /etc/ssh/sshd_config
Works perfectly!
AllowUsers boy857bt soladmin svc-Orion_SAM solarw boy857lj boy806nk boy806sk boy857sr xxxxxxx
But when I run it inside a python script, it wipes out the 'AllowUsers' line and add another line, look below:
#RSAAuthentication yes
^A kn857sr xxxxxxx
Here is the python script.
import pxssh
my_list=[
"server121"
]
for i in my_list:
s = pxssh.pxssh(timeout=15, maxread=2000000)
s.SSH_OPTS += "-o StrictHostKeyChecking=no"
s.SSH_OPTS += "-o UserKnownHostsFile=/dev/null"
s.login(i,'username','password')
print (i)
s.sendline('/usr/local/bin/sudo su -')
s.expect('Password: ')
s.sendline('xxxxxxx')
s.prompt()
print s.before
s.sendline('perl -pi.bak -e \'s/^(AllowUsers.*)/\1 boy857sr xxxxxxx/g;\'
/etc/ssh/sshd_config')
s.prompt()
print s.before
s.sendline('exit')
s.logout()