|
@@ -0,0 +1,29 @@
|
|
|
+import paramiko
|
|
|
+
|
|
|
+
|
|
|
+def run_cmd(ssh_client, cmd):
|
|
|
+ """
|
|
|
+ 运行单条命令
|
|
|
+ :param ssh_client:
|
|
|
+ :param cmd:
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ # bash -l -c解释:-l(login)表示bash作为一个login shell;-c(command)表示执行后面字符串内的命令,这样执行的脚本,可以获取到/etc/profile里的全局变量,包括我们搜索命令的目录PATH
|
|
|
+ print("执行命令: " + cmd)
|
|
|
+ stdin, stdout, stderr = ssh_client.exec_command(cmd)
|
|
|
+ res, err = stdout.read(), stderr.read()
|
|
|
+ result = res if res else err
|
|
|
+ result = result.decode('utf-8')
|
|
|
+ print("运行结果: " + result)
|
|
|
+ return result
|
|
|
+
|
|
|
+
|
|
|
+def get_client(json):
|
|
|
+ """获取sshClient"""
|
|
|
+ ssh_client = paramiko.SSHClient()
|
|
|
+ ssh_client.load_system_host_keys()
|
|
|
+ ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
|
+
|
|
|
+ ssh_client.connect(hostname=json.get('host'), port=int(json.get('port')), username=json.get('username'),
|
|
|
+ password=json.get('password'), timeout=int(json.get('timeout')))
|
|
|
+ return ssh_client
|