背景:我必须将一个文件从一台服务器复制到testing环境中的100多台服务器。 一旦文件被复制,它需要对文件的权限进行更改/validation。 这些都是Linux服务器。 他们中的大多数具有相同的login密码,但有些可能不会。
我需要在我的bash脚本中做一个循环的帮助。 这实际上是在期待。 我想做的更好或修复的地方是一些事情
我试图写一个循环的密码片,但我不确定这种思维方式是否成立。 我试着添加另一个“期望密码”,以为如果第二次得到提示,就退出,但是我很难让这个工作成功。 谢谢!
#!/bin/bash while read ip; do sleep 2 expect <<- DONE set timeout 1 spawn scp yoman.txt root@$ip:/felixtemp if above command fails, dump the IP to fail.txt, otherwise continue expect yes/no { send yes\r } expect Password: { send aaaaaa\r } #if this is good, continue the script from ***** else #exit the script expect Password: { send 033\r } expect # { send "echo 'password failed'\r" } && dump to a text file called fail.txt ***** expect # { send "exit\r\r" } sleep 1 set timeout 1 spawn ssh root@$ip sleep 2 expect yes/no { send yes\r } sleep 2 expect Password: { send aaaaaa\r } sleep 5 expect # { send "cd /felixtemp\r" } expect # { send "chown informix:informix yoman.txt\r" } expect # { send "chmod 775 yoman.txt\r" } expect # { send "sum yoman.txt | grep 10350 && echo 'transfer good' || echo 'transfer bad'\r" } expect # { send exit\r } sleep 1 DONE done < ip.txt
如果您必须pipe理数百台Linux服务器,则应使用configurationpipe理工具来执行这些任务。 一个非常简单的configurationpipe理工具是可行的,托pipe系统的唯一要求是Python 2.4或更高版本( http://docs.ansible.com/intro_installation.html#managed-node-requirements )。
你的问题解决了可靠的:
1)定义一个主机列表,您可以为所有主机定义一个默认密码,并为某些主机定义其他密码
[hosts_list] 172.17.0.101 ansible_ssh_user=root ansible_ssh_pass=password 172.17.0.102 ansible_ssh_user=root ansible_ssh_pass=oldpassword 172.17.0.103 172.17.0.104 [hosts_list:vars] ansible_ssh_user=root ansible_ssh_pass=default_password
2)定义一个简单的手册,其中包含您要在受pipe节点上执行的任务
root@node1:~# cat play.yoman --- - hosts: hosts_list tasks: - name: "Build hosts file" copy: src=/root/yoman.txt dest=/tmp/felixtemp owner=user group=adm mode=0755
3)执行剧本并检查结果
root@node1:~# ansible-playbook -i hosts_list play.yoman PLAY [hosts_list] ************************************************************* GATHERING FACTS *************************************************************** fatal: [172.17.0.104] => SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue fatal: [172.17.0.103] => SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue ok: [172.17.0.101] fatal: [172.17.0.102] => Authentication failure. TASK: [Build hosts file] ****************************************************** ok: [172.17.0.101] PLAY RECAP ******************************************************************** to retry, use: --limit @/root/play.yoman.retry 172.17.0.101 : ok=2 changed=0 unreachable=0 failed=0 172.17.0.102 : ok=0 changed=0 unreachable=1 failed=0 172.17.0.103 : ok=0 changed=0 unreachable=1 failed=0 172.17.0.104 : ok=0 changed=0 unreachable=1 failed=0
您将获得执行任务的服务器列表以及任务失败的服务器列表(服务器无法访问或密码错误)。 您还获得了文件,其权限和内容未被更改的服务器子集,因为它们已经被更新
你为什么不使用sshpass ?
循环与表:
tab=( 1.2.3.4 4.3.2.1 ... ); for (( i = 1; i < ${#tab[*]}; i++ )) { echo ${tab[i]}; ... }
您需要一些特定IP地址的条件才能设置正确的密码。
SCP
sshpass -p $PASSWORD scp -o StrictHostKeyChecking=no $FILE $USER@$HOST:$PATH
根据手册页,返回值是:
0成功
1无效的命令行参数
2给出了相互矛盾的论据
3一般运行时错误
4来自ssh的无法识别的响应(parsing错误)
5密码无效/不正确
6主机公钥是未知的。 sshpass没有确认新密钥就退出。
如果您想在具有返回值的文件中打印$ HOST,这很有用:
sshpass -p $PASSWORD scp -o StrictHostKeyChecking=no $FILE $USER@$HOST:$PATH if [ $? -ne 0 ] then echo $HOST:$? >> file.txt; fi
SSH
发送命令:
sshpass -p $PASSWORD ssh -o StrictHostKeyChecking=no $USER@$HOST $CMD