Created
July 14, 2014 05:27
-
-
Save bot11/c63967c3a463f80b7322 to your computer and use it in GitHub Desktop.
Paramiko ssh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__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