英文:
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.
#!/bin/bash
file_closed() {
FILENAME=$(echo $FILE | grep -o '^.*\.txt')
OUTPUT=$(eval $FILENAME)
mv "$DIRECTORY/$FILE" "$DIRECTORY/$OUTPUT"
}
git_commit() {
DATE_FMT=$(date +"%Y-%m-%d %H:%M:%S")
cd /var/lib/tftpboot/$DIRECTORY
git add .
git commit -m "Config updated ($DATE_FMT)"
git push
}
inotifywait -q -m -r -e close_write $1 | while read DIRECTORY EVENT FILE; do
case $EVENT in
CLOSE*)
file_closed "$DIRECTORY" "$FILE"
git_commit
;;
esac
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.
#!/bin/bash
file_closed() {
FILENAME="echo $FILE | grep -o '^.*\.txt'"
OUTPUT=$(eval $FILENAME)
mv "$DIRECTORY/$FILE" "$DIRECTORY/$OUTPUT"
}
git_commit() {
DATE_FMT=$(date +"%Y-%m-%d %H:%M:%S")
cd /var/lib/tftpboot/$DIRECTORY
git add .
git commit -m "Config updated ($DATE_FMT)"
git push
}
inotifywait -q -m -r -e close_write $1 | while read DIRECTORY EVENT FILE; do
case $EVENT in
CLOSE*)
file_closed "$DIRECTORY" "$FILE"
git_commit
;;
esac
done
Executing the script will commit the changes to GIT bit also get stuck in a loop trying to "mv" other files..
root@server:/var/lib/tftpboot# ./monitor_tftp.sh router_configs/Warriewood
[main d229d3c] Config updated (2023-07-20 14:22:05)
2 files changed, 1069 insertions(+), 1 deletion(-)
create mode 100644 PHC-16JUB-SW01.txt-Jul-20-2023-04-13-57.744-UTC-30
Counting objects: 4, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 452 bytes | 452.00 KiB/s, done.
Total 4 (delta 2), reused 0 (delta 0)
To http://gitlab.domain.local/router-configs/Warriewood.git
54533ee..d229d3c main -> main
mv: cannot stat 'router_configs/Warriewood/.git/objects/7b//tmp_obj_HDELHr': No such file or directory
fatal: this operation must be run in a work tree
fatal: this operation must be run in a work tree
Everything up-to-date
mv: cannot stat 'router_configs/Warriewood/.git/objects/66//tmp_obj_95TDBr': No such file or directory
fatal: this operation must be run in a work tree
fatal: this operation must be run in a work tree
Everything up-to-date
mv: cannot stat 'router_configs/Warriewood/.git//index.lock': No such file or directory
fatal: this operation must be run in a work tree
fatal: this operation must be run in a work tree
Everything up-to-date
mv: cannot stat 'router_configs/Warriewood/.git/objects/c3//tmp_obj_6wezzq': No such file or directory
fatal: this operation must be run in a work tree
fatal: this operation must be run in a work tree
Everything up-to-date
mv: cannot stat 'router_configs/Warriewood/.git//index.lock': No such file or directory
fatal: this operation must be run in a work tree
fatal: this operation must be run in a work tree
Everything up-to-date
mv: cannot stat 'router_configs/Warriewood/.git//COMMIT_EDITMSG': No such file or directory
fatal: this operation must be run in a work tree
fatal: this operation must be run in a work tree
Everything up-to-date
mv: cannot stat 'router_configs/Warriewood/.git/objects/d2//tmp_obj_YefZCq': No such file or directory
fatal: this operation must be run in a work tree
fatal: this operation must be run in a work tree
Everything up-to-date
mv: cannot stat 'router_configs/Warriewood/.git//HEAD.lock': No such file or directory
fatal: this operation must be run in a work tree
fatal: this operation must be run in a work tree
Everything up-to-date
mv: cannot stat 'router_configs/Warriewood/.git/refs/heads//main.lock': No such file or directory
fatal: this operation must be run in a work tree
fatal: this operation must be run in a work tree
Everything up-to-date
mv: cannot stat 'router_configs/Warriewood/.git/logs//HEAD': No such file or directory
fatal: this operation must be run in a work tree
fatal: this operation must be run in a work tree
Everything up-to-date
mv: cannot stat 'router_configs/Warriewood/.git/logs/refs/heads//main': No such file or directory
fatal: this operation must be run in a work tree
fatal: this operation must be run in a work tree
Everything up-to-date
mv: cannot stat 'router_configs/Warriewood/.git/refs/remotes/origin//main.lock': No such file or directory
fatal: this operation must be run in a work tree
fatal: this operation must be run in a work tree
Everything up-to-date
mv: cannot stat 'router_configs/Warriewood/.git/logs/refs/remotes/origin//main': No such file or directory
fatal: this operation must be run in a work tree
fatal: this operation must be run in a work tree
Everything up-to-date
答案1
得分: 0
感谢Kurtis Rader的建议。我运行了以下代码,它完美地工作。
#!/bin/bash
file_closed() {
local FILE="$1"
local FILENAME="${FILE%%.txt-*}.txt"
mv "$DIRECTORY/$FILE" "$DIRECTORY/$FILENAME"
}
git_commit() {
local DIRECTORY="$1"
local DATE_FMT=$(date +"%Y-%m-%d %H:%M:%S")
cd "/var/lib/tftpboot/$DIRECTORY"
git add .
git commit -m "Config updated ($DATE_FMT)"
git push
}
while read -r DIRECTORY EVENT FILE; do
case $EVENT in
CLOSE*)
file_closed "$FILE"
git_commit "$DIRECTORY"
;;
esac
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.
#!/bin/bash
file_closed() {
local FILE="$1"
local FILENAME="${FILE%%.txt-*}.txt"
mv "$DIRECTORY/$FILE" "$DIRECTORY/$FILENAME"
}
git_commit() {
local DIRECTORY="$1"
local DATE_FMT=$(date +"%Y-%m-%d %H:%M:%S")
cd "/var/lib/tftpboot/$DIRECTORY"
git add .
git commit -m "Config updated ($DATE_FMT)"
git push
}
while read -r DIRECTORY EVENT FILE; do
case $EVENT in
CLOSE*)
file_closed "$FILE"
git_commit "$DIRECTORY"
;;
esac
done < <(inotifywait -q -m -e close_write --format '%w %e %f' "$1")
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论