首先使用paramiko模块,封装连接服务器,输入命令方法,使用read()读取命令输入后的返回值,并返回为字符串
def server_conn(ip,command):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=ip,username='', password="")
stdin, stdout, stderr = ssh.exec_command(command,get_pty=True)
result = stdout.read()
ssh.close()
return result
检测网卡,返回为一个列表
# 检测服务器中已连接上的网卡
def eth_check(ip):
eth_list=[]
read_list = server_conn(ip, 'ifconfig').splitlines()
for i in range(len(read_list)):
if 'eth' in read_list[i]:
if 'RUNNING' in read_list[i+1]:
eth_list.append(read_list[i].split()[0])
检测情况输出:
['eth0', 'eth1', 'eth2']
附:使用ethtool修改网卡速率为100Mb/s
# 修改网卡速度为100Mb/s命令
edit_netspeed_command= u'ethtool -s {} speed 100 duplex full'
def netspeed_edit(ip):
eth_check(ip)
server_conn(ip, edit_netspeed_command.format(eth_list[0]))