Expect Usage Examples

Beispiel #1 Expect Usage Example

This example connects to the remote host via SSH, and prints the remote uptime.

<?php
ini_set
("expect.loguser""Off");

$stream fopen("expect://ssh root@remotehost uptime""r");

$cases = array (
    array (
=> "password:"=> PASSWORD)
);

switch (
expect_expectl ($stream$cases)) {
    case 
PASSWORD:
        
fwrite ($stream"password\n");
        break;
 
    default:
        die (
"Error was occurred while connecting to the remote host!\n");
}

while (
$line fgets($stream)) {
      print 
$line;
}
fclose ($stream);
?>

The following example connects to the remote host, determines whether installed OS is for 32 or 64 bit, then runs update for specific package.

Beispiel #2 Another Expect Usage Example

<?php
ini_set
("expect.timeout", -1);
ini_set("expect.loguser""Off");

$stream expect_popen("ssh root@remotehost");

while (
true) {
    switch (
expect_expectl ($stream, array (
            array (
"password:"PASSWORD), // SSH is asking for password
            
array ("yes/no)?"YESNO), // SSH is asking whether to store the host entry
            
array ("~$ "SHELLEXP_EXACT), // We've got the shell!
    
))) {
        case 
PASSWORD:
            
fwrite ($stream"secret\n");
            break;

        case 
YESNO:
            
fwrite ($stream"yes\n");
            break;

        case 
SHELL:
            
fwrite ($stream"uname -a\n");
            while (
true) {
                    switch (
expect_expectl ($stream, array (
                            array (
"~$ "SHELLEXP_EXACT), // We've got the shell!
                            
array ("^Linux.*$"UNAMEEXP_REGEXP), // uname -a output
                    
), $match)) {
                        case 
UNAME:
                            
$uname .= $match[0];
                            break;

                        case 
SHELL:
                            
// Run update:
                            
if (strstr ($uname"x86_64")) {
                                    
fwrite ($stream"rpm -Uhv http://mirrorsite/somepath/some_64bit.rpm\n");
                            } else {
                                    
fwrite ($stream"rpm -Uhv http://mirrorsite/somepath/some_32bit.rpm\n");
                            }
                            
fwrite ($stream"exit\n");
                            break 
2;

                        case 
EXP_TIMEOUT:
                        case 
EXP_EOF:
                            break 
2;

                        default:
                            die (
"Error has occurred!\n");
                    }
            }
            break 
2;

        case 
EXP_TIMEOUT:
        case 
EXP_EOF:
            break 
2;

        default:
            die (
"Error has occurred!\n");
    }
}

fclose ($stream);
?>

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

PHP cURL-Tutorial: Verwendung von cURL zum Durchführen von HTTP-Anfragen

cURL ist eine leistungsstarke PHP-Erweiterung, die es Ihnen ermöglicht, mit verschiedenen Servern über verschiedene Protokolle wie HTTP, HTTPS, FTP und mehr zu kommunizieren. ...

TheMax

Autor : TheMax
Kategorie: PHP-Tutorials

Midjourney Tutorial - Anleitung für Anfänger

Über Midjourney, dem Tool zur Erstellung digitaler Bilder mithilfe von künstlicher Intelligenz, gibt es ein informatives Video mit dem Titel "Midjourney Tutorial auf Deutsch - Anleitung für Anfänger" ...

Mike94

Autor : Mike94
Kategorie: KI Tutorials

Grundlagen von Views in MySQL

Views in einer MySQL-Datenbank bieten die Möglichkeit, eine virtuelle Tabelle basierend auf dem Ergebnis einer SQL-Abfrage zu erstellen. ...

admin

Autor : admin
Kategorie: mySQL-Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

Warning: Undefined variabl

Your article presents comprehensive and comprehensible information. I gained knowledge from this article that I had previously lacked. While I gen ...

Geschrieben von poppy12 am 30.05.2024 09:58:23
Forum: PHP Developer Forum
PHP 8.3.3 - App kann auf dem PC nicht ausgeführt werden

Many thanks for the valuable information you provide. In the future, I hope you will continue to provide everyone with as many excellent postings ...

Geschrieben von poppy12 am 30.05.2024 09:56:42
Forum: PHP Developer Forum
Schleifenproblem für MYSQL mit gleichen INPUT Feldnamen

This one-of-a-kind game has sparked a lot of attention. In poppy playtime (https://poppy-playtime.io), you must visit the Playtime toy factory. Th ...

Geschrieben von poppy12 am 30.05.2024 09:54:26
Forum: PHP Developer Forum
Seite neu Laden

Thank you very much for the excellent information you provided. In the future, I hope you will continue to provide everyone with as many fantastic ...

Geschrieben von dordle2 am 30.05.2024 09:52:03
Forum: Fragen/Vorschläge zum Forum