I am trying to accomplish a simple job via expect. I want to create ssh keys using the \"ssh-keygen\" command on Linux VMs. My below expect code looks to be straight forward
The spawned program is likely sending more output than exactly what you're trying to match. That's why regular expression matching is so helpful.
Try this:
spawn ssh-keygen -t rsa
expect -re {Enter file in which to save the key (/root/.ssh/id_rsa): $}
send -- "\r"
expect -re {Enter passphrase (empty for no passphrase): $}
send -- "\r"
expect -re {Enter same passphrase again: $}
send -- "\r"
I suppose you're exiting to quickly. This one works for me:
#!/usr/bin/expect
spawn ssh-keygen -t rsa
expect "Enter file in which to save the key (/root/.ssh/id_rsa): "
send "\r"
expect "Enter passphrase (empty for no passphrase): "
send "\r"
expect "Enter same passphrase again: "
send "\r"
expect