Skip to content

Instantly share code, notes, and snippets.

@bot11
Created July 14, 2014 05:27
Show Gist options
  • Save bot11/c63967c3a463f80b7322 to your computer and use it in GitHub Desktop.
Save bot11/c63967c3a463f80b7322 to your computer and use it in GitHub Desktop.
Paramiko ssh
__author__ = 'bot11'
'''
Hop on to the servers and make the passwordauthentication no
'''
import sys
import paramiko as pm
hosts = {'host1': '192.168.1.1',
'host2': '192.168.1.2'}
port = 22
username = 'user'
password = 'password'
passwordauthnocmd = 'sed \'s/PasswordAuthentication yes/PasswordAuthentication no/\' /etc/ssh/sshd_config > /etc/ssh/sshd_config'
sshrestartcmd = 'service sshd restart'
for host, ip in hosts.items():
try:
print "Doing the change for host : %s " % host
client = pm.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(pm.AutoAddPolicy())
client.connect(ip, port=port, username=username, password=password)
stdin, stdout, stderr = client.exec_command(sshrestartcmd)
print stdout.read(),
client.close()
except:
e = sys.exc_info()[0]
print e
continue
finally:
print "------------------------Done-----------------------\n\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment