Last active
August 29, 2015 14:16
-
-
Save imtapps/8ca304693cdba5085f4b to your computer and use it in GitHub Desktop.
Execute shell script on remote system
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
import paramiko | |
import os | |
def main(environment): | |
ssh_config = paramiko.SSHConfig() | |
with open(os.path.expanduser('~/.ssh/config')) as f: | |
ssh_config.parse(f) | |
ssh_params = ssh_config.lookup(environment) | |
ssh = paramiko.SSHClient() | |
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
privatekey = ssh_params['identityfile'][0] | |
mkey = paramiko.RSAKey.from_private_key_file(privatekey) | |
ssh.connect(ssh_params['hostname'], username=ssh_params['user'], pkey=mkey) | |
stdin, stdout, stderr = ssh.exec_command( | |
""" | |
#!/bin/bash | |
until [ $(ps -Ao pid,cmd | grep celeryd-vector | grep -v grep | awk '{print $1}' | wc -l) -eq 0 ]; do | |
echo "Celery is running, killing" | |
sudo kill -9 $(ps -Ao pid,stat,cmd | grep celeryd-vector | grep -v grep | awk '{print $1}') | |
echo "Wait for 5 seconds" | |
sleep 5s | |
done | |
echo "Starting Celery" | |
sudo service celeryd-vector start | |
""" | |
) | |
ssh.close() | |
if __name__ == '__main__': | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-e', '--environ', type=str, required=True, help='Host name from ~/.ssh/config') | |
args = parser.parse_args() | |
main(args.environ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment