Skip to content

Commit e7c0a21

Browse files
committed
execute commands on remote host via SSH protocol
1 parent 301b7bd commit e7c0a21

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Created by PyCharm.
5+
File Name: LinuxBashShellScriptForOps:ssh-execute-commands-on-remote-host.py
6+
Version: 0.0.1
7+
Author: dgden
8+
Author Email: dgdenterprise@gmail.com
9+
URL: https://github.com/DingGuodong/LinuxBashShellScriptForOps
10+
Download URL: https://github.com/DingGuodong/LinuxBashShellScriptForOps/tarball/master
11+
Create Date: 2021/3/19
12+
Create Time: 11:27
13+
Description:
14+
Long Description:
15+
References:
16+
Prerequisites: []
17+
Development Status: 3 - Alpha, 5 - Production/Stable
18+
Environment: Console
19+
Intended Audience: System Administrators, Developers, End Users/Desktop
20+
License: Freeware, Freely Distributable
21+
Natural Language: English, Chinese (Simplified)
22+
Operating System: POSIX :: Linux, Microsoft :: Windows
23+
Programming Language: Python :: 2.6
24+
Programming Language: Python :: 2.7
25+
Topic: Utilities
26+
"""
27+
28+
29+
def execute_commands_on_remote_host(host, command, **kwargs):
30+
"""
31+
execute commands on remote host via SSH protocol
32+
33+
:param host: hostname or ip address
34+
:type host: str
35+
:param command: commands
36+
:type command: str
37+
:param kwargs:
38+
port: ssh port, type: int
39+
username: username, type: str
40+
key_filename: the path of ssh private key file, type: str
41+
timeout: timeout, type: int
42+
NOTE: we always recommend that using ssh key auth instead using a weak password
43+
:type kwargs:
44+
:return:
45+
:rtype:
46+
"""
47+
import paramiko
48+
49+
port = kwargs.get("port") or 22
50+
username = kwargs.get("username") or 'root'
51+
key_filename = kwargs.get("key_filename") # os.path.expanduser(r'~/.ssh/id_rsa')
52+
timeout = kwargs.get("timeout") or 5
53+
54+
client = paramiko.SSHClient()
55+
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
56+
client.connect(hostname=host, port=port, username=username, key_filename=key_filename, timeout=timeout)
57+
58+
stdin, stdout, stderr = client.exec_command(command=command,
59+
get_pty=True) # type: paramiko.ChannelStdinFile, list, paramiko.ChannelStderrFile
60+
"""
61+
warning::
62+
The server may reject this request depending on its ``AcceptEnv``
63+
setting; such rejections will fail silently (which is common client
64+
practice for this particular request type). Make sure you
65+
understand your server's configuration before using!
66+
"""
67+
for line in stdout:
68+
print "Stdout: ", line,
69+
70+
for line in stdout:
71+
print "Stderr: ", line,
72+
client.close()
73+
74+
75+
if __name__ == '__main__':
76+
execute_commands_on_remote_host("47.240.129.250", 'uname -a',
77+
port=22,
78+
username='root',
79+
key_filename=r"C:\Users\dgden\.ssh\exportedkey201310171355"
80+
)

0 commit comments

Comments
 (0)