一、常用命令
1.
shell 脚本中 : set -x 是开启, set +x是关闭
set -e 表示有报错即退出, set +e 表示关闭这种设置。 set -e 等价于 set -o errexit , set +e 等价于 set +o errexit 。
2. 设置用户免密sudo
(1) visudo
visudo后在编辑内容中加入
chris ALL=(ALL:ALL) ALL
chris为需要免密执行sudo的用户名
(2) 命令追加
或者使用root执行命令
echo “${user} ALL=(ALL) NOPASSWD:ALL” >> /etc/sudoers
二、代码片段
1、遍历数组
for in默认支持两种
hosts=(192.168.14.226 192.168.14.227 192.168.14.228 192.168.14.229)
#hosts="192.168.14.226 192.168.14.227 192.168.14.228 192.168.14.229"
for host in ${hosts[@]}; do
echo $host
done
for(( i=0;i<${#hosts[@]};i++)) do
echo ${hosts[i]};
done;
for i只支持数组
hosts=(192.168.14.226 192.168.14.227 192.168.14.228 192.168.14.229)
for(( i=0;i<${#hosts[@]};i++)) do
echo ${hosts[i]};
done;
2.自动交互
三、linux使用
1. ssh登录提示The authenticity of host ‘192.168.68.xx (192.168.68.xx)’ can’t be established
[root@docker136 ~]# ssh root@192.168.72.137 "echo a"
The authenticity of host '192.168.72.137 (192.168.72.137)' can't be established.
ECDSA key fingerprint is SHA256:dCHiCA3DV94HF9AP9IoA22UFXBAWfvap9JXaQQ3Ccdo.
ECDSA key fingerprint is MD5:0d:45:ba:eb:ec:a3:8c:93:db:ce:3c:82:5e:d0:be:ae.
Are you sure you want to continue connecting (yes/no)?
(1)ssh -o StrictHostKeyChecking=no
执行一下:
ssh -o StrictHostKeyChecking=no 192.168.xx.xx
重新连接,OK
(2)修改文件
The authenticity of host 192.168.0.xxx can't be established.
修改/etc/ssh/ssh_config中的StrictHostKeyChecking的ask 为no就可以
2.服务器间免密登录
ssh-keygen后一直回车生成公钥和私钥
[root@docker136 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:1f0jlyujwSUS0ENpw43VyWlekC38gWdIoUqVVDWcpaM root@docker136
The key's randomart image is:
+---[RSA 2048]----+
| .+.*+B=%o+|
| .O.+.& X.|
| .o+.+ X .|
| ..o o +.|
| So .Eo +.|
| o o o o|
| o o . |
| o o |
| . |
+----[SHA256]-----+
查看ls /root/.ssh
[root@docker136 ~]# ls /root/.ssh
id_rsa id_rsa.pub known_hosts
ssh-copy-id 拷贝公钥到指定主机,接口实现免密执行ssh和scp等命令了
[root@docker136 ~]# ssh-copy-id root@192.168.72.137
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.72.137 (192.168.72.137)' can't be established.
ECDSA key fingerprint is SHA256:dCHiCA3DV94HF9AP9IoA22UFXBAWfvap9JXaQQ3Ccdo.
ECDSA key fingerprint is MD5:0d:45:ba:eb:ec:a3:8c:93:db:ce:3c:82:5e:d0:be:ae.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.72.137's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.72.137'"
and check to make sure that only the key(s) you wanted were added.
总结
# 1.生成公钥和私钥
ssh-keygen
# 2. ssh-copy-id 复制公钥到指定主机,根据提示输入yes(第一次需要)之后输入密码,以后就可以免密ssh scp等操作
ssh-copy-id root@192.168.72.137
设置shell脚本输出字体和颜色
bold=
(
t
p
u
t
b
o
l
d
)
u
n
d
e
r
l
i
n
e
=
(tput bold) underline=
(tputbold)underline=(tput sgr 0 1)
reset=$(tput sgr0)
red=
(
t
p
u
t
s
e
t
a
f
1
)
g
r
e
e
n
=
(tput setaf 1) green=
(tputsetaf1)green=(tput setaf 76)
white=
(
t
p
u
t
s
e
t
a
f
7
)
t
a
n
=
(tput setaf 7) tan=
(tputsetaf7)tan=(tput setaf 202)
blue=$(tput setaf 25)
Headers and Logging
underline() { printf "${underline}${bold}%s${reset}\n" "$@"
}
h1() { printf "\n${underline}${bold}${blue}%s${reset}\n" "$@"
}
h2() { printf "\n${underline}${bold}${white}%s${reset}\n" "$@"
}
debug() { printf "${white}%s${reset}\n" "$@"
}
info() { printf "${white}➜ %s${reset}\n" "$@"
}
success() { printf "${green}✔ %s${reset}\n" "$@"
}
error() { printf "${red}✖ %s${reset}\n" "$@"
}
warn() { printf "${tan}➜ %s${reset}\n" "$@"
}
bold() { printf "${bold}%s${reset}\n" "$@"
}
note() { printf "\n${underline}${bold}${blue}Note:${reset} ${blue}%s${reset}\n" "$@"
}
set -e
set +o noglob