-
Notifications
You must be signed in to change notification settings - Fork 6
/
ssh_paramiko_automate.py
executable file
·61 lines (50 loc) · 1.84 KB
/
ssh_paramiko_automate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python2
"""
Author :Julio Sanz
Website :www.elarraydejota.com
Email :juliojosesb@gmail.com
Description :Script to automate the execution of a list of tasks in remote server/s
Just pass the server/s IP or hostname as an argument
The same credentials are used for all servers, so it is assumed that
the same user/password is used in all of them to connect via SSH
Dependencies :paramiko
Usage :ssh_paramiko_automate.py [ IP/hostname ]
Examples :./ssh_paramiko_automate.py 192.168.2.156
./ssh_paramiko_automate.py 192.168.2.156 192.168.2.30 server3
License :GPLv3
"""
#
# IMPORTS
#
import paramiko, getpass, sys
#
# VARIABLES
#
# Add to the list of boring tasks you want to send to the
# remote server/s. This is just an example:
remote_commands = [ 'updatedb', 'ls -l /home/jota', \
'/opt/wordpress/wp_backup.sh', '/opt/wordpress/post_backup.sh' ]
#
# FUNCTIONS
#
def connect_ssh():
#v_remoteserver = raw_input("Remote server IP/hostname: ")
v_username = raw_input("Username: ")
v_password = getpass.getpass("Password: ")
for v_remoteserver in sys.argv[1:]:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(v_remoteserver, username=v_username, password=v_password)
print "============================================================================"
print "--------========Executing commands in server "+v_remoteserver+"========---------"
for order in remote_commands:
print "_"*60
print "Executing command ---> "+order+"\n"
stdin, stdout, ssh_stderr = ssh.exec_command(order)
print (stdout.read())
ssh.close()
#
# MAIN
#
if __name__ == '__main__':
connect_ssh()