跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 请教下脚本大佬
未分類
26 5 月 2020

请教下脚本大佬

mushan099 大佬有话说 :

请教下脚本大佬

本帖最后由 mushan099 于 2020-5-26 10:10 编辑

家里整了台母鸡,在服务器上搭了kvm,装了一台模板机,用virsh define 命令来批量创建
xml文件要手动一个个去改uuid name mac 磁盘路径,求个sh脚本可以自动替换的yc019t ,送gcp香港台湾机器:D

test1.xml
<name>test1</name>
<uuid>9da1d06d-cc17-4a40-b36b-31dac8aac7c3</uuid>
<source file=’/var/lib/libvirt/images/test1.qcow2’/>
<source dev=’/dev/sdb1’/>
<mac address=’52:54:00:0d:90:d2’/>

上面是一个文件,有类似这样的test2 test3 test410来个
例如
test2.xml
<name>test2</name>
<uuid>9da1d06d-cc17-4a40-b36b-31dac8aac7c3</uuid># 格式固定字符随机
<source file=’/var/lib/libvirt/images/test2.qcow2’/>
<source dev=’/dev/sdb2’/>
<mac address=’52:54:00:0d:90:d2’/>#格式固定字符随机https://cdn.jsdelivr.net/gh/hishis/forum-master/public/images/patch.gif

van 大佬有话说 :

这些uuid name mac ,你都可以提供吗?

h20 大佬有话说 :

这个用sed就搞定了,什么uuid的linux都有现成的

mushan099 大佬有话说 :

van 大佬有话说 : 2020-5-26 10:13
这些uuid name mac ,你都可以提供吗?

uuid 其实我研究出来了,随便填只要格式对就可以的
sed -i "s/<uuid>.*</<uuid>$(uuidgen)</g" /root/bak/test6.xml
主要是mac那个搞不定:lol
想着大佬可能有更牛逼的方法,我就全抛上来了嘿嘿
mac也是随机生成就行,其他的那些按顺序 嘿嘿嘿https://cdn.jsdelivr.net/gh/hishis/forum-master/public/images/patch.gif

mushan099 大佬有话说 :

h20 大佬有话说 : 2020-5-26 10:14
这个用sed就搞定了,什么uuid的linux都有现成的

大佬是的,就是用sed,只是初学linux脚本写的一塌糊涂yc021thttps://cdn.jsdelivr.net/gh/hishis/forum-master/public/images/patch.gif

marile 大佬有话说 :

本帖最后由 marile 于 2020-5-26 10:27 编辑

https://help.ubuntu.com/community/KVM/Managing
里面提到了:
To get a new mac address to paste into your xml file, use this command:

MACADDR="52:54:$(dd if=/dev/urandom count=1 2>/dev/null | md5sum | sed ‘s/^(..)(..)(..)(..).*$/1:2:3:4/’)"; echo $MACADDR

To get a new uuid for your xml file, use: uuidgen

https://superuser.com/questions/218340/how-to-generate-a-valid-random-mac-address-with-bash-shell
这里有一些别的办法

h20 大佬有话说 :

mushan099 大佬有话说 : 2020-5-26 10:17
uuid 其实我研究出来了,随便填只要格式对就可以的
主要是mac那个搞不定
想着大佬可能有更牛逼的方法 …

你这就是思维江化

你为什么不用个占位符先写好一个模板文件,然后替换这个占位符就好了,还去整什么正则表达式不是自找麻烦吗?

mushan099 大佬有话说 :

h20 大佬有话说 : 2020-5-26 10:21
你这就是思维江化

你为什么不用个占位符先写好一个模板文件,然后替换这个占位符就好了,还去整什么正则 …

哎好像是这个道理。。重点不是匹配啊大佬,重点是替换:Dhttps://cdn.jsdelivr.net/gh/hishis/forum-master/public/images/patch.gif

h20 大佬有话说 :

mushan099 大佬有话说 : 2020-5-26 10:22
哎好像是这个道理。。重点不是匹配啊大佬,重点是替换

十六进制你就先生成一个0-255的随机数,然后printf "%x" $rand

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具