python使用libssh2连接linux
(1)使用下面命令获得最新版本的ssh4py安装包
import socket
import libssh2
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('exmaple.com', 22))
session = libssh2.Session()
session.startup(sock)
#还需要使用基本的密码进行验证登陆
session.userauth_password(username, password)
#现在可以开始使用它了
channel = session.channel()
channel.execute('ls /etc/debian_version')
channel.wait_closed()
if channel.get_exit_status() == 0:
print "It's a Debian system"
else:
print "It's not a Debian system"
import socket
import libssh2
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('exmaple.com', 22))
session = libssh2.Session()
session.startup(sock)
#还需要使用基本的密码进行验证登陆
session.userauth_password(username, password)
channel = session.channel()
channel.execute('ls -l')
stdout = []
stderr = []
while not channel.eof:
data = channel.read(1024)
if data:
stdout.append(data)
data = channel.read(1024, libssh2.STDERR)
if data:
stderr.append(data)
print ''.join(stdout)
print ''.join(stderr)
这里我们都会发现,使用exec_command('cd dirname')时并不会切换目录,
execute_command() 是a single session,每次执行完后都要回到缺省目录。
所以可以 .execute_command('cd /var; pwd')。
python使用libssh2连接linux的更多相关文章
- windows 上 Python 通过 SCP 连接linux server
环境搭建 需要安装以下包 pycrypto (需要VC编译环境) paramiko (SSH的基础) scpclient-0.4 (paramiko 上的一个wrapper) 在安装pycrypto ...
- Python ssh连接Linux服务器报Incompatible ssh peer (no acceptable kex algorithm) 解决方法
python通过ssh连接linux服务器,部分服务器出现如下异常 03:50:48.725 FAIL ftp operation failed, Incompatible ssh peer (no ...
- 转 windows下安装pycharm并连接Linux的python环境 以及 windows 下notepad ++编辑 linux 的文件
######sample 1:windows下安装pycharm并连接Linux的python环境 https://www.cnblogs.com/junxun/p/8287998.html wind ...
- Python (paramiko) 连接Linux服务器
目录 参考资料 Paramiko 安装 连接Linux 文件上传/下载 文件封装 其他 参考资料 https://www.liujiangblog.com/blog/15/ https://blog. ...
- 用 Python 脚本实现对 Linux 服务器的监控
目前 Linux 下有一些使用 Python 语言编写的 Linux 系统监控工具 比如 inotify-sync(文件系统安全监控软件).glances(资源监控工具)在实际工作中,Linux 系统 ...
- 运用Python语言编写获取Linux基本系统信息(三):Python与数据库编程,把获取的信息存入数据库
运用Python语言编写获取Linux基本系统信息(三):Python与数据库编程 有关前两篇的链接: 运用Python语言编写获取Linux基本系统信息(一):获得Linux版本.内核.当前时间 运 ...
- python使用stomp连接activemq
一.安装ActiveMQ服务 1. 当使用windows时,安装参考:https://blog.csdn.net/WuLex/article/details/78323811 启动:运行activem ...
- Pycharm 连接Linux 远程开发
Pycharm 连接Linux 远程开发 在Liunx上安装python3.6(Ubuntu16) 下载Python-3.6.4.tgz 解压 tar -xzvf Python-3.6.4.tgz 进 ...
- boto3--通过Python的SDK连接aws
通过Python的SDK连接aws 参考: https://aws.amazon.com/cn/developers/getting-started/python/ aws上生成访问密钥 ID 和私有 ...
随机推荐
- 大神的vim配置
大神的vim配置,O(∩_∩)O spf13-vim : Steve Francia's Vim Distribution __ _ _____ _ ___ _ __ / _/ |___ / __ _ ...
- WPF:使用Json.NET在TreeView中树形显示JSON数据
原文 WPF:使用Json.NET在TreeView中树形显示JSON数据 据 读者可以参考这个开源的可以树形显示XML和JSON的工具: Mgen Object 603:XML/JSON树形显示小工 ...
- BZOJ 1609: [Usaco2008 Feb]Eating Together麻烦的聚餐
1609: [Usaco2008 Feb]Eating Together麻烦的聚餐 Description 为了避免餐厅过分拥挤,FJ要求奶牛们分3批就餐.每天晚饭前,奶牛们都会在餐厅前排队入内,按F ...
- imagemagick /tmp/magick-xxxxxxxx
问题 imagemagick在某种场景下会狂写/tmp目录,文件名形如magick-xxxxxxxx, ls -lh查看这些文件达到几百G, du -sh查看则只有几十M 被这个问题折磨了许久,大晚上 ...
- poj 3281 Dining 网络流-最大流-建图的题
题意很简单:JOHN是一个农场主养了一些奶牛,神奇的是这些个奶牛有不同的品味,只喜欢吃某些食物,喝某些饮料,傻傻的John做了很多食物和饮料,但她不知道可以最多喂饱多少牛,(喂饱当然是有吃有喝才会饱) ...
- Linux下Django开发学习(一)
一,环境配置: 环境:Ubuntu 14.04 LTS, Python 2.7, Django 1.8 安装Python : Ubuntu自带python2.7,不用安装 安装pip:sudo ap ...
- IP数据包结构
Linux 网络编程——IP 数据报格式详解 http://www.linuxidc.com/Linux/2015-04/116149.htm
- ContentType
常用的 ContentType XML <% response.ContentType ="text/xml" %> GIF images <% respons ...
- Qt 文件监视器 QFileSystemWatcher
之前有过对Qt的QFile以Text纯文本方式进行读取时的学习,这两天由于实时需要又对QFileSystemWatcher(这个类是干什么用的)进行了学习,发现也是问题很让人头疼. 我想监视一个文件夹 ...
- qml+opencv(二),实现人脸检测
http://blog.csdn.net/zhx6044/article/details/45048765