Chinaunix

标题: 还是用paramiko写远程批量处理的脚本遇到的问题 [打印本页]

作者: onlinekof2001    时间: 2013-04-15 17:28
标题: 还是用paramiko写远程批量处理的脚本遇到的问题
没写过python脚本,今天想写远程登陆批量操作的脚本。基本是借鉴网上的脚本,脚本如下

======================
import paramiko
import threading


def ssh2(ip,user,pw,pt,cmd):
    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(ip,username=user,password=ip_address,port=pt)
        for m in cmd:
            stdin, stdout, stderr = ssh.exec_command(m)
            out = stdout.readlines()
            for o in out:
                print o,
        print '%s\tOK\n'%(ip_address)
        ssh.close()
    except :
        print '%s\tError\n'%(ip_address)

if __name__=='__main__':

    cmd = ['echo ','test > /root/test.txt']
    user = "root"
    #threads = []

f = file('~/file')
while True:
    line = f.readline()
    if len(line) == 0:
        break
    ip1 = line.split()
    ip_address = ip1[0]
    pt1 = ip1[1]
    ip_passwd = ip1[2]
    print "address is",ip_address,"and port is",pt1,"passwd is",ip_passwd
    #ssh2(ip_address,user,ip_passwd,cmd,pt1)
    affair=threading.Thread(target=ssh2,args=(ip_address,pt1,user,ip_passwd,cmd))
    affair.start()
f.close()

=================================
脚本从本地去读文件file,file的内容为一台服务器信息一共3列,分别为ip,port,passwd。也参考了论坛里其他几乎相同的脚本。运行无报错,但是就是无法登陆到远程服务器上进行命令操作。除非把端口写死,才可以连接。求感兴趣的朋友帮忙分析下。谢谢啊~
作者: thinkc    时间: 2013-04-16 08:54
提示: 作者被禁止或删除 内容自动屏蔽
作者: jeppeter    时间: 2013-04-16 09:43
回复 1# onlinekof2001


    建议你对端口要用int(port)转换,可能是这个原因造成的。
作者: onlinekof2001    时间: 2013-04-16 11:05
#!/bin/env python2.7

import paramiko
import threading


def ssh2(host,user,pw,pt,cmd):
    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(host,username=user,password=pw,port=pt)
        for m in cmd:
            stdin,stdout,stderr = ssh.exec_command(m)
            out = stdout.readlines()
#        print o.read()
            for o in out:
                print o,
        print '%s\tOK\n'%(hosta)
        ssh.close()
    except :
        print '%s\tError\n'%(hosta)

if __name__=='__main__':

    cmd = ['echo test1 > /root/test.txt']
    user = "root"
    threads = []

file = open("/root/file","r"

for line in file.readlines():
    if len(line) == 0:
        break
    s = line.split()
    hosta = s[0]
    pt1 = s[1]
    hostp = s[2]
    print "address is",hosta,"and port is",pt1,"passwd is",hostp
#    ssh2(hosta,user,hostp,int(pt1))
    affair=threading.Thread(target=ssh2,args=(hosta,user,hostp,int(pt1),cmd))
    affair.start()
file.close()
也不知道有什么不同,我做了一些修改。现在可以执行批量了。 刚才回到别人帖子了。
作者: onlinekof2001    时间: 2013-04-16 14:44
回复 3# jeppeter

现在又遇到了问题,因为涉及到交互,我调用stdin.write("y") 进行交过程中的回答。没生效。 脚本一直处于等待回答的状态,python我不是很懂,如何去检验脚本?
作者: jeppeter    时间: 2013-04-16 20:06
回复 5# onlinekof2001


    你这种情况 ,最好用pexpect这样的模块更好。
作者: onlinekof2001    时间: 2013-04-17 08:52
问题已经解决了。直接问了微薄的博主,给了很好的解答,stdin.write("y") 传递到远程的时候没有结束符,误以为只要传递一个字符就可以继续了。实际应为stdin.write("y\n")
作者: onlinekof2001    时间: 2013-04-17 08:55
回复 6# jeppeter

关于哪个比较好的问题,我不是太清楚,我是百度找到的一篇博客,作者写列pexpect和paramiko两个脚本,提到前者不够理想的情况下写了后者。于是我之间参照后的
   




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2