#!/usr/bin/env python
#coding=utf-8
#python setup.py build
#python setup.py install
# install PyCrypto
#ssh = paramiko.SSHClient()
#ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#ssh.connect(host,port,user,passwd)
import paramiko
host = '192.168.233.130'
port = 22
user = 'root'
passwd = 'toor'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#发送主机,端口,用户,密码
ssh.connect(host, port, user, passwd)
#执行shell命令
stdin, stdout, stderr = ssh.exec_command("ls /root")
#显示执行结果
print stdout.read()
stdin, stdout, stderr = ssh.exec_command("df")
print stdout.read()
stdin, stdout, stderr = ssh.exec_command("uptime")
print stdout.read()
stdin, stdout, stderr = ssh.exec_command("netstat -ntl|grep 22")
print stdout.read()
#关闭连接
ssh.close()
#下载文件
t = paramiko.Transport((host,port))
t.connect(username=user, password=passwd)
sftp = paramiko.SFTPClient.from_transport(t)
#远程文件
remotepath = '/root/xorg.log'
#本地目录
localpath = 'C:\\Users\\Silence\\Desktop\\xorg.log'
#执行下在
sftp.get(remotepath, localpath)
#关闭连接
t.close()
#上传文件
#发送主机,端口
t = paramiko.Transport((host, port))
#发送帐户
t.connect(username=user, password=passwd)
sftp = paramiko.SFTPClient.from_transport(t)
#远程目录
remotepath='/root/test.org'
#本地文件
localpath='C:\\Users\\Silence\\Desktop\\test.log'
#上传文件
sftp.put(localpath, remotepath)
#关闭连接
t.close()
python学习--paramiko
最新推荐文章于 2024-04-16 12:03:35 发布