python
  • 安装
  • 第一个python程序
  • 基本数据类型
  • 复杂数据类型
    • Tuple
    • List
    • Slice
    • Set
    • Dictionary
  • 函数
    • 可变参数
    • 默认值参数
  • 条件判断
  • 循环
  • 模块与包
  • 类
  • SSH远程执行命令
Powered by GitBook
On this page

Was this helpful?

SSH远程执行命令

#!/usr/bin/python

import paramiko

def ssh(ip, port, username, password, cmd):
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(ip, port, username=username, password=password, timeout=20)
    stdin, stdout, stderr = client.exec_command(cmd)
    result = stdout.readlines()
    return result

if __name__=="__main__":
    ip = "192.168.0.102"
    port = "22"
    username = "root"
    password = "password"
    cmd = "echo $HOSTNAME"
    print ssh(ip, port, username, password, cmd)
Previous类

Last updated 5 years ago

Was this helpful?