远程运行批处理文件的记录输出

时间:2016-03-15 02:58:17

标签: python batch-file paramiko

我想使用paramiko来远程控制bat文件,并获取文件输出。但它不起作用。

    def ssh2(username, password, hostname):
        if __name__ == '__main__':
           paramiko.util.log_to_file('paramiko.log')
           ssh = paramiko.SSHClient()
           ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
           ssh.connect(hostname = hostname, username = username, password = password)
           cmd = './tmp/batch_ping_dce.bat'
           stdin,stdout,stderr = ssh.exec_command(cmd,timeout = 10)
           print stdout.read()
           ssh.close()

Windows服务器已经安装了bat文件所在的ssh服务器。 paramiko.log是这样的:

DEB [20160315-10:53:26.700] thr=1   paramiko.transport: starting thread (client mode): 0x2b3acc0L
DEB [20160315-10:53:26.700] thr=1   paramiko.transport: Local version/idstring: SSH-2.0-paramiko_1.16.0
DEB [20160315-10:53:26.809] thr=1   paramiko.transport: Remote version/idstring: SSH-2.0-OpenSSH_6.1
INF [20160315-10:53:26.809] thr=1   paramiko.transport: Connected (version 2.0, client OpenSSH_6.1)
DEB [20160315-10:53:26.855] thr=1   paramiko.transport: kex algos:[u'ecdh-sha2-nistp256', u'ecdh-sha2-nistp384', u'ecdh-sha2-nistp521', u'diffie-hellman-group-exchange-sha256', u'diffie-hellman-group-exchange-sha1', u'diffie-hellman-group14-sha1', u'diffie-hellman-group1-sha1'] server key:[u'ssh-rsa', u'ssh-dss', u'ecdsa-sha2-nistp256'] client encrypt:[u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'arcfour256', u'arcfour128', u'aes128-cbc', u'3des-cbc', u'blowfish-cbc', u'cast128-cbc', u'aes192-cbc', u'aes256-cbc', u'arcfour', u'rijndael-cbc@lysator.liu.se'] server encrypt:[u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'arcfour256', u'arcfour128', u'aes128-cbc', u'3des-cbc', u'blowfish-cbc', u'cast128-cbc', u'aes192-cbc', u'aes256-cbc', u'arcfour', u'rijndael-cbc@lysator.liu.se'] client mac:[u'hmac-md5', u'hmac-sha1', u'umac-64@openssh.com', u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-ripemd160', u'hmac-ripemd160@openssh.com', u'hmac-sha1-96', u'hmac-md5-96'] server mac:[u'hmac-md5', u'hmac-sha1', u'umac-64@openssh.com', u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-ripemd160', u'hmac-ripemd160@openssh.com', u'hmac-sha1-96', u'hmac-md5-96'] client compress:[u'none', u'zlib@openssh.com'] server compress:[u'none', u'zlib@openssh.com'] client lang:[u''] server lang:[u''] kex follows?False
DEB [20160315-10:53:26.855] thr=1   paramiko.transport: Kex agreed: diffie-hellman-group1-sha1
DEB [20160315-10:53:26.855] thr=1   paramiko.transport: Cipher agreed: aes128-ctr
DEB [20160315-10:53:26.855] thr=1   paramiko.transport: MAC agreed: hmac-sha2-256
DEB [20160315-10:53:26.855] thr=1   paramiko.transport: Compression agreed: none
DEB [20160315-10:53:26.917] thr=1   paramiko.transport: kex engine KexGroup1 specified hash_algo <built-in function openssl_sha1>
DEB [20160315-10:53:26.917] thr=1   paramiko.transport: Switch to new keys ...
DEB [20160315-10:53:26.934] thr=2   paramiko.transport: Adding ssh-rsa host key for 172.17.2.33: 377447af08a1f74e1c25582050e20d83
DEB [20160315-10:53:27.137] thr=1   paramiko.transport: userauth is OK
INF [20160315-10:53:31.980] thr=1   paramiko.transport: Authentication (password) successful!
DEB [20160315-10:53:31.996] thr=2   paramiko.transport: [chan 0] Max packet in: 32768 bytes
DEB [20160315-10:53:31.996] thr=1   paramiko.transport: [chan 0] Max packet out: 32768 bytes
DEB [20160315-10:53:31.996] thr=1   paramiko.transport: Secsh channel 0 opened.
DEB [20160315-10:53:32.059] thr=1   paramiko.transport: [chan 0] Sesch channel 0 request ok
DEB [20160315-10:53:44.214] thr=1   paramiko.transport: EOF in transport thread

那么我如何使用python来完成这项工作呢?

0 个答案:

没有答案