python3使用ssh连接服务器–执行命令

我的环境:win10(64bit)、python3.6.6(64bit)

  1. 安装paramiko

    pip install paramiko

2、建立ssh连接

ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(ssh_server,ssh_port,ssh_user,ssh_password)

  1. 执行命令

    示例:

#检查有没有文件夹

stdin,stdout,stderr = ssh.exec_command(‘find /home/www)

result = stdout.read().decode(‘utf-8’)

如果有查询到就会返回值否则为空

  1. 断开连接

ssh.close()

  1. 使用捕获异常

    try:

    Except Exception as e:

    print(e)

闻道有先后 术业有专攻

From: shaynerain.com

By: Shaynerain

发布了123 篇原创文章 · 获赞 59 · 访问量 168万+

猜你喜欢

转载自blog.csdn.net/shaynerain/article/details/102970040