hello cc

python 中使用ssh链接模块

Posted on 2016-01-13 16:42  星际海盗  阅读(548)  评论(0)    收藏  举报

说明:

  python中针对ssh协议的模块有很多,如:pxssh,fabric,pexpect,paramiko等...  目前用的最多的就是paramiko包,里面有很多好玩的东西,详细参考:

https://paramiko-docs.readthedocs.org/en/1.15/index.html

一.paramiko模块使用举例:

1) 一个连接实例:

import paramiko
from paramiko.py3compat import u

paramiko.util.log_to_file('paramiko-test.log')
sshd = paramiko.SSHClient()
sshd.set_missing_host_key_policy(paramiko.AutoAddPolicy())    允许连接不在know_hosts文件中的主机
sshd.connect("1.1.1.1",22, "root","root")
stdin, stdout, stderr = sshd.exec_command('free;ifconfig;id;df -h')
print stdout.read()
sshd.close()

 

二. pxssh模块

> import pxssh

> ss = pxssh.pxssh()     ,more can ues dir(ss) or help(ss)

> ss.login('1.1.1.1','root','root')

> Ture        return Ture login success,

> ss.sendline('w')   'w'  bash command

> ss.prompt()    

> ss.before      return w result.

......

 

hello man