python远程执行shell命令
paramiko
安装依赖库
pip install paramiko
demo.py
import paramiko
import time
try:
sshd = paramiko.SSHClient()
sshd.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sshd.connect('127.0.0.1', port='20',
username='abcde', password='123456')
stdin, stdout, stderr = sshd.exec_command('cat pwddd')
time.sleep(1)
out = stdout.read().decode('ascii').strip("\n")
err = stderr.read().decode('ascii').strip("\n")
print(out)
if err is not None:
raise Exception(err)
sshd.close()
except Exception as e:
print(e)
exit()
import paramiko
from paramiko import SSHClient
from paramiko import AutoAddPolicy
try:
sshd = SSHClient()
sshd.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sshd.connect('127.0.0.1', port='22',username='abcde')
channel = sshd.invoke_shell(width=3000)
channel.send("docker pull centos\n")
while True:
buf = channel.recv(1024)
print(buf)
if len(buf) == 0:
break
sshd.close()
except Exception as e:
print(e)
exit()