I have a file with a list of hostnames. I want to read in the file and ssh to each hostname on the list. Do some commands, logout of the server and go onto the next on the list.
I am trying to find a site that is a good tutorial or examples so I can learn how to do more than just the basics with expect.
I'm sure you noticed I still entered a passwd. Ok so you setup your ssh keys between hosts so you don't have to enter a passwd.
So you parse your file to get your list of hostnames.
I have no idea the particulars of you situation -- So I am making stuff up.
Code:
cat file
somethinghere host1 somethingthere
somethinghere host2 somethingthere
somethinghere host3 somethingthere
somethinghere host4 somethingthere
FILE=your_file_here
HOSTS=`awk '{print $2}' $FILE`
for x in $HOSTS;do
ssh $x "date;uptime;df -k /"
done
This will execute date, uptime, and the df on all four hosts...
I use a script I made called forhost to do similar tasks. I work often on hundreds of machines in series, executing one -- or more commands on the host then moving on to the next host.
$1 is the command/commands you want to run and $2 is you list of boxes...
Here is how to use it:
chris@host01>./forhost "uptime;date" tmp.lst
host-ap551 is alive
SYSTEM :: host-ap551 1:20pm up 16 day(s), 5:47, 11 users, load average: 3.09, 3.02, 2.99
Tue Aug 26 13:20:12 CDT 2008
host-db501 is alive
SYSTEM :: host-db501 1:20pm up 16 day(s), 6 hr(s), 40 users, load average: 4.45, 4.35, 4.62
Tue Aug 26 13:20:14 CDT 2008
host-ww501 is alive
SYSTEM :: host-ww501 1:20pm up 9 day(s), 1:55, 3 users, load average: 0.41, 0.38, 0.39
Tue Aug 26 13:20:16 CDT 2008
host-ww502 is alive
SYSTEM :: host-ww502 1:20pm up 9 day(s), 1:54, 24 users, load average: 5.52, 5.41, 4.09
Tue Aug 26 13:20:21 CDT 2008
Or....
Code:
chris@host01>./forhost "uptime;date" " host-ww503 host-ww504"
host-ww503 is alive
SYSTEM :: host-ww503 1:22pm up 16 day(s), 3:36, 9 users, load average: 1.20, 1.37, 1.32
Tue Aug 26 13:22:07 CDT 2008
host-ww504 is alive
SYSTEM :: host-ww504 1:22pm up 12 day(s), 14:17, 20 users, load average: 0.68, 0.89, 1.32
Tue Aug 26 13:22:09 CDT 2008
Or.....
Code:
chris@host01>./forhost "uptime;date" "`grep db tmp.lst`"
host-db501 is alive
SYSTEM :: host-db501 1:24pm up 16 day(s), 6:04, 41 users, load average: 4.34, 4.35, 4.56
Tue Aug 26 13:24:13 CDT 2008
And here is the script.....
Code:
#!/bin/ksh
#### Determine if $1 $2 are present
if [[ -z $1 || -z $2 ]];then
print "Useage: $0 \"command 1;command 2;command 3\" list of servers"
print "command may be one or more commands eperated by ;"
print "any spaces in command sequence require wrapping in \" \""
print " "
print "the list of servers can be a file. If so any line containing # will be omitted"
print "or the list can be a few hosts wrapped in \" \""
else
continue
fi
#### Determine if $2 is a file or not
if [[ -f $2 ]]
then
LIST=`grep -v '#' $2|sort`
else
LIST=$2
fi
for SYSLIST in $LIST
do
## Check and see if $x is alive
#UP=`ping $SYSLIST 2`
if ping $SYSLIST 2
then
#### Execute a command on SYSTEM defined in $LIST
ssh -q $SYSLIST "echo \"SYSTEM :: `echo $SYSLIST` \c\";$1;echo ' '"
else
echo "$SYSLIST is Down? "
fi
done
Hey guys, i am new in Shell programming and i would like to know some good links and websiites which has good tutorials and a wide range of examples on how the commands can be used. those which i have found just provide a brief coverage on the topic and mostly do not give proper examples of how the... (5 Replies)
Hello, I am starting ym first webpage and I am learning how to get images from a remote unix site to my unix account. But I don't know of any good sites to get some. Anyone know of any? (3 Replies)
Hello to all...this is my first post (so please go easy). :)
I feel pretty solid at expect scripting, but I'm running into an issue that I'm not able to wrap my head around. I wrote a script that is a little advanced for logging into a remote Linux machine and changing text in a file using sed.... (2 Replies)
Hi,
I am knew to Shell Scripting.
Can someone please tell me some good books for Shell Scripting which explains in a very simple language and covers all the topics nicely.
Thanks,
Shubh. (3 Replies)
HI All,
Does anyone out there know of any good online tutorials for the basics of routing, gateways, networking with AIX, preferably the more recent versions like 5.2/5.3?
Using commands such as netstat, smit mkroute, etc. to connect to boxes on a network, and load software with networked... (1 Reply)
i need to learn this very quick. i'm not trying delve deep into it. just enough to do basic things. does anyone know of any good sites to visit?? (1 Reply)
Just a quick request guys
As you might have guessed I've just started getting involved in Unix
The guys and the boss in the unix team (not with them yet) have given me some projects to do at my request.
Some of which involve scripting. The work is paying for me to go on a scripting... (2 Replies)
i'm not looking for anything that deals with "if-then" scripts.
i'd like something simple on how to run a series of processes. for example the following:
1. ftp://ftp.netbsd.org/pub/NetBSD/NetBSD-current/tar_files/
2. lcd /
3. get pkgsrc.tar.gz
4. bye
5. cd /usr
6. rm -rf pkgsrc
7. cd... (3 Replies)