笔记整理1.21-免互交-正则符号-sed命令-cut命令-脚本执行检查-全文替换
免互交
yum -y install expect
vi p.sh
#!/usr/bin/expect set ipaddr "192.168.1.63" set name "root" set passwd "guochao77!" set timeout 30 #设置超时时间默认timeout为10s。 spawn ssh $name@$ipaddr # spawn是进入expect后的命令 expect { "yes/no" { send "yes\r";exp_continue } "password" { send "$passwd\r" } } expect "#" #判断上次输出结果里是否包含“password:”的字符串,如果有则立即返回,向下执行;否则就一直等待,直到超时时间到 send "touch /root/xuegod1011.txt\r" send "ls /etc > /root/xuegod1011.txt\r" send "mkdir /zsl\r" send "exit\r" expect eof #执行完成上述命令后,退出Expect,把控制权交给控制台,变回手工操作
expect ssh.exp #开始执行也可以是.sh格式
expect p.sh
[root@xuegod63 ~]# cat ip_pass.txt #这里写上要执行的IP地址和root用户密码
192.168.1.63 123456
192.168.1.63 123456
[root@xuegod63 ~]# cat ssh2.exp #编写要执行的操作
#!/usr/bin/expect set ipaddr [lindex $argv 0] set passwd [lindex $argv 1] set timeout 30 spawn ssh root@$ipaddr expect { "yes/no" { send "yes\r";exp_continue } "password" { send "$passwd\r" } } expect "#" send "touch /root/xuegod1011.txt\r" send "ls /etc > /root/xuegod1011.txt\r" send "mkdir /tmp/xuegod1011\r" send "exit\r" expect eof
[root@xuegod63 ~]# cat login.sh #开始执行
#!/bin/bash echo for ip in `awk '{print $1}' /root/ip_pass.txt` do pass=`grep $ip /root/ip_pass.txt|awk '{print $2}'` expect /root/ssh2.exp $ip $pass done
$ 匹配结尾字符
( ) 标记子表达式
* 零次或多次
+ 一次或多次
. \n 之外的任何单字符
[ 表达式的开始
? 子表达式零次或一次
\ 转义
^ 匹配开始字符
{ 标记限定符表达式的开始
| 指明两项之间的一个选择
定位符
^ 匹配输入字符串开始的位置
$ 匹配输入字符串结尾的位置
非打印字符
\n 匹配一个换行符
\r 匹配一个回车符
\t 匹配一个制表符
egrep -v "^$|^#" /etc/ssh/sshd_config #扩展正则表达式
sed的执行过程:
1、一次读取一行数据
2、根据我们提供的规则来匹配相关的数据,比如查找root。
3、按照命令修改数据流中的数据,比如替换
4、将结果进行输出
5、重复上面四步
sed
-a 在当前行下面插入文件
-n 读取下一个输入行,用下一个命令处理新的行而不是用第一个命令
-i 编辑文件内容 ***
-r 使用扩展的正则表达式
替换标记:
数字:表明新文本将替换第几处模式匹配的地方
g:表示新文本将会替换所有匹配的文本
\1:子串匹配标记,前面搜索可以用元字符集\(..\),
&:保留搜索到的字符用来替换其它字符
i:忽略大小写
sed匹配字符集
^ 匹配行开始
$ 匹配行结束
. 匹配一个非换行符的任意字符
* 匹配0个或多个字符
sed -i 's/apple/dog/' a.txt #-i编辑文件
sed '2s/bin/xuegod/' /etc/passwd #只替换第二行的
sed '2,4d' /etc/hosts #删除2-4行
sed '/192.168/d' /etc/hosts #将包括192.168的行删除
echo "hello world" | sed 'i\ xuegod ' #i前插a后插
sed '$a\192.168.1.65 xuegod65.cn' /etc/hosts #最后插
sed '2a\192.168.1.65 xuegod65.cn' /etc/hosts #第二行号后插
sed '2,4a\hello world' word1.txt #第2行到第4行后追加
sed '4c\192.168.1.65 xuegod65.cn' /etc/hosts #修改第四行内容
sed '2,$c\192.168.1.65 xuegod65.cn' /etc/hosts #修改第二行到最后的内容
sed '/a2/c\b3' /etc/hosts #包含a2的行换成b3
sed -n '2p' /etc/hosts #打印第二行
sed -n '/root/w c.txt' /etc/passwd #包含root的行保存到c.txt
sed -i 's/dygasport-app/dygabarrage/g' deploy.sh
#!/bin/bash A=dygaliving-payment-service cp -r dygabarrage $A sed -i 's/dygabarrage/'$A'/g' $A/deploy.sh
cut -f1 -d ":" /etc/passwd
[root@xuegod63 ~]# cut -c1-3 /etc/passwd
[root@xuegod63 ~]# cut -c-2 /etc/passwd
[root@xuegod63 ~]# cut -c5- /etc/passwd
[root@xuegod63 ~]# cat 99.sh
for i in `seq 9`
do
for j in `seq $i`
do
echo -n "$i*$j= `echo $(($i*$j))` "
done
echo " "
done
root@xuegod63 ~]# sh -v p.sh #-x真正执行

浙公网安备 33010602011771号