paramiko学习笔记

1.Linux安装paramiko要先安装好python-devel和PyCrypto的模块
Windows安装paramiko要先安装好pycrypto和ecdsa两个模块。
2.paramiko例子
1)ssh例子
[root@scctmpdev02101:/root/python]#vim paramikosshv2.py
#!/usr/bin/python
#coding=utf-8 #python2的编码默认是ASCII,你的文件里有中文就必须要用utf-8编码,只要在文件需要在文件
import paramiko
import sys #读取参数
import getpass #人机交互读取密码
USER = 'root'
PASSWORD = 'xxxxxx'
PASSWORD = getpass.getpass('Input Password: ')
COMMAND = 'touch /tmp/20170307103210'
COMMAND = "touch /tmp/`date +%Y%m%d%H%M%S`"
COMMAND =  open(sys.argv[1]).read()
#for line in open('/root/python/host_hdpdev').readlines():#absolute path or relative
for line in open(sys.argv[2]).readlines():
        if  line.startswith('#'):
                print 'the line is not  a valid IP:' + line.strip()
        else:
                print 'the line is a valid IP:' + line.strip()
                IP = line
                paramiko.util.log_to_file('/root/python/paramiko.log')#每次都是追加写入
                s = paramiko.SSHClient()
                s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                s.connect(hostname=IP,username=USER,password=PASSWORD)
                stdin,stdout,stderr = s.exec_command(COMMAND)
                print line.strip() + '执行结果' + '\n' + stdout.read() + '\n' + stderr.read()
                s.close
2)sftp例子
[root@scctmpdev02101:/root/python]#vim paramikosftp.py
import sys,paramiko,threading,getpass,os
action = sys.argv[1]
flag = '-p'
username = sys.argv[3]
password = getpass.getpass('Password: ')
sourcepath = sys.argv[4]
targetpath = sys.argv[5] if not sys.argv[5].endswith('/') else sys.argv[5] + '/' + os.path.basename(sourcepath)
#for line in open('/root/python/host_hdpdev').readlines():#absolute path
for line in open(sys.argv[2]).readlines():#absolute path
        if  line.startswith('#'):
                print 'the line is not  a valid IP:' + line.strip()
        else:
                print 'the line is a valid IP:' + line.strip()
                host=line.strip()
                t = paramiko.Transport((host,22))
                try:
                        t.connect(username=username,password=password)
                except Exception as e:
                        print host,str(e)
                sftp = paramiko.SFTPClient.from_transport(t)
                #if  action == flag:
                #if  action.strip() == '-p':
                if  (action == '-p'):
                        sftp.put(sourcepath,targetpath)
                else:
                        sftp.get(sourcepath,targetpath+'_'+host)
                t.close()
3 for例子

1)for i in range(0,10):
        if i > 5:
            break; #在值为6的时候退出for循环
        else:
                print "hello world";

2)for遍历读取文件行

for line in open('/root/python/host_hdpdev').readlines():#absolute path
        if  line.startswith('#'):
                print 'the line is not  a valid line.strip()' + line.strip()
        else:
                print 'the line is a valid line.strip():' + line.strip()

for和open文件结合的用法
for val in open("./host_hdpdev","r").read():#遍历文件的第一行,把第一行中的每个字符作为一行来读取
    print val

for val in open("./host_hdpdev","r").readline():#把文件的每个字符作为一行来读取
    print val

for val in open("./host_hdpdev","r").readlines():#一行一行读取文件
    print val

posted on 2017-03-08 16:57  石下江的太阳光  阅读(209)  评论(0编辑  收藏  举报

导航