ssh paramiko && subprocess
subprocess:
#!/usr/bin/python3
import paramiko
import os
import sys
import subprocess
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)
from src.logutils import logger
log=logger("root",rootstdout=True,handlerList=['I','E'])
class SshConnect(object): def __init__(self,ip,user,pwd,port):
self.port=port
self.pwd=pwd
self.user=user
self.ip=ip
try:
client=paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(self.ip,self.port,self.user,self.pwd)
self.client=client
except ConnectionError as e:
log.error(e)
def exe_cmd(self,shell):
stdin, stdout, stderr = self.client.exec_command(shell)
res,err = stdout.read(),stderr.read()
result = res if res else err
return result
# print(result.decode("utf-8"))
def close_client(self):
self.client.close()
def run():
ip='192.168.81.129'
port=22
pwd='admin'
user='root'
client=SshConnect(ip,user,pwd,port)
res=client.exe_cmd('ps -ef|grep python&&ifconfig')
log.info(res) def run_sshpass():
shell=" ps -ef|grep java&&df -m"
sub=subprocess.Popen(shell,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
sub.wait(timeout=20)
res=sub.stdout.readlines()
log.info(deal_res(res)) def deal_res(res):
std=''
for i in res: output=i.decode("utf-8") std +=output
return std if __name__ == '__main__':
run_sshpass()
#run()
[root@hostuser src]#
[root@hostuser src]# python3 sshutils.py
[INFO]2019-05-14 00:31:27 Tue --root-- sshutils.py:
root 28680 28677 0 00:31 pts/0 00:00:00 /bin/sh -c ps -ef|grep java&&df -m
root 28683 28680 0 00:31 pts/0 00:00:00 grep java
Filesystem 1M-blocks Used Available Use% Mounted on
/dev/mapper/centos-root 27627 8616 19012 32% /
devtmpfs 894 0 894 0% /dev
tmpfs 910 1 910 1% /dev/shm
tmpfs 910 10 901 2% /run
tmpfs 910 0 910 0% /sys/fs/cgroup
/dev/sda1 1014 232 783 23% /boot
paramiko:
#!/usr/bin/python3
import paramiko
import os
import sys
import subprocess
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)
from src.logutils import logger
log=logger("root",rootstdout=True,handlerList=['I','E'])
class SshConnect(object): def __init__(self,ip,user,pwd,port):
self.port=port
self.pwd=pwd
self.user=user
self.ip=ip
try:
client=paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(self.ip,self.port,self.user,self.pwd)
self.client=client
except ConnectionError as e:
log.error(e)
def exe_cmd(self,shell):
stdin, stdout, stderr = self.client.exec_command(shell)
res,err = stdout.read(),stderr.read()
result = res if res else err
return result
# print(result.decode("utf-8"))
def close_client(self):
self.client.close()
def run():
ip='192.168.81.129'
port=22
pwd='admin'
user='root'
client=SshConnect(ip,user,pwd,port)
res=client.exe_cmd('ps -ef|grep python&&ifconfig')
log.info(res) def run_sshpass():
shell=" ps -ef|grep java&&df -m"
sub=subprocess.Popen(shell,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
sub.wait(timeout=20)
res=sub.stdout.readlines()
log.info(deal_res(res)) def deal_res(res):
res1= res.decode("utf-8") return res1 if __name__ == '__main__':
#run_sshpass()
run()
[root@hostuser src]# vi sshutils.py
[root@hostuser src]# python3 sshutils.py
[INFO]2019-05-14 00:28:19 Tue --paramiko.transport-- transport.py:
Connected (version 2.0, client OpenSSH_7.4)
/usr/local/lib/python3.7/site-packages/paramiko/kex_ecdh_nist.py:39: CryptographyDeprecationWarning: encode_point has been deprecated on EllipticCurvePublicNumbers and will be removed in a future version. Please use EllipticCurvePublicKey.public_bytes to obtain both compressed and uncompressed point encoding.
m.add_string(self.Q_C.public_numbers().encode_point())
/usr/local/lib/python3.7/site-packages/paramiko/kex_ecdh_nist.py:96: CryptographyDeprecationWarning: Support for unsafe construction of public numbers from encoded data will be removed in a future version. Please use EllipticCurvePublicKey.from_encoded_point
self.curve, Q_S_bytes
/usr/local/lib/python3.7/site-packages/paramiko/kex_ecdh_nist.py:111: CryptographyDeprecationWarning: encode_point has been deprecated on EllipticCurvePublicNumbers and will be removed in a future version. Please use EllipticCurvePublicKey.public_bytes to obtain both compressed and uncompressed point encoding.
hm.add_string(self.Q_C.public_numbers().encode_point())
[INFO]2019-05-14 00:28:19 Tue --paramiko.transport-- transport.py:
Authentication (publickey) failed.
[INFO]2019-05-14 00:28:19 Tue --paramiko.transport-- transport.py:
Authentication (password) successful!
[INFO]2019-05-14 00:28:45 Tue --root-- sshutils.py:
b'root 28245 16287 2 00:28 pts/0 00:00:00 python3 sshutils.py\nroot 28310 28248 0 00:28 ? 00:00:00 bash -c ps -ef|grep python&&ifconfig\nroot 28326 28310 0 00:28 ? 00:00:00 grep python\nens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500\n inet 192.168.81.129 netmask 255.255.255.0 broadcast 192.168.81.255\n inet6 fe80::f08c:a9:42b2:6ec4 prefixlen 64 scopeid 0x20<link>\n ether 00:0c:29:11:d6:35 txqueuelen 1000 (Ethernet)\n RX packets 7533 bytes 640098 (625.0 KiB)\n RX errors 0 dropped 0 overruns 0 frame 0\n TX packets 6119 bytes 755348 (737.6 KiB)\n TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0\n\nlo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536\n inet 127.0.0.1 netmask 255.0.0.0\n inet6 ::1 prefixlen 128 scopeid 0x10<host>\n loop txqueuelen 1000 (Local Loopback)\n RX packets 52950 bytes 43181732 (41.1 MiB)\n RX errors 0 dropped 0 overruns 0 frame 0\n TX packets 52950 bytes 43181732 (41.1 MiB)\n TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0\n\n'
ssh paramiko && subprocess的更多相关文章
- day 28 黏包 ssh模块 subprocess模块
套接字的信仰 一切皆文件 昨日作业: import socket sock=socket.socket() # TCP协议 IP_PORT=("127.0.0.1",8899) s ...
- Python time、datetime、os、random、sys、hashlib、json、shutil、logging、paramiko、subprocess、ConfigParser、xml、shelve模块的使用
文章目录: 1. time & datetime模块 2. os模块 3. random模块 4. sys模块 5. hashlib模块 6. json模块 7. shutil模块 8. lo ...
- python paramiko模块SSH自动登录linux系统进行操作
1). Linux系统首先要开启SSH服务:service ssh status 如果没安装的话,则要:apt-get install openssh-server service ssh resta ...
- paramiko SSH 模块简单应用。
目的:需要ssh链接到Linux主机,执行telnet 命令,抓回显匹配制定内容. ssh --->执行telnet到本地端口--->执行类似 ls 的命令.匹配命令执行后的特定回显字段. ...
- Python 模块功能paramiko SSH 远程执行及远程下载
模块 paramiko paramiko是一个用于做远程控制的模块,使用该模块可以对远程服务器进行命令或文件操作,值得一说的是,fabric和ansible内部的远程管理就是使用的paramiko来现 ...
- python 使用paramiko模块上传本地文件到ssh
我们要了解几个函数: paramiko.Tranport(("目标ip,端口"))#这是上传目标的IP和端口 paramiko.SFTPClient.from_tranport() ...
- 【Python】 SSH连接的paramiko
paramiko *paramiko需要PyCrypto模块的支持 paramiko支持通过SSH协议进行一些操作,比如远程执行命令,上下传文件等等 用法: ① 远程命令 ssh = paramiko ...
- Python3学习之路~9.1 paramiko模块:实现ssh执行命令以及传输文件
我们一般使用linux的时候,都是在Windows上安装一个ssh客户端连接上去.那么从一台linux如何连接到另一条linux呢?使用ssh命令即可,因为每台linux机器自己都有一个ssh客户端. ...
- paramiko之ssh登录,执行cmd,下载文件
一.paramiko远程登录及执行命令 1.1:exec_command(cmd)远程执行命令 client = paramiko.SSHClient() client.set_missing_hos ...
随机推荐
- 一段讯飞、百度等语音识别API无法识别的语音最终解决办法
最近在做语音识别.字幕扒词相关的工作,遇到了一段录音(https://download.csdn.net/download/u014220286/12169183,各位有兴趣的可以下载下来试试),音质 ...
- STL关联容器总结
有序的都不带unordered,即如下: set multiset map multimap 其中带multi的表示关键字可以重复 无序的带unordered,如下: unordered_map un ...
- python入门(十九讲):多进程
1.进程概念 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动.是系统进行资源分配和调度的基本单位,是操作系统结构的基础. 狭义定义:进程是正在运行的程序的实例. 在早期面向进程设 ...
- C++类的函数成员
类的基本成员函数 一个类包含:构造.析构.拷贝构造.赋值构造.常对象取地址.普通对象取地址基本函数 class Test { public: Test() :m_data(data) { cout & ...
- imread函数+cvtColor()函数
加载图像(用cv::imread) imread功能是加载图像文件成为一个Mat对象,其中第一个参数表示图像文件名称 第二个参数,表示加载的图像是什么类型,支持常见的三个参数值 IMREAD_UNCH ...
- L3-010 是否完全二叉搜索树 (30分)
题解 判断一棵树是否是完全二叉树: 取队列的头,将头的左右孩子入队,循环每次判断是否为空,如果为空节点,此时退出循环. 然后检查队列中的元素是否全部为空,如果是则说明是完全二叉树,否则不是. 代码 # ...
- Django框架-模型层
Django框架-模型层 一.单表查询之必知必会13条 1.时间字段中的两个关键性参数 create_time = models.DateField() # 年月日 create_time = mod ...
- Educational Codeforces Round 78 (Rated for Div. 2)E(构造,DFS)
DFS,把和当前结点相连的点全都括在当前结点左右区间里,它们的左端点依次++,然后对这些结点进行DFS,优先对左端点更大的进行DFS,这样它右端点会先括起来,和它同层的结点(后DFS的那些)的区间会把 ...
- reduce 方法(升序)
语法: array1.reduce(callbackfn[, initialValue]) 参数 定义 array1 必需.一个数组对象. callbackfn 必需.一个接受最多四个参数的函数.对于 ...
- Linux安装Redis服务
下载:wget http://download.redis.io/releases/redis-5.0.5.tar.gz 解压:tar -zxvf redis-5.0.5.tar.gz 进入redi ...