#!/bin/bash
H=" 192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 "
for i in $H ; do expect << -EOF- set timeout 2 spawn scp ${rootidfile} root@${host}:/tmp/id_file expect "d:" send "${password}\n" expect eof spawn ssh ${host} expect "d:" send "${password}\n" expect "#" send "mkdir -p /root/.ssh; chmod 700 /root/.ssh\n" expect "#" send "cat /tmp/id_file >> /root/.ssh/authorized_keys\n" expect "#" send "rm -f /tmp/id_file; exit\n" expect eof -EOF-
done
http://www.bsdmap.com/2010/11/06/ssh-shell-script/
------------------ Original ------------------ From: "Kill Script"killscript@gmail.com; Date: Tue, Nov 16, 2010 02:47 AM To: "centos"centos@centos.org;
Subject: [CentOS] good shell script examples?
I am looking for a beginner guide to shell scripting simple tasks on CentOS (e.g. ssh'ing into a server / router / switch, checking for certain things, then exiting and going to the next IP).
Does anyone have any suggestions on where to look? ?(I'm relatively new to bash)
On Wednesday 17 Nov 2010 07:47:17 洪 川 wrote:
#!/bin/bash
H=" 192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 "
for i in $H ; do expect << -EOF- set timeout 2 spawn scp ${rootidfile} root@${host}:/tmp/id_file expect "d:" send "${password}\n" expect eof spawn ssh ${host} expect "d:" send "${password}\n" expect "#" send "mkdir -p /root/.ssh; chmod 700 /root/.ssh\n" expect "#" send "cat /tmp/id_file >> /root/.ssh/authorized_keys\n" expect "#" send "rm -f /tmp/id_file; exit\n" expect eof -EOF-
done
Or have a look at Fabric [1] which deals with SSH, sudo, passwords, etc. and multiple hosts so it would be a matter of:
@hosts('192.168.1.1', '192.168.1.2', '192.168.1.3', '192.168.1.4') def some_task(): put('rootidfile', '~/.ssh/authorized_keys')
and run it as:
$ fab -U root some_task