Paramiko:

  paramiko模块,基于SSH用于连接远程服务器并执行相关操作。

  SSHClient:

    用于连接远程服务器并执行基本命令

  SFTPClient:

    用于连接远程服务器并执行上传下载

以下该脚本可以实现:上传文件、下载文件、执行命令

 #!/usr/bin/env python
#定义函数
import paramiko
import os
import datetime
import sys
import re
#定义基础命令格式
def perform_command(hostname, port, username, password, comm):
try:
t=paramiko.Transport((hostname,port))
t.connect(username=username,password=password)
try:
print "\033[33mIP:\033[0m", hostname
print "\033[33mPerform command:\033[0m", comm
print '\033[32m###########################################################\033[0m'
s=paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname = hostname, port = port, username = username, password = password)
stdin,stdout,stderr=s.exec_command(comm)
print stdout.read()
print '\033[32m###########################################################\033[0m'
print '\033[32mScript perform success %s \033[0m' % datetime.datetime.now()
s.close()
except Exception:
print "\033[31mERROR - Perform error\033[0m"
except Exception:
print "\033[31mERROR - Connect error! IP: %s!\033[0m" % hostname #定义发送文件格式
def send_file(hostname,port,username,password,local_dir,client_dir,file):
try:
t=paramiko.Transport((hostname,port))
t.connect(username=username,password=password)
try:
print "\033[33mIP:\033[0m", hostname
print "\033[33mSend file:\033[0m", file
print "\033[33mClient_dir:\033[0m", client_dir
sftp=paramiko.SFTPClient.from_transport(t)
sftp.put(os.path.join(local_dir,file),os.path.join(client_dir,file))
t.close()
print '\033[32mScript perform success %s \033[0m' % datetime.datetime.now()
except Exception:
print "\033[31mERROR - Perform error\033[0m"
except Exception:
print "\033[31mERROR - Connect error! IP: %s!\033[0m" % hostname
#定义下载文件格式
def get_file(hostname,port,username,password,local_dir,client_dir,file):
try:
t=paramiko.Transport((hostname,port))
t.connect(username=username,password=password)
try:
print "\033[33mIP:\033[0m", hostname
print "\033[33mGet file:\033[0m", file
print "\033[33mClient_dir:\033[0m", client_dir
sftp=paramiko.SFTPClient.from_transport(t)
sftp.get(os.path.join(client_dir,file),os.path.join(local_dir,file))
t.close()
print '\033[32mScript perform success %s \033[0m' % datetime.datetime.now()
except Exception:
print "\033[31mERROR - Perform error\033[0m"
except Exception:
print "\033[31mERROR - Connect error! IP: %s!\033[0m" % hostname
#定义帮助文件
def help():
print """
\033[33mHelp: Welcome to use python script, this script can help you run commands, send files and get files. Use this script, you can running for one host or a host group, and you know get file can't for a host group. Give you examples: If you want perform command:
Usage: (for one host) ./paramiko-upload.py -H [hostip] -c [command]
Example: ./paramiko-upload.py -H 10.100.139.245 -c 'ls -al' Usage: (for a host group) ./paramiko-upload.py -F [ip.txt] -c [command]
Example: ./paramiko-upload.py -F 'ip.txt' -c 'ls -al' If you want send file: Usage: (for one host) ./paramiko-upload.py -H [hostip] -s [local_dir] [client_dir] [file]
Example: ./paramiko-upload.py -H 10.100.139.245 -s '/opt/Felix' '/tmp' ip.txt Usage: (for a host group) ./paramiko-upload.py -F [ip.txt] -s [local_dir] [client_dir] [file]
Example: ./paramiko-upload.py -F ip.txt -s '/opt/Felix' '/tmp' ip.txt If you want get file:
Usage: (for one host) ./paramiko-upload.py -H [hostip] -g [local_dir] [client_dir] [file]
Example: ./paramiko-upload.py -H 10.100.139.245 -g '/opt/Felix' '/tmp' ip.txt ip.txt file content like this:
1.1.1.1
2.2.2.2\033[0m
"""
sys.exit() #定义命令详情 if __name__ == '__main__':
#定义账号、端口、密码等信息
username='root'
port=2222
password='abcdefg' if len(sys.argv) > 2:
if sys.argv[1] == '-H':
if len(sys.argv) == 5 and sys.argv[3] == '-c':
hostname=sys.argv[2]
comm = sys.argv[4]
perform_command(hostname, port, username, password, comm)
elif len(sys.argv) == 7 and sys.argv[3] == '-s':
hostname=sys.argv[2]
local_dir=sys.argv[4]
client_dir=sys.argv[5]
file=sys.argv[6]
send_file(hostname,port,username,password,local_dir,client_dir,file)
elif len(sys.argv) == 7 and sys.argv[3] == '-g':
hostname=sys.argv[2]
username='root'
port=2222
password='abcdefg' if len(sys.argv) > 2:
if sys.argv[1] == '-H':
if len(sys.argv) == 5 and sys.argv[3] == '-c':
hostname=sys.argv[2]
comm = sys.argv[4]
perform_command(hostname, port, username, password, comm)
elif len(sys.argv) == 7 and sys.argv[3] == '-s':
hostname=sys.argv[2]
local_dir=sys.argv[4]
client_dir=sys.argv[5]
file=sys.argv[6]
send_file(hostname,port,username,password,local_dir,client_dir,file)
elif len(sys.argv) == 7 and sys.argv[3] == '-g':
hostname=sys.argv[2]
local_dir=sys.argv[4]
client_dir=sys.argv[5]
file=sys.argv[6]
get_file(hostname,port,username,password,local_dir,client_dir,file)
else:
help()
elif sys.argv[1] == '-F':
ip_file=sys.argv[2]
file_stat=os.path.exists(ip_file)
if file_stat:
f=open(ip_file)
ips=f.readlines()
f.close()
for ip in ips:
ip = ip.strip('\n')
if re.match(r'^#', ip):
continue
if len(sys.argv) == 5 and sys.argv[3] == '-c':
hostname = ip
comm = sys.argv[4]
perform_command(hostname, port, username, password, comm)
elif len(sys.argv) == 7 and sys.argv[3] == '-s':
hostname=ip
local_dir=sys.argv[4]
client_dir=sys.argv[5]
file=sys.argv[6]
send_file(hostname,port,username,password,local_dir,client_dir,file)
else:
help()
else:
print "\033[31mERROR - Not found ip_file!\033[0m"
help()
else:
help()
else:
help()

【python】用python脚本Paramiko实现远程执行命令、下载、推送/上传文件功能的更多相关文章

  1. Python脚本控制的WebDriver 常用操作 <二十六> 上传文件

    测试用例场景 上传文件的方法是找到上传文件的对象,通常是的对象.然后直接往这个对象send_keys,传入需要上传文件的正确路径.绝对路径和相对路径都可以,但是上传的文件必须存在,否则会报错. Pyt ...

  2. “通过jumpserver远程登录linux服务器,rz上传文件速度过慢”问题的解决

    问题: windows通过jumpserver远程登录到linux服务器,使用rz上传jar包,速度太慢(10k以内). 解决方案: 思路:通过ssh直接登录远程服务器 1.secureCRT-> ...

  3. paramiko多线程远程执行命令

    import paramiko import sys import getpass import threading import os def rcmd(host=None, port=22, us ...

  4. Ajax+Python flask实现上传文件功能

    HTML: <div > <input type="file" name="FileUpload" id="FileUpload&q ...

  5. python day 14: 作业:开发一个能够多用户上传文件的FTP脚本

    目录 python day 14 1. 要求 2. 自己写的程序目录 3. models模块 4. settings模块 5. tcp_server模块 6. client模块 7. 后记 pytho ...

  6. SSH远程执行命令环境变量问题

    SSH命令格式 usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec] [-D [bind_address: ...

  7. Python学习总结 06 paramiko 远程执行命令

    有时会需要在远程的机器上执行一个命令,并获得其返回结果.对于这种情况,python 可以很容易的实现. 1 工具 Python paramiko 1) Paramiko模块安装 在Linux的Term ...

  8. python之paramiko 远程执行命令

    有时会需要在远程的机器上执行一个命令,并获得其返回结果.对于这种情况,python 可以很容易的实现. 1 .工具 Python paramiko 1) Paramiko模块安装 在Linux的Ter ...

  9. Python 模块功能paramiko SSH 远程执行及远程下载

    模块 paramiko paramiko是一个用于做远程控制的模块,使用该模块可以对远程服务器进行命令或文件操作,值得一说的是,fabric和ansible内部的远程管理就是使用的paramiko来现 ...

随机推荐

  1. iscroll 子表左右滚动同时保持页面整体上下滚动

    if ( this.options.preventDefault && !utils.isBadAndroid && !utils.preventDefaultExce ...

  2. T4模板编辑器

    一 二.工具  (T4模板编辑器) 使用效果 1.tangibleT4EditorPlusModellingToolsVS2013.msi 2.devart T4 Editor for Visual ...

  3. 即将到来的Android N,将具备这些新特性

    原文转自:http://www.leiphone.com/news/201602/pSRQAuAjMFJITqHe.html         原创 訾竣喆 即将到来的Android N,将具备这些新特 ...

  4. vue组件class绑定

    当在一个自定义组件上使用 class 属性时,这些类将被添加到该组件的根元素上面.这个元素上已经存在的类不会被覆盖. 例如,如果你声明了这个组件: Vue.component('my-componen ...

  5. 【LeetCode】Partition List ——链表排序问题

    [题目] Given a linked list and a value x, partition it such that all nodes less than x come before nod ...

  6. 高阶函数:filter()

    Python内建的filter()函数用于过滤序列. 和map()类似,filter()也接收一个函数和一个序列.和map()不同的是,filter()把传入的函数依次作用于每个元素,然后根据返回值是 ...

  7. HDU 2255 奔小康赚大钱 KM裸题

    #include <stdio.h> #include <string.h> #define M 310 #define inf 0x3f3f3f3f int n,nx,ny; ...

  8. Spring学习十----------Bean的配置之Autowired注解实现

    © 版权声明:本文为博主原创文章,转载请注明出处 @Required -@Required注解适用于bean属性的setter方法 -这个注解仅仅表示,受影响的bean属性必须在配置时被填充,通过在b ...

  9. leetCode 61.Rotate List (旋转链表) 解题思路和方法

    Rotate List  Given a list, rotate the list to the right by k places, where k is non-negative. For ex ...

  10. jquery中this和$(this)使用的地方

    插件中this代表$('元素')选择器 on()函数中this代表单个元素,$(this)代表单个选择器 反正有时代表整体,有时代表单个,代表单个时可以用$(this)来把他变成jquery对象