Bash脚本用于重命名文件,然后将文件(路由器配置)从TFTP同步到Git。

huangapple go评论84阅读模式
英文:

Bash Script to Rename Files then Sync Files (router configs) from TFTP to Git

问题

I created a script so when a cisco router config is saved its sent to a TFTP server, i would like to rename the config (as cisco appends the time) from
PHC-16JUB-SW01.txt-Jul-20-2023-04-13-57.744-UTC-30
to
PHC-16JUB-SW01.txt
I am doing this so when i commit to GIT its not a "unique" file.

Once the file has been renamed, its then sent to GIT for auditing/backup

im not proficient in coding, this is my best try from all the SO and github scripts out there.

  1. #!/bin/bash
  2. file_closed() {
  3. FILENAME=$(echo $FILE | grep -o '^.*\.txt')
  4. OUTPUT=$(eval $FILENAME)
  5. mv "$DIRECTORY/$FILE" "$DIRECTORY/$OUTPUT"
  6. }
  7. git_commit() {
  8. DATE_FMT=$(date +"%Y-%m-%d %H:%M:%S")
  9. cd /var/lib/tftpboot/$DIRECTORY
  10. git add .
  11. git commit -m "Config updated ($DATE_FMT)"
  12. git push
  13. }
  14. inotifywait -q -m -r -e close_write $1 | while read DIRECTORY EVENT FILE; do
  15. case $EVENT in
  16. CLOSE*)
  17. file_closed "$DIRECTORY" "$FILE"
  18. git_commit
  19. ;;
  20. esac
  21. done

Executing the script will commit the changes to GIT but also get stuck in a loop trying to "mv" other files.

英文:

I created a script so when a cisco router config is saved its sent to a TFTP server, i would like to rename the config (as cisco appends the time) from
PHC-16JUB-SW01.txt-Jul-20-2023-04-13-57.744-UTC-30
to
PHC-16JUB-SW01.txt
I am doing this so when i commit to GIT its not a "unique" file.

Once the file has been renamed, its then sent to GIT for auditing/backup

im not proficient in coding, this is my best try from all the SO and github scripts out there.

  1. #!/bin/bash
  2. file_closed() {
  3. FILENAME="echo $FILE | grep -o '^.*\.txt'"
  4. OUTPUT=$(eval $FILENAME)
  5. mv "$DIRECTORY/$FILE" "$DIRECTORY/$OUTPUT"
  6. }
  7. git_commit() {
  8. DATE_FMT=$(date +"%Y-%m-%d %H:%M:%S")
  9. cd /var/lib/tftpboot/$DIRECTORY
  10. git add .
  11. git commit -m "Config updated ($DATE_FMT)"
  12. git push
  13. }
  14. inotifywait -q -m -r -e close_write $1 | while read DIRECTORY EVENT FILE; do
  15. case $EVENT in
  16. CLOSE*)
  17. file_closed "$DIRECTORY" "$FILE"
  18. git_commit
  19. ;;
  20. esac
  21. done

Executing the script will commit the changes to GIT bit also get stuck in a loop trying to "mv" other files..

  1. root@server:/var/lib/tftpboot# ./monitor_tftp.sh router_configs/Warriewood
  2. [main d229d3c] Config updated (2023-07-20 14:22:05)
  3. 2 files changed, 1069 insertions(+), 1 deletion(-)
  4. create mode 100644 PHC-16JUB-SW01.txt-Jul-20-2023-04-13-57.744-UTC-30
  5. Counting objects: 4, done.
  6. Compressing objects: 100% (4/4), done.
  7. Writing objects: 100% (4/4), 452 bytes | 452.00 KiB/s, done.
  8. Total 4 (delta 2), reused 0 (delta 0)
  9. To http://gitlab.domain.local/router-configs/Warriewood.git
  10. 54533ee..d229d3c main -> main
  11. mv: cannot stat 'router_configs/Warriewood/.git/objects/7b//tmp_obj_HDELHr': No such file or directory
  12. fatal: this operation must be run in a work tree
  13. fatal: this operation must be run in a work tree
  14. Everything up-to-date
  15. mv: cannot stat 'router_configs/Warriewood/.git/objects/66//tmp_obj_95TDBr': No such file or directory
  16. fatal: this operation must be run in a work tree
  17. fatal: this operation must be run in a work tree
  18. Everything up-to-date
  19. mv: cannot stat 'router_configs/Warriewood/.git//index.lock': No such file or directory
  20. fatal: this operation must be run in a work tree
  21. fatal: this operation must be run in a work tree
  22. Everything up-to-date
  23. mv: cannot stat 'router_configs/Warriewood/.git/objects/c3//tmp_obj_6wezzq': No such file or directory
  24. fatal: this operation must be run in a work tree
  25. fatal: this operation must be run in a work tree
  26. Everything up-to-date
  27. mv: cannot stat 'router_configs/Warriewood/.git//index.lock': No such file or directory
  28. fatal: this operation must be run in a work tree
  29. fatal: this operation must be run in a work tree
  30. Everything up-to-date
  31. mv: cannot stat 'router_configs/Warriewood/.git//COMMIT_EDITMSG': No such file or directory
  32. fatal: this operation must be run in a work tree
  33. fatal: this operation must be run in a work tree
  34. Everything up-to-date
  35. mv: cannot stat 'router_configs/Warriewood/.git/objects/d2//tmp_obj_YefZCq': No such file or directory
  36. fatal: this operation must be run in a work tree
  37. fatal: this operation must be run in a work tree
  38. Everything up-to-date
  39. mv: cannot stat 'router_configs/Warriewood/.git//HEAD.lock': No such file or directory
  40. fatal: this operation must be run in a work tree
  41. fatal: this operation must be run in a work tree
  42. Everything up-to-date
  43. mv: cannot stat 'router_configs/Warriewood/.git/refs/heads//main.lock': No such file or directory
  44. fatal: this operation must be run in a work tree
  45. fatal: this operation must be run in a work tree
  46. Everything up-to-date
  47. mv: cannot stat 'router_configs/Warriewood/.git/logs//HEAD': No such file or directory
  48. fatal: this operation must be run in a work tree
  49. fatal: this operation must be run in a work tree
  50. Everything up-to-date
  51. mv: cannot stat 'router_configs/Warriewood/.git/logs/refs/heads//main': No such file or directory
  52. fatal: this operation must be run in a work tree
  53. fatal: this operation must be run in a work tree
  54. Everything up-to-date
  55. mv: cannot stat 'router_configs/Warriewood/.git/refs/remotes/origin//main.lock': No such file or directory
  56. fatal: this operation must be run in a work tree
  57. fatal: this operation must be run in a work tree
  58. Everything up-to-date
  59. mv: cannot stat 'router_configs/Warriewood/.git/logs/refs/remotes/origin//main': No such file or directory
  60. fatal: this operation must be run in a work tree
  61. fatal: this operation must be run in a work tree
  62. Everything up-to-date

答案1

得分: 0

感谢Kurtis Rader的建议。我运行了以下代码,它完美地工作。

  1. #!/bin/bash
  2. file_closed() {
  3. local FILE="$1"
  4. local FILENAME="${FILE%%.txt-*}.txt"
  5. mv "$DIRECTORY/$FILE" "$DIRECTORY/$FILENAME"
  6. }
  7. git_commit() {
  8. local DIRECTORY="$1"
  9. local DATE_FMT=$(date +"%Y-%m-%d %H:%M:%S")
  10. cd "/var/lib/tftpboot/$DIRECTORY"
  11. git add .
  12. git commit -m "Config updated ($DATE_FMT)"
  13. git push
  14. }
  15. while read -r DIRECTORY EVENT FILE; do
  16. case $EVENT in
  17. CLOSE*)
  18. file_closed "$FILE"
  19. git_commit "$DIRECTORY"
  20. ;;
  21. esac
  22. done < <(inotifywait -q -m -e close_write --format '%w %e %f' "$1")

在这个修正后的版本中,我们正确传递了DIRECTORY和FILE变量给函数。我们使用参数扩展来提取不带时间戳的基本文件名,而不是在file_closed函数中。git_commit函数现在接受DIRECTORY作为参数。inotifywait命令使用--format选项来获取有关事件的必要信息。

英文:

thanks to Kurtis Rader suggestion.. i ran the code through ChatGPT and got the below code, which works flawlessly.

  1. #!/bin/bash
  2. file_closed() {
  3. local FILE=&quot;$1&quot;
  4. local FILENAME=&quot;${FILE%%.txt-*}.txt&quot;
  5. mv &quot;$DIRECTORY/$FILE&quot; &quot;$DIRECTORY/$FILENAME&quot;
  6. }
  7. git_commit() {
  8. local DIRECTORY=&quot;$1&quot;
  9. local DATE_FMT=$(date +&quot;%Y-%m-%d %H:%M:%S&quot;)
  10. cd &quot;/var/lib/tftpboot/$DIRECTORY&quot;
  11. git add .
  12. git commit -m &quot;Config updated ($DATE_FMT)&quot;
  13. git push
  14. }
  15. while read -r DIRECTORY EVENT FILE; do
  16. case $EVENT in
  17. CLOSE*)
  18. file_closed &quot;$FILE&quot;
  19. git_commit &quot;$DIRECTORY&quot;
  20. ;;
  21. esac
  22. done &lt; &lt;(inotifywait -q -m -e close_write --format &#39;%w %e %f&#39; &quot;$1&quot;)

In this corrected version, we pass the DIRECTORY and FILE variables correctly to the functions. We use parameter expansion to extract the base filename without the timestamp in the file_closed function. The git_commit function now accepts the DIRECTORY as an argument. The inotifywait command is used with the --format option to get the necessary information about the event.

huangapple
  • 本文由 发表于 2023年7月20日 12:38:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726707.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定