python模拟shell执行脚本
工作时候需要模拟shell来执行任务,借助包paramkio
import paramiko
class ShellExec(object):
host = \'127.0.0.1\'
port = 3600
user = \'username\'
pkey = \'/home/username/.ssh/id_rsa\' #
def __init__(self):
self._connect()
def _connect(self):
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.ssh.load_system_host_keys()
self.ssh.connect(self.host, self.port, self.user, self.pkey)
def execute(self, cmd):
stdin, stdout, stderr = self.ssh.exec_command(cmd)
return stdout.read(), stderr.read()