abbbi / xtdcliconf

Drop into HP Comware switches Extended CLI mode directly
2 stars 1 forks source link

Automating switch-config-export via crontab/bash #1

Open FJerusalem opened 4 years ago

FJerusalem commented 4 years ago

Hi, I'm having the following problem: I have created a wrapper-script around your script to automate config-backups. lets all it "runbackup.sh"

#!/bin/bash rm -f /opt/xtdcliconf/configs/* /usr/bin/python3 /opt/xtdcliconf/xtdcliconf.py --host myswitch1 --user admin --password mysupersecretpassword --no-systemview --execute /opt/xtdcliconf/backup-config | /usr/bin/tee /opt/xtdcliconf/configs/myswitch1

When I execute the bash-script in a running terminal I am able to capture the output with tee. But: using cron it fails as following:

/etc/crontab: SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin ... 45 * * * * xtdcliconf /bin/bash -c "/opt/xtdcliconf/runbackup.sh" >> /var/log/myjob.log 2>&1 or 45 * * * * xtdcliconf /opt/xtdcliconf/runbackup.sh or 45 * * * * root sudo -u xtdcliconf /opt/xtdcliconf/runbackup.sh or 45 * * * * root /opt/xtdcliconf/runbackup.sh

xtdcliconf:execute_xtd_cli(): Configured switch supports XTD CLI Cmd, switch to extended cli mode with cmd: "xtd-cli-mode" xtdcliconf:execute_cmdfile(): executing commands from file: /opt/xtdcliconf/backup-config Traceback (most recent call last): File "/opt/xtdcliconf/xtdcliconf.py", line 204, in <module> interact.take_control() File "/usr/local/lib/python3.6/site-packages/paramiko_expect.py", line 288, in take_control original_tty = termios.tcgetattr(sys.stdin) termios.error: (25, 'Inappropriate ioctl for device')

Already tried to switch to script instead of tee, using pipe for stdout-redirection, changed the cron-parameters, env-vars, "python -u", etc. This seems to be a python-related issue. Any idea on how to get around this or provide an optional command-parameter to save the config to a file?

Any help would be highly appreciated.

Best regards Florian Jerusalem

abbbi commented 4 years ago

hi,

the script uses paramiko-expect to be able to login onto the switches. Expect fetches the switch output, parses it and sends the right answer message to the switch. This all only works on a shell with TTY. The issue here is that CRON executes its command in a non-interactive manner, as such no TTY is present and paramiko-expect fails to access the device.

Im not sure if theres a way to make it work with CRON, you would have to search the web for expect + cron/tty issues.

FJerusalem commented 4 years ago

Thanks for the fast reply. Got this solved now. Thanks for pushing me into the right direction. Using tmux as wrapper:

$ cat runbackup.sh

!/bin/bash

source ~/.bashrc rm -f /opt/xtdcliconf/configs/*

until [ -s /opt/xtdcliconf/configs/switch1 ]; do /usr/bin/tmux new-session -d -s xtd /usr/bin/tmux send-keys -t xtd "/usr/bin/python3 /opt/xtdcliconf/xtdcliconf.py --host switch1 --user admin --password secret --no-systemview --execute /opt/xtdcliconf/backup-config | /usr/bin/tee /opt/xtdcliconf/configs/switch1" C-m sleep 20 /usr/bin/tmux kill-session -t xtd sleep 5 done

until [ -s /opt/xtdcliconf/configs/switch2 ]; do /usr/bin/tmux new-session -d -s xtd /usr/bin/tmux send-keys -t xtd "/usr/bin/python3 /opt/xtdcliconf/xtdcliconf.py --host switch2 --user admin --password secret2 --no-systemview --execute /opt/xtdcliconf/backup-config | /usr/bin/tee /opt/xtdcliconf/configs/switch2" C-m sleep 20 /usr/bin/tmux kill-session -t xtd sleep 5 done

$ cat /etc/crontab ... 30 xtdcliconf /opt/xtdcliconf/runbackup.sh