(转)python的paramiko模块
python的paramiko模块
>>> import paramiko
>>> a = paramiko.Transport((“127.0.0.1″,2222))
>>> a.connect(username=”root”, password=’123456′)
>>> sftp = paramiko.SFTPClient.from_transport(a)
>>> localpath=’ftp-test.log’
>>> remotepath=’/data/ftp-test.log’
>>> sftp.put(localpath,remotepath)

#!/usr/bin/python
import paramiko
import os,sys
ssh_host = sys.argv[1]
ssh_port = 22
user = 'root'
password = 'xxxxxx'
cmd = sys.argv[2]
paramiko.util.log_to_file('/tmp/test') #使用paramiko记录日志
s = paramiko.SSHClient() #绑定一个实例
s.load_system_host_keys() #加载known_hosts文件
s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #远程连接如果提示yes/no时,默认为yes
s.connect(ssh_host,ssh_port,user,password,timeout=5) #连接远程主机
stdin,stdout,stderr = s.exec_command(cmd) #执行指令,并将命令本身及命令的执行结果赋值到标准办入、标准输出或者标准错误
cmd_result = stdout.read(),stderr.read() #取得执行的输出
for line in cmd_result:
print line
s.close()

pkey_file = '/home/breeze/.ssh/id_rsa'
key = paramiko.RSAKey.from_private_key_file(pkey_file)
s.connect(host,port,username,pkey=key,timeout=5)
stdin,stdout,stderr = s.exec_command(cmd)

#!/usr/bin/python
import os,sys
import paramiko
host = sys.argv[1]
rfilename = sys.argv[2]
lfilename = os.path.basename(rfilename)
user = 'root'
password = 'xxxx'
paramiko.util.log_to_file('/tmp/test')
t = paramiko.Transport((host,22))
t.connect(username=user,password=password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.get(rfilename,lfilename)
#sftp.put('paramiko1.py','/tmp/paramiko1.py')
t.close()


#!/usr/bin/python
import socket
import sys
# windows does not have termios...
try:
import termios
import tty
has_termios = True
except ImportError:
has_termios = False
def interactive_shell(chan):
if has_termios:
posix_shell(chan)
else:
windows_shell(chan)
def posix_shell(chan):
import select
oldtty = termios.tcgetattr(sys.stdin)
try:
tty.setraw(sys.stdin.fileno())
tty.setcbreak(sys.stdin.fileno())
chan.settimeout(0.0)
while True:
r, w, e = select.select([chan, sys.stdin], [], [])
if chan in r:
try:
x = chan.recv(1024)
if len(x) == 0:
print '\r\n*** EOF\r\n',
break
sys.stdout.write(x)
sys.stdout.flush()
except socket.timeout:
pass
if sys.stdin in r:
x = sys.stdin.read(1)
if len(x) == 0:
break
chan.send(x)
finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
# thanks to Mike Looijmans for this code
def windows_shell(chan):
import threading
sys.stdout.write("Line-buffered terminal emulation. Press F6 or ^Z to send EOF.\r\n\r\n")
def writeall(sock):
while True:
data = sock.recv(256)
if not data:
sys.stdout.write('\r\n*** EOF ***\r\n\r\n')
sys.stdout.flush()
break
sys.stdout.write(data)
sys.stdout.flush()
writer = threading.Thread(target=writeall, args=(chan,))
writer.start()
try:
while True:
d = sys.stdin.read(1)
if not d:
break
chan.send(d)
except EOFError:
# user hit ^Z or F6
pass


#!/usr/bin/python
#_*_coding:utf8_*_
import paramiko
import interactive
#记录日志
paramiko.util.log_to_file('/tmp/test')
#建立ssh连接
ssh=paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.128.82',port=22,username='root',password='cheyian')
#建立交互式shell连接
channel=ssh.invoke_shell()
#建立交互式管道
interactive.interactive_shell(channel)
#关闭连接
channel.close()
ssh.close()

(转)python的paramiko模块的更多相关文章
- Python之paramiko模块
今天我们来了解一下python的paramiko模块 paramiko是python基于SSH用于远程服务器并执行相应的操作. 我们先在windows下安装paramiko 1.cmd下用pip安装p ...
- 使用python的Paramiko模块登陆SSH
使用python的Paramiko模块登陆SSH paramiko是用Python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接. python的paramiko模块 ...
- 利用python 下paramiko模块无密码登录
利用python 下paramiko模块无密码登录 上次我个大家介绍了利用paramiko这个模块,可以模拟ssh登陆远程服务器,并且可以返回执行的命令结果,这次给大家介绍下如何利用已经建立的密钥 ...
- Python之paramiko模块和SQL连接API
堡垒机前戏 开发堡垒机之前,先来学习Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作 SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: i ...
- python的paramiko模块
paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接.paramiko支持Linux, Solaris, BSD, MacOS X, ...
- python的paramiko模块的安装与使用
一:简介 paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接. 由于使用的是python这样的能够跨平台运行的语言,所以所有python支 ...
- Python修改paramiko模块开发运维审计保垒机
目前市面上,专门做IT审计堡垒机的厂商有很多,他们的产品都有一个特点,那就是基本上每台的售价都在20万以上.像我们做技术的,不可能每次待的公司都是大公司,那么在小公司,是不太可能投资20多万买一台硬件 ...
- python(paramiko模块的简单使用)
#通过paramiko模块连接主机运行bash命令 import paramiko hostname = '192.168.88.31' port = 22 username = 'root' pas ...
- python中paramiko模块的使用
paramiko是python一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接1.可以远程操作服务器文件 例如: df:查看磁盘使用情况 mkdir:创建目录 mv/cp/mk ...
随机推荐
- Python网络编程总结
----learn from luffycity---- 1. 什么是C/S架构? C指的是client(客户端软件),S指的是Server(服务端软件),C/S架构就是基于网络实现客户端与服务端通信 ...
- 使用async-http-client实现异步批量http请求
最近项目中需要在微服务中调用rest接口,而且需要调用得次数很多,所以同步得http客户端已经不满足要求,在网上查阅资料后发现了async-http-client这个包得性能不错,所以写了个demo测 ...
- 给Notepad++换主题
Notepad++是一款不错的编辑器,很轻巧,我很喜欢它.再换个主题,加个代码高亮,看上去就更专业了.如果你是Mac用户,应该听说或使用过Textmate(什么?没听过,那你该补课了!),Textma ...
- 一)如何开始 ehcache ?
官网地址 http://www.ehcache.org/ 从哪开始 第一步优先下载 http://www.ehcache.org/downloads/ 下载 Ehcache 2.10.0 .tar.g ...
- post异步请求
//创建url NSURL *url = [[NSURL alloc] initWithString:@"http://api.hudong.com/iphonexml.do"]; ...
- Activity生命流程
做Android的同学说起 Activity,那绝对是熟悉的不能再熟悉了,但是越熟悉的东西往往越陌生.我们真的了解她吗?她是我们所认识的那样吗?或许是,或许不是!了解与否, 让我们往下看.首先借And ...
- Codeforces735D Taxes 2016-12-13 12:14 56人阅读 评论(0) 收藏
D. Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- office2010安装不成功提示缺少MSXML 6.10.1129.0?
office2010安装 1. office重装 由于之前重装系统后安装office2010很顺利,这次删除office2010,由于没有删除干净,在程序删除面板中误点删除了其他文件所致,所以在此安装 ...
- [翻译][HTML]CELLPADDING and CELLSPACING
w3school手册:http://www.w3schools.com/tags/att_table_cellspacing.asp 一直以来都发现自己对cellpadding&cellspa ...
- 转(C# 实现生产者消费者队列)
class Program { // 任务队列 static Queue<string> _tasks = new Queue<string>(); // 为保证线程安全,使用 ...