Script interactif

De BlaxWiki
Révision datée du 16 octobre 2013 à 09:35 par 217.174.199.129 (discussion)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)
Aller à la navigationAller à la recherche

Lorsque l'on veut automatiser des installations, il arrive que certain programme pose des questions, et qu'il n'est pas possible d'y répondre automatiquement en utilisant des commandes classiques (comme echo -e "YES\n\n y" | script.sh). On peut alors utiliser la commande expect est un outil d’automatisation de tâches Unix, pour des applications interactive comme ssh, telnet, ftp, passwd, fsck ou autre. Il utilise le langage de script Tcl et fonctionne depuis n’importe quel Unix et windows.

Script pour mysql_config_editor[modifier]

Voici un exemple pour la commande mysql_config_editor.

  • Script expect_script_mysql
#!/usr/bin/expect

spawn mysql_config_editor set --login-path=local --host=localhost --user=localuser2 --password
expect "assword"
send "TopSecret\r"
expect "ontinue"
send "Y\r"
expect eof
  • Commande en mode manuel
#  mysql_config_editor set --login-path=local --host=localhost --user=localuser2 --password
Enter password:
WARNING : 'local' path already exists and will be overwritten.
 Continue? (Press y|Y for Yes, any other key for No) : y
  • Commande avec expect
# ./expect_script_mysql
spawn mysql_config_editor set --login-path=local --host=localhost --user=localuser2 --password
Enter password:
WARNING : 'local' path already exists and will be overwritten.
 Continue? (Press y|Y for Yes, any other key for No) : Y

Script pour telnet[modifier]

  # Assume $remote_server, $my_user_id, $my_password, and $my_command were read in earlier 
  # in the script.
  # Open a telnet session to a remote server, and wait for a username prompt.
  spawn telnet $remote_server
  expect "username:"
  # Send the username, and then wait for a password prompt.
  send "$my_user_id\r"
  expect "password:"
  # Send the password, and then wait for a shell prompt.
  send "$my_password\r"
  expect "%"
  # Send the prebuilt command, and then wait for another shell prompt.
  send "$my_command\r"
  expect "%"
  # Capture the results of the command into a variable. This can be displayed, or written to disk.
  set results $expect_out(buffer)
  # Exit the telnet session, and wait for a special end-of-file character.
  send "exit\r"
  expect eof

Script pour ssh[modifier]

#!/usr/bin/expect -f
set password [lindex $argv 2]
set usertomodify billou
set NewPassword [lindex $argv 3]
set timeout -1
spawn ssh -l [lindex $argv 0] [lindex $argv 1]
expect "Password:"
send -- "$password\r"
# cette commande expect est nécessaire, c'est pour attendre le prompt du shell, sinon ca envoie la commande "passwd" juste après avoir envoyé le mot de pass de la connexion ssh, du
#coup la commande est envoyé avant d'être réellement loggué sur ta machine distante.ompt du shell 
expect "#"
send -- "passwd $usertomodify\r"
expect "New Password:"
send -- "$NewPassword\r"
expect "Re-enter new Password:"
send "$NewPassword\r"
expect "successfully"
send "exit\r"
expect eof