Utilisation de deux interactions dans un script anticipé

J’essaie d’écrire un script qui se connecte à une machine Linux via SSH et permet un contrôle interactif des équipements Cisco à partir de là. après avoir fini de contrôler l’équipement, je veux aussi quitter le shell.

J’ai des clés SSH et je n’ai pas besoin de mot de passe pour me connecter. go dans le code ci-dessous est un script Bash qui se connecte à l’équipement cible via SSH / telnet.

Ce que j’ai fait jusqu’à présent est:

 #!/usr/bin/expect set arg1 [lindex $argv 0] spawn ssh -p 24 my_username@my_linux.domain.com expect "#" send "go $arg1 \n" expect "sername:" send "my_username\n" expect "assword:" send "my_password\n" interact expect "root@my_linux:~#" send "logout\n" expect "my_username@my_linux:~ $" send "logout\n" interact 

L’erreur que je reçois lorsque je quitte le shell est la suivante:

 Connection to my_linux.domain.com closed. expect: spawn id exp4 not open while executing "expect "root@my_linux:~#"" (file "./aaa" line 11) 

J’ai résolu le problème:

 #!/usr/bin/expect set timeout -1 set arg1 [lindex $argv 0] spawn ssh -p 24 my_username@my_linux.domain.com expect "#" send "go $arg1 \n" expect "sername:" send "my_username\n" expect "assword:" send "my_password\n" expect "#" interact timeout 5 return send "\n" expect "root@my_linux:~#" send "exit\n exit\n" interact 

Explication: J’ai ajouté quelques lignes:

 # This prevents commands from timing out (default timeout is 10 seconds). set timeout -1 # When I type something, the timeout is ignored, but when I'm not typing, # it waits 5 seconds and then continues. interact timeout 5 return send "\n" expect "root@my_linux:~#" send "exit\n exit\n" 

L’espoir aide n’importe qui