Script mit dem man sich auf einem Cisco-Switch einloggen kann oder mal schnell ein Kommando absetzen kann.
Voraussetzung: Eine Passwort-Datei im YAML-Format, die man natürlich auf einem verschlüsseltem Datenträger lagert denn man nur dafür öffnet:
$HOME/.config/encrypt/switch.yaml
--- # config / password file for switch scripts in yaml format userpw: "1totalSCHWERESundLANGESpasswort!!1elf" user: "annabeispiel"
$HOME/bin/switch
#!/usr/bin/env python3 """ python script for - opening a interactive session with a cisco switch or - set commands to a cisco switch """ # pylint: disable=no-member import sys import os import socket import yaml import pexpect # where the password file is stored (encrypted disk!) CONFIGFILE = os.path.expanduser("~")+"/.config/encrypt/switch.yaml" def do_telnet(geraet, user, command): """do something via telnet""" print("### telnet to %s as user %s ..." % (geraet, user)) ## with pexpect instead of telnet lib child = pexpect.spawn('telnet ' + geraet) child.expect('.*sername:.*') child.sendline(user) child.expect('.*assword:.*') child.sendline(PASSWORD) child.expect('.*#') child.sendline('terminal length 0') if command: child.sendline(command) child.expect('.*#') child.expect('.*#') output1 = child.before output2 = child.after # decode, because the return is byte and not string print(output1.decode("UTF-8")) print(output2.decode("UTF-8")) else: child.interact() child.sendline('exit') child.close() print("### end of telnet session with %s" % geraet) def do_ssh(geraet, user, command): """do something via ssh""" print("### ssh to %s as user %s ..." % (geraet, user)) child = pexpect.spawn('ssh %s@%s' % (user, geraet)) child.expect('.*assword:.*') child.sendline(PASSWORD) child.expect('.*#') child.sendline('terminal length 0') if command: child.sendline(command) child.expect('.*#') child.expect('.*#') output1 = child.before output2 = child.after print(output1.decode("UTF-8")) print(output2.decode("UTF-8")) else: child.interact() child.sendline('exit') child.close() print("### end of ssh session with %s" % geraet) # read config CFG = yaml.load(open(CONFIGFILE)) PASSWORD = CFG['userpw'] USER = CFG['user'] GERAET = sys.argv[1] COMMAND = "" try: COMMAND = sys.argv[2] except IndexError: pass # check for ssh (port 22) SOCK1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) I_DO_SSH = not SOCK1.connect_ex((GERAET, 22)) # check for telnet (port 23) SOCK2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) I_DO_TELNET = not SOCK2.connect_ex((GERAET, 23)) if I_DO_SSH: #print "ssh works on %s " % GERAET do_ssh(GERAET, USER, COMMAND) elif I_DO_TELNET: #print "telnet works on %s" % GERAET do_telnet(GERAET, USER, COMMAND) sys.exit(0) else: print("neither ssh nor telnet port open on %s" % GERAET) ## FINI
gilt für alle Tipps, Tricks & Spickzettel:
dies sind einfache, teils banale Notizen für meinen persönlichen Gebrauch,
die hier eher zufällig auch öffentlich lesbar sind
(vielleicht hilft es ja jemandem weiter). Verwendung auf eigene Gefahr
Fehler-Hinweise, Dankesschreiben , etc. bitte an: web.21@unixwitch.de
weitere Tools / Spickzettel