Rootop 服务器运维与web架构

2019-04-26
发表者 Venus
package golang.org/x/sys/unix: unrecognized import path “golang.org/x/sys/unix” (https fetch: Get https://golang.org/x/sys/unix?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)已关闭评论

package golang.org/x/sys/unix: unrecognized import path “golang.org/x/sys/unix” (https fetch: Get https://golang.org/x/sys/unix?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)

root@venus:~/桌面/go_project/process# go get -t golang.org/x/sys/unix
package golang.org/x/sys/unix: unrecognized import path "golang.org/x/sys/unix" (https fetch: Get https://golang.org/x/sys/unix?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)

在用go get安装包时出错,这个是因为被墙了(不明白为什么)。

这里通过配置代理后再安装需要的包

# shell中设置http、https代理

root@venus:~/桌面/go_project/process# export http_proxy=http://127.0.0.1:1111
root@venus:~/桌面/go_project/process# export https_proxy=http://127.0.0.1:1111

# 忽略某个地址走代理,多个用逗号隔开,支持域名或ip

root@venus:~/桌面/go_project/process# no_proxy="www.rootop.org"

127.0.0.1:1111 是我本地shadowsocks-Qt5客户端监听的地址。


在shell中通过设置http_proxy和https_proxy两个变量,可以提供给curl、wget之类的命令自动走代理。同样go get时也会自动找这俩变量实现代理。
再次安装解决。

2019-04-25
发表者 Venus
cmd/go: unsupported GOOS/GOARCH pair linux /amd64已关闭评论

cmd/go: unsupported GOOS/GOARCH pair linux /amd64

windows下go编译成linux可执行文件报错:
cmd/go: unsupported GOOS/GOARCH pair linux /amd64

操作步骤如下:
D:\web\go>SET CGO_ENABLED=0
D:\web\go>SET GOOS=linux 
D:\web\go>SET GOARCH=amd64
D:\web\go>go build moniterLogstash.go

最后还是在 https://github.com/golang/go/issues/24501#issuecomment-375682124 找到原因。
是因为在 SET GOOS=linux 这句后面多了个空格(直接复制的命令。。。)
编译器也没有自动去掉多余的空格,不容易发现错误原因。

2019-04-23
发表者 Venus
利用rsync删除大量文件已关闭评论

利用rsync删除大量文件

有个文件夹有大量文件(四百多万个)需要删除,用rm是不行的,会提示 Argument list too long ,就需要一个快速删除方法。
这里采用rsync把一个空文件夹同步到要删除的文件夹实现。

需要清空的目标文件夹名:creditReport

# 创建一个空文件夹

[root@sych userfiles]# mkdir empty

# 写入要保留的文件名,一行一个。建议先备份一下要保留的文件。

[root@sych userfiles]# touch list.txt 

# 执行

[root@sych userfiles]# rsync --delete-before -d --exclude-from=list.txt empty/ creditReport/

# 参数解释

--delete-before # receiver deletes before transfer, not during 传输之前先删除文件,就是利用这个参数实现。
-d              # transfer directories without recursing 不递归传输目录
--exclude-from=FILE # read exclude patterns from FILE 从指定文件中读取要排除的文件,也就是要保留的文件

这四百多万个文件删除大约用时43分钟

2019-04-17
发表者 Venus
while read line语句中ssh远程执行命令,只运行了一次。已关闭评论

while read line语句中ssh远程执行命令,只运行了一次。

while read line语句中ssh远程执行命令,只运行了一次。
目的:批量修改主机信息。

有一个host.txt文件,记录服务器ip,已经配置好秘钥登陆。如:

[root@m ~]# cat host.txt
192.168.10.71
192.168.10.72
192.168.10.73

现在想批量修改主机名,比如idc-001、idc-002 …,后面3位是数字
# 通过 expr 表达式实现shell中进行数学运算
# 通过 printf 格式化字符串,不足3位用0补全

# 先拼接出来主机名,再进一步添加修改功能

[root@m ~]# cat b
#!/bin/bash
str="idc-"
n=1
while read ip
do

 host_num=`printf "%03d" $n`
 hostname=$str$host_num
 echo $ip $hostname
 n=`expr $n + 1`

done < host.txt

# 执行结果没问题

[root@m ~]# sh b
192.168.10.71 idc-001
192.168.10.72 idc-002
192.168.10.73 idc-003

# 下一步就要通过ssh登陆实现修改主机名

[root@m ~]# cat b
#!/bin/bash
str="idc-"
n=1
while read ip
do

 host_num=`printf "%03d" $n`
 hostname=$str$host_num
 echo $ip $hostname
 ssh -o "StrictHostKeyChecking no" root@$ip "echo $hostname > /etc/hostname && hostname $hostname"
 n=`expr $n + 1`

done < host.txt

# 运行

[root@m ~]# sh b
192.168.10.71 idc-001
[root@m ~]# 

就执行了一行,退出了。去看71这台机器是修改成功了,但是72、73仍旧没变化。怀疑问题出在ssh上。

原因:
ssh中有个参数为 -n 通过man看到解释:

 
Redirects stdin from /dev/null (actually, prevents reading from stdin).  This must be used when ssh is run in the background.  A common trick is to
use this to run X11 programs on a remote machine.  For example, ssh -n shadows.cs.hut.fi emacs & will start an emacs on shadows.cs.hut.fi, and the
X11 connection will be automatically forwarded over an encrypted channel.  The ssh program will be put in the background.  (This does not work if
ssh needs to ask for a password or passphrase; see also the -f option.)

即ssh从stdin中读取了while read line的内容,导致while语句没有输入了,就执行完了。
-n的作用就是阻止ssh读取标准输入,为了验证这个结论,修改下host.txt文件,并且把ssh要执行的命令去掉。

[root@m ~]# cat b
#!/bin/bash
str="idc-"
n=1
while read ip
do

 host_num=`printf "%03d" $n`
 hostname=$str$host_num
 echo $ip $hostname
 ssh -o "StrictHostKeyChecking no" root@$ip # 去掉了后面的命令
 n=`expr $n + 1`

done < host.txt
[root@m ~]# cat host.txt 最后两行为两条系统命令
192.168.10.71
uname -r
date

# 执行

[root@m ~]# sh b
192.168.10.71 idc-001
Pseudo-terminal will not be allocated because stdin is not a terminal.
3.10.0-514.el7.x86_64
Wed Apr 17 16:44:58 CST 2019

可以看到内核版本和日期,说明ssh从标准输入读取了内容做为命令执行了,导致while标准输入为空,最后退出。

# 修改后正确姿势,添加-n参数

#!/bin/bash
str="idc-"
n=1
while read ip
do

 host_num=`printf "%03d" $n`
 hostname=$str$host_num
 echo $ip $hostname
 ssh -n -o "StrictHostKeyChecking no" root@$ip "hostname $hostname && echo $hostname > /etc/hostname"
 n=`expr $n + 1`

done < host.txt

# 执行结果

[root@m ~]# sh b
192.168.10.71 idc-001
192.168.10.72 idc-002
192.168.10.73 idc-003

再去看72、73两台机器正确修改了。