python paramiko执行ifconfig失败

问题:import paramiko远程ssh执行ifconfig,stdout返回结果为空?
解决问题:ifconfig换成‘/usr/sbin/ifconfig’(或者‘/sbin/ifconfig’)输入
原因:Linux的远程PATH和本地PATH路径不一样(再深好像和tty有关)
修改代码如下:

import requests
import json
import re
import traceback
import paramiko


def sshclient_execmd(hostname, port, username, password, execmd):
    s = paramiko.SSHClient()
    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    s.connect(hostname=hostname, port=port, username=username, password=password)
    stdin, stdout, stderr = s.exec_command(execmd)
    stdin.write("Y")
    #print 'shell-result:' + stdout.read()
    Resp = stdout.read()
    # s.close()
    return (Resp)


def network_Check():
    hostname = '10.1.1.1'
    port = 22
    username = 'admin'
    password = '********'
    cmd_list = ['cat /etc/sysconfig/network-scripts/ifcfg-eth0|grep HWADD', 'ping -c 5 8.8.8.8', '/usr/sbin/ifconfig']

    for execmd in cmd_list:
        print '*************************************************************'
        print '执行命令'+ ':' +execmd
        print '标准输出'+':' +sshclient_execmd(hostname, port, username, password, execmd)


if __name__ == "__main__":
    network_Check()

运行完显示

*************************************************************
执行命令:cat /etc/sysconfig/network-scripts/ifcfg-eth0|grep HWADD
标准输出:HWADDR=6c:92:bf:05:f0:e5

*************************************************************
执行命令:ping -c 5 8.8.8.8
标准输出:PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=2 ttl=31 time=50.6 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=31 time=50.6 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=31 time=50.5 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=31 time=50.6 ms

--- 8.8.8.8 ping statistics ---
5 packets transmitted, 4 received, 20% packet loss, time 4004ms
rtt min/avg/max/mdev = 50.581/50.636/50.698/0.043 ms

*************************************************************
执行命令:/usr/sbin/ifconfig
标准输出:eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.1.1.1 netmask 255.255.255.0  broadcast 255.255.255.0
        ether 22:92:bf:05:f0:22  txqueuelen 1000  (Ethernet)
        RX packets 721709  bytes 919828772 (877.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 338876  bytes 179805146 (171.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device memory 0xdf120000-df13ffff  

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 123.1.1.1  netmask 255.255.255.0  broadcast 123.1.1.255
        ether 33:92:bf:05:f0:11  txqueuelen 1000  (Ethernet)
        RX packets 12719  bytes 1512558 (1.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1034  bytes 184303 (179.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device memory 0xdf100000-df11ffff  

eth2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 1.1.1.22  netmask 255.255.255.0  broadcast 1.1.1.255
        ether 22:e0:ed:24:22:23  txqueuelen 1000  (Ethernet)
        RX packets 898  bytes 110454 (107.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 65  bytes 12883 (12.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 37  

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1  (Local Loopback)
        RX packets 304891  bytes 6656331413 (6.1 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 304891  bytes 6656331413 (6.1 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

猜你喜欢

转载自blog.csdn.net/u013908944/article/details/86256403