Foil 12 - Running commands on remote systems (2/2)

 1 #******************* Loop for each client to check *******************
 2 while read days client level1 level2 level3 v r m f
 3   do
 4     first=`echo "$days" | awk '{print substr($0,1,1)}'`
 5     if [ -z "$client" -o "$first" = "#" ]
 6       then
 7         continue # Skip the rest of the loop
 8       fi
 9     printf "\n%-10.10s %-14.14s: " "$client" "${level1}-${level2}-${level3}"
10     #******* Check that the client is on the network
11     ping_count=`eval $ping_cmd`
12     if [ "$ping_count" != "1" ]
13       then
14         echo "connection timed out @ `date`"
15         continue # Skip the rest of the loop
16       fi
17 
18     #*************** The actual commands to execute ******************
19     $rsh_cmd $client -n '/usr/sbin/swlist -l file|grep "/usr/lib/dld\.sl"'
20 
21     $rsh_cmd $client 'mv    /usr/dir/dumpauth /usr/dir/dumpauth.old'
22     rcp -p       /distdir/dumpauth $client:/usr/dir/dumpauth
23 
24  done < /tmp/checks/unix.client.hpux                               #12

This is the first of five slides showing the more complex, but very useful of performing operations on remote systems. This can be used for distributing software, checking processes, changing configuration files, rebooting, etc.

The second slide loops through the list of client systems, checks that they are on the network, and executes the commands on each system.

  • Lines 2 and 24 read a configuration file and parse each line into variables with reasonable names.
  • Lines 4-7 check that the line is not a comment and we have a client system's name. Additional checks and filtering can be made as desired.
  • Line 4 uses awk to pick off the first character of the string since the shell doesn't have a substring function.
  • Line 5 checks that this is not a comment and the client name is not null. Without the quotes a null value would result in a syntax error.
  • Line 9 uses the printf command directly in contrast to using awk in a previous example.
  • Line 11 uses the ping command from the previous slide. The name of the client is substituted by the eval command.
  • Lines 19-22 are examples of commands to run on the remote system. Watch out for variables and characters that will be interpreted by the shell on the local or remote system.

    Previous   Next   Index