亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2562 | 回復(fù): 7
打印 上一主題 下一主題

還是用paramiko寫遠程批量處理的腳本遇到的問題 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2013-04-15 17:28 |只看該作者 |倒序瀏覽
沒寫過python腳本,今天想寫遠程登陸批量操作的腳本;臼墙梃b網(wǎng)上的腳本,腳本如下

======================
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的內(nèi)容為一臺服務(wù)器信息一共3列,分別為ip,port,passwd。也參考了論壇里其他幾乎相同的腳本。運行無報錯,但是就是無法登陸到遠程服務(wù)器上進行命令操作。除非把端口寫死,才可以連接。求感興趣的朋友幫忙分析下。謝謝啊~

論壇徽章:
0
2 [報告]
發(fā)表于 2013-04-16 08:54 |只看該作者
提示: 作者被禁止或刪除 內(nèi)容自動屏蔽

論壇徽章:
1
15-16賽季CBA聯(lián)賽之新疆
日期:2017-03-09 12:33:45
3 [報告]
發(fā)表于 2013-04-16 09:43 |只看該作者
回復(fù) 1# onlinekof2001


    建議你對端口要用int(port)轉(zhuǎn)換,可能是這個原因造成的。

論壇徽章:
0
4 [報告]
發(fā)表于 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()
也不知道有什么不同,我做了一些修改。現(xiàn)在可以執(zhí)行批量了。 剛才回到別人帖子了。

論壇徽章:
0
5 [報告]
發(fā)表于 2013-04-16 14:44 |只看該作者
回復(fù) 3# jeppeter

現(xiàn)在又遇到了問題,因為涉及到交互,我調(diào)用stdin.write("y") 進行交過程中的回答。沒生效。 腳本一直處于等待回答的狀態(tài),python我不是很懂,如何去檢驗?zāi)_本?

論壇徽章:
1
15-16賽季CBA聯(lián)賽之新疆
日期:2017-03-09 12:33:45
6 [報告]
發(fā)表于 2013-04-16 20:06 |只看該作者
回復(fù) 5# onlinekof2001


    你這種情況 ,最好用pexpect這樣的模塊更好。

論壇徽章:
0
7 [報告]
發(fā)表于 2013-04-17 08:52 |只看該作者
問題已經(jīng)解決了。直接問了微薄的博主,給了很好的解答,stdin.write("y") 傳遞到遠程的時候沒有結(jié)束符,誤以為只要傳遞一個字符就可以繼續(xù)了。實際應(yīng)為stdin.write("y\n")

論壇徽章:
0
8 [報告]
發(fā)表于 2013-04-17 08:55 |只看該作者
回復(fù) 6# jeppeter

關(guān)于哪個比較好的問題,我不是太清楚,我是百度找到的一篇博客,作者寫列pexpect和paramiko兩個腳本,提到前者不夠理想的情況下寫了后者。于是我之間參照后的
   
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP