Using Expect to administer computers through SSH, but does not complete all tasks

* Please do not buy or tell me to use SSH keys. If it seems to me that I am doing this, pretend that I'm trying to connect to telnet. :-) *

I use a wait script to run some routine commands on many servers under my control through ssh. The script should run a set of commands (for example, svn update ~ / folderx \ r ") on each of the computers. My current script does everything I want it to do ... sometimes. Sometimes it leaves the ssh connection before it will complete one of the last teams.

Any thoughts on how I can establish a connection until all the commands are complete? The code below successfully logs in, successfully executes the first two commands or so (ap-get update and one of svn updates), and then disconnects.

#!/usr/bin/expect -f

spawn ssh username@ipaddress
set timeout -1
expect "Are you sure you want to continue connecting" {send "yes\r"; exp_continue} "password:" {send "*******\r"; exp_continue
    } "username@machine" {send "sudo apt-get update\r"}
expect "password" {send "*******\r"; exp_continue} "username@machine" {send "sudo svn update ~/folder1\r"}
expect "password" {send "*******\r"; exp_continue} "username@machine" {send "sudo svn update ~/folder2\r"}
expect "password" {send "*******\r"; exp_continue} "username@machine" {send "sudo svn update ~/folder3\r"}
expect "password" {send "*******\r"; exp_continue} "username@machine" {send "sudo reboot\r"}
close
+3
source share
2 answers

, , , , , , , svn, . " " ( "username @machine: ~ $" ). script, "username @", .

p.s. ssh script, , .

+2

Expect - , , . - ssh-, ssh . :

0. Create public/private key pair on local machine.
   (Only needs to be done once on local machine for all remote machines.)
   Go to the ~/.ssh directory on your local machine and do this:
   % ssh-keygen -t rsa
1. Copy the public key to the remote machine:
   % scp ~/.ssh/id_rsa.pub you@foo.com:.
2. Append that key to the authorized_keys file on the remote machine:
   % ssh you@foo.com 'cat id_rsa.pub >> .ssh/authorized_keys; /bin/rm id_rsa.pub'
3. Finally, in case it doesn't work, check permissions, which must be just so: 
   (or maybe it just that they can't be group/world writeable)
   % cd ~; ls -ld . .ssh .ssh/authorized_keys
     drwxr-xr-x  .
     drwxr-xr-x  .ssh
     -rw-r--r--  .ssh/authorized_keys

a script, :

http://jakehofman.com/code/sshkey

:

ssh alice@remote.com ./foo args

sudo . , . Expect .

+3

Source: https://habr.com/ru/post/1754292/


All Articles