免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2530 | 回复: 7
打印 上一主题 下一主题

还是用paramiko写远程批量处理的脚本遇到的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-04-15 17:28 |只看该作者 |倒序浏览
没写过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。也参考了论坛里其他几乎相同的脚本。运行无报错,但是就是无法登陆到远程服务器上进行命令操作。除非把端口写死,才可以连接。求感兴趣的朋友帮忙分析下。谢谢啊~

论坛徽章:
0
2 [报告]
发表于 2013-04-16 08:54 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
1
15-16赛季CBA联赛之新疆
日期:2017-03-09 12:33:45
3 [报告]
发表于 2013-04-16 09:43 |只看该作者
回复 1# onlinekof2001


    建议你对端口要用int(port)转换,可能是这个原因造成的。

论坛徽章:
0
4 [报告]
发表于 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()
也不知道有什么不同,我做了一些修改。现在可以执行批量了。 刚才回到别人帖子了。

论坛徽章:
0
5 [报告]
发表于 2013-04-16 14:44 |只看该作者
回复 3# jeppeter

现在又遇到了问题,因为涉及到交互,我调用stdin.write("y") 进行交过程中的回答。没生效。 脚本一直处于等待回答的状态,python我不是很懂,如何去检验脚本?

论坛徽章:
1
15-16赛季CBA联赛之新疆
日期:2017-03-09 12:33:45
6 [报告]
发表于 2013-04-16 20:06 |只看该作者
回复 5# onlinekof2001


    你这种情况 ,最好用pexpect这样的模块更好。

论坛徽章:
0
7 [报告]
发表于 2013-04-17 08:52 |只看该作者
问题已经解决了。直接问了微薄的博主,给了很好的解答,stdin.write("y") 传递到远程的时候没有结束符,误以为只要传递一个字符就可以继续了。实际应为stdin.write("y\n")

论坛徽章:
0
8 [报告]
发表于 2013-04-17 08:55 |只看该作者
回复 6# jeppeter

关于哪个比较好的问题,我不是太清楚,我是百度找到的一篇博客,作者写列pexpect和paramiko两个脚本,提到前者不够理想的情况下写了后者。于是我之间参照后的
   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP