我正在使用ssh2-python在Cisco交换机上执行网络更改。似乎默认的stdout缓冲区大小有一个限制,并且想知道是否有一种方法可以覆盖它。
如果删除登录标语,脚本将配置交换机,但是使用标语,输出将在标语完成且未配置交换机之前停止。
是否可以增加标准输出大小?我尝试将channel.read(2048)
放入代码中,但这似乎无济于事。
这是我的代码:
import socket, os, time
from ssh2.session import Session
def credentials():
""" Read the switch username and password from a secured location. """
userpass = {}
with open("../secret/credentials.txt") as f:
for line in f:
(key, val) = line.split()
userpass[str(key)] = val
return userpass
def change(dict_detail):
""" This class change the port """
credential = credentials()
output = {}
switch_count = 0
while switch_count < len(dict_detail['switches']):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((
dict_detail['switches'][switch_count]['ip'],
dict_detail['switches'][switch_count]['ssh_port']
))
session = Session()
session.handshake(sock)
session.userauth_password(
credential['username'],
credential['password']
)
channel = session.open_session()
channel.shell()
channel.write('configure terminal\n',)
port_count = 0
while port_count < len(dict_detail['switches'][switch_count]['port']):
channel.write('interface ' + dict_detail['switches'][switch_count]['port'][port_count] + '\n')
channel.write('switchport access vlan ' + dict_detail['vlan'] + '\n')
time.sleep(1)
port_count = port_count + 1
size, data = channel.read()
print(data.decode())
channel.close()
print("Exit status: {0}".format(channel.get_exit_status()))
switch_count = switch_count + 1
def main():
''' Example of what will be feed to the API from the front-end app '''
mydict = {
'switches':
{
0:{
'ip': '192.168.1.150',
'netmask': '255.255.255.0',
'ssh_port': 22,
'port': { 0: 'FastEthernet0/12',
1: 'FastEthernet0/13',
2: 'FastEthernet0/14',
}
},
1:{
'ip': '192.168.1.150',
'netmask': '255.255.255.0',
'ssh_port': 22,
'port': { 0: 'FastEthernet0/10',
1: 'FastEthernet0/11',
}
},
},
'type': 'change',
'vlan': '50'
}
change(mydict)
if __name__ == "__main__":
main()
这是我的横幅输出:
$ python network.py
CCCCCC
/--------------------------------------------------------------------------\
| |
| ATTENTION !!! |
| |
| Only authorized users may use this system for legitimate |
| business purposes. There is no expectation of privacy in connection |
| with your activities, the information handled, sent or stored on |
| this network. By accessing this system you accept that your actions |
| on this network may be monitored and/or recorded. Information gathered |
| may be used to pursue any and all remedies available by law, including |
| termination of employment or the providing of the evidence of such |
| monitoring to law enforcement officials. |
| |
Exit status: 0
CCCCCC
/--------------------------------------------------------------------------\
| |
| ATTENTION !!! |
| |
| Only authorized users may use this system for legitimate |
| business purposes. There is no expectation of privacy in connection |
| with your activities, the information handled, sent or stored on |
| this network. By accessing this system you accept that your actions |
| on this network may be monitored and/or recorded. Information gathered |
| may be used to pursue any and all remedies available by law, including |
| termination of employment or the providing of the evidence of such |
| monitoring to law enforcement officials. |
| |
Exit status: 0
这里没有我的横幅输出:
$ python network.py
Baker-Switch-01#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Baker-Switch-01(config)#interface FastEthernet0/12
Baker-Switch-01(config-if)#switchport access vlan 50
Baker-Switch-01(config-if)#interface FastEthernet0/13
Baker-Switch-01(config-if)#switchport access vlan 50
Baker-Switch-01(config-if)#interface FastEthernet0/14
Baker-Switch-01(config-if)#switchport access vlan 50
Baker-Switch-01(config-if)#
Exit status: 0
Baker-Switch-01#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Baker-Switch-01(config)#interface FastEthernet0/10
Baker-Switch-01(config-if)#switchport access vlan 50
Baker-Switch-01(config-if)#interface FastEthernet0/11
Baker-Switch-01(config-if)#switchport access vlan 50
Baker-Switch-01(config-if)#
Exit status: 0