network:shell_scripting
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
network:shell_scripting [14.09.2017 13:55] – richard | network:shell_scripting [04.02.2025 11:59] (aktuell) – Externe Bearbeitung 127.0.0.1 | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
- | ~~DISCUSSION|Ergänzungen~~ | + | ~~DISCUSSION:closed|Ergänzungen~~ |
====== Kurzeinführung in Skripting ====== | ====== Kurzeinführung in Skripting ====== | ||
Zeile 180: | Zeile 180: | ||
print (testVar) | print (testVar) | ||
</ | </ | ||
+ | |||
+ | |||
+ | ===== ssh mit python ===== | ||
+ | |||
+ | Es gibt mehrere Implementierungen für ssh in python. Das Module '' | ||
+ | |||
+ | Installation: | ||
+ | < | ||
+ | python3 -m pip --proxy https:// | ||
+ | </ | ||
+ | |||
+ | Beispiel-Script: | ||
+ | <file python ssh_paramiko_test.py> | ||
+ | # https:// | ||
+ | |||
+ | import paramiko | ||
+ | USERNAME=' | ||
+ | PASSWORD=' | ||
+ | HOST=' | ||
+ | |||
+ | ssh = paramiko.client.SSHClient() | ||
+ | ssh.set_missing_host_key_policy( | ||
+ | | ||
+ | |||
+ | ssh.connect(HOST, | ||
+ | stdin, stdout, stderr = ssh.exec_command(' | ||
+ | while not stdout.channel.exit_status_ready(): | ||
+ | # Print data when available | ||
+ | if stdout.channel.recv_ready(): | ||
+ | alldata = stdout.channel.recv(1024) | ||
+ | prevdata = b" | ||
+ | while prevdata: | ||
+ | prevdata = stdout.channel.recv(1024) | ||
+ | alldata += prevdata | ||
+ | |||
+ | print(" | ||
+ | |||
+ | stdin, stdout, stderr = ssh.exec_command(' | ||
+ | while not stdout.channel.exit_status_ready(): | ||
+ | # Print data when available | ||
+ | if stdout.channel.recv_ready(): | ||
+ | alldata = stdout.channel.recv(1024) | ||
+ | prevdata = b" | ||
+ | while prevdata: | ||
+ | prevdata = stdout.channel.recv(1024) | ||
+ | alldata += prevdata | ||
+ | |||
+ | print(" | ||
+ | </ | ||
+ | |||
+ | <file python ssh_ssh_test.py> | ||
+ | from ssh.session import Session | ||
+ | from ssh import options | ||
+ | # Username für Login | ||
+ | USERNAME= ' | ||
+ | # Zielmaschine hier mikrotik | ||
+ | HOST = ' | ||
+ | |||
+ | # Aufbau der ssh-Session | ||
+ | s = Session() | ||
+ | s.options_set(options.HOST, | ||
+ | s.connect() | ||
+ | |||
+ | # Login mit USERNAME | ||
+ | s.userauth_agent(USERNAME) | ||
+ | |||
+ | chan = s.channel_new() | ||
+ | chan.open_session() | ||
+ | chan.request_exec(' | ||
+ | """ | ||
+ | # Ausgabe der Antwort | ||
+ | size, data = chan.read() | ||
+ | while size > 0: | ||
+ | print(data.strip()) | ||
+ | size, data = chan.read() | ||
+ | """ | ||
+ | chan.request_exec(' | ||
+ | chan.request_exec(' | ||
+ | |||
+ | # Ausgabe der Antwort | ||
+ | size, data = chan.read() | ||
+ | while size > 0: | ||
+ | print(data.strip()) | ||
+ | size, data = chan.read() | ||
+ | |||
+ | chan.close() | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== ssh über bash ===== | ||
+ | |||
+ | <code bash ssh_test.sh> | ||
+ | #!/bin/bash | ||
+ | |||
+ | user=" | ||
+ | host=" | ||
+ | ip_addr=" | ||
+ | |||
+ | ssh $user@$host<< | ||
+ | /ip address print | ||
+ | /ip address add address=${ip_addr}/ | ||
+ | /ip address print | ||
+ | EOF | ||
+ | </ |
network/shell_scripting.1505397343.txt.gz · Zuletzt geändert: 04.02.2025 12:02 (Externe Bearbeitung)