Skip to content

Instantly share code, notes, and snippets.

@jjam3774
Last active November 16, 2018 21:43
Show Gist options
  • Save jjam3774/6149006 to your computer and use it in GitHub Desktop.
Save jjam3774/6149006 to your computer and use it in GitHub Desktop.
A python version of the ruby script that allows you to execute the same command on multiple servers.
#!/usr/bin/python
import paramiko
import getpass
class Remote():
def __init__(self, hostfile, username, commands):
self.hostfile = hostfile
self.username = username
self.commands = commands
def execute(self):
client = paramiko.SSHClient()
client.load_system_host_keys()
##########################################################
# just in case it does not recognize the known_host keys
# in the known_hosts file
##########################################################
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.password = getpass.getpass("Password: ")
for i in self.hostfile.readlines():
print("Connecting to..." + i)
client.connect(i.strip(), 22, self.username, self.password)
stdin, stdout, stderr = client.exec_command(self.commands)
for t in stdout.readlines():
print(t.strip())
for t in stderr.readlines():
print(t.strip())
#--------------------------------------------------------
commands="""
echo "##################################################";
hostname;
echo "##################################################";
uname -a;
echo "##################################################";
dmidecode -t bios
"""
#---------------------------------------------------------
username = raw_input("Username: ")
hostfile = open('hosts')
a = Remote(hostfile, username, commands)
a.execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment