Sunday, February 25, 2024

#!/usr/bin/expect to automate Linux commands ?

 #!/usr/bin/expect

spawn some_command   # Replace some_command with the command you want to automate

expect "ExpectedPrompt"   # Replace ExpectedPrompt with the prompt you expect

send "your_input\r"   # Replace your_input with the input you want to provide

interact   # Allows you to interact with the spawned process manually if needed


chmod 777 example.exp 
./example.exp


example.: 

#!/usr/bin/expect
spawn su - goldengate
expect "Password: \r"
send "your_password\r"
expect eof    # wait for spawn processes to finish


\r :- carriage return simulate "Enter" 
\e or \x1B :- for escape key
Ctrl-a : - control plus a 


[root@localhost ~]# cat send.sh
#!/bin/bash
 
 
expect<<!!
spawn vim ./file02.txt
expect "insert mode:\r"
send "i\r"
expect "type some content:\r"
send "I am living in Pune.\r"
expect "save exit\r"
send "\x1B"
send ":wq\r"
expect eof
!!

No comments:

Post a Comment

Enable OpenSSH on Windows 11

Step 1: Install OpenSSH Server You can do this via PowerShell (run as Administrator ): Check if it's already available: Get-WindowsCapab...