摘要
本人最近遇到的一些有关于定期执行的服务器上的脚本的问题,于是本人通过的利用python中的paramiko架包来实现对远程的一个控制。执行需要制定的脚本的命令。
一、paramiko的安装
pip install paramiko
二、paramiko的简单使用
#!/usr/bin/env python
import socket
import paramiko
paramiko.util.log_to_file('paramiko.log')
hostname = '192.168.25.138'
username = 'root'
password = 'xjl'
def connect_remote_invoke_cmd():
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=hostname, username=username, password=password)
stdin, stdout, stderr = client.exec_command('docker images')
output_date = str(stdout.read())[2:-1].split("\\n")
for data in output_date:
print(data)
print("----------------------------------------------------------------------------")
stdin, stdout, stderr = client.exec_command('docker search mysql')
result = (str(stdout.read(), encoding="utf-8"))[2:-1].split("\\n")
for data in result:
print(data)
print("----------------------------------------------------------------------------")
stdin, stdout, stderr = client.exec_command('docker ps -a')
result = (str(stdout.read(), encoding="utf-8"))[2:-1].split("\\n")
for data in result:
print(data)
client.close()
if __name__ == '__main__':
connect_remote_invoke_cmd()
三、paramiko的扩展使用
- paramiko模块exec_command()函数是将服务器执行完的结果一次性返回给你;
- invoke_shell()函数类似shell终端,可以将执行结果分批次返回,看到任务的执行情况,不会因为执行一个很长的脚本而不知道是否执行成功。
#!/usr/bin/env python
import socket
import paramiko
import timer
paramiko.util.log_to_file('paramiko.log')
hostname = '192.168.25.138'
username = 'root'
password = 'xjl'
def connect_remote_invoke_cmd():
"""
连接远程服务器并执行的cmd命令
:return:
"""
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=hostname, username=username, password=password)
stdin, stdout, stderr = client.exec_command('docker images')
output_date = str(stdout.read())[2:-1].split("\\n")
for data in output_date:
print(data)
print("----------------------------------------------------------------------------")
stdin, stdout, stderr = client.exec_command('docker search mysql')
result = (str(stdout.read(), encoding="utf-8"))[2:-1].split("\\n")
for data in result:
print(data)
print("----------------------------------------------------------------------------")
stdin, stdout, stderr = client.exec_command('docker ps -a')
result = (str(stdout.read(), encoding="utf-8"))[2:-1].split("\\n")
for data in result:
print(data)
client.close()
def exec_command_test():
"""
连接远程服务器并将服务器执行完的结果一次性返回给你;
:return:
"""
cmd=""
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=hostname, username=username, password=password)
stdin, stdout, stderr = client.exec_command(cmd)
result = (str(stdout.read(), encoding="utf-8"))[2:-1]
return result
def invoke_shell_test():
"""
连接远程服务器并将服务器执行完的并查询返回的结果
:return:
"""
cmd = ""
client = paramiko.SSHClient()
# 允许将信任的主机自动加入到host_allow 列表,此方法必须有,且必须放在connect方法的前面,否则报错
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
client.connect(hostname=hostname, username=username, password=password)
# 激活terminal
channel=client.invoke_shell()
# 向服务器发送commond
channel.send(cmd+"\n")
while True:
timer.set_timer(1)
# 接受服务器的返回值
stoud = channel.recv(1024)
# 查询返回的执行结果
print(stoud.decode('utf-8'))
if __name__ == '__main__':
connect_remote_invoke_cmd()