Matt Feenstra is an Architect and Developer, living and working in Palm Desert, CA
Sunday, March 10, 2013
Automating SSH public key distribution using Expect
This script will help automate distribution of a SSH generated key (pub.key) using Expect
Script command line argument = hostname / IP address
#!/bin/bash
PROMP=":~ "
PASS="rootspassword"
USER="root"
PUBKEY=`cat ./pub.key`
/usr/bin/expect -c "
set timeout 180
spawn ssh -q -p 22 $USER@$1
expect {
yes/no {
sleep 1 ;
send "yes"\r;
exp_continue
}
assword: {
sleep 1 ;
send "$PASS"\r;
}
}
expect {
$PROMP {
send \"mkdir ~/.ssh\r\";
sleep 1;
send \"echo '$PUBKEY' >>~/.ssh/authorized_keys\r\";
send \"echo\r\";
}
}
expect {
$PROMP {
sleep 1;
send \"history -c\r\";
}
}
expect {
$PROMP {
sleep 1;
send \"exit\r\";
}
}
"
exit
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment