采用如下方式,

  1. 首先创建SSHClient对象,ssh

2.使用ssh对象,调用connect函数建立连接

3.调用exec_command函数,在远程机执行相关操作

4.关闭连接

#!/usr/bin/env python
#-*- coding:utf-8  -*-

import paramiko

HostIP = '192.168.1.1'
username = 'root'
passwd = '******'


def run():
    try:
       ssh = paramiko.SSHClient()
       ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
       ssh.connect(HostIP,22,username,passwd)

       ssh.exec_command('echo "test" >> /a.txt')
       print "check status %s OK\n" %HostIP
       ssh.close()
    except Exception as ex:
       print "\tError %s\n"% ex


if __name__ == '__main__':
  print "begin"
  run()
Logo

更多推荐