-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathAWSIoTArduinoYunWebsocketCredentialConfig.sh
executable file
·57 lines (50 loc) · 2.05 KB
/
AWSIoTArduinoYunWebsocketCredentialConfig.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/expect
# This script helps to upload the targeted file/directory to remote Arduino Yun Board
# Args check
if {$argc!=5} {
send_user "usage: ./AWSIoTArduinoYunWebsocketCredentialConfig.sh <Board IP> <UserName> <Board Password> <AWS_ACCESS_KEY_ID> <AWS_SECRET_ACCESS_KEY>\n"
exit
}
set timeout 10
set ArduinoYunIPAddress [lindex $argv 0]
set ArduinoYunUserName [lindex $argv 1]
set ArduinoYunPassword [lindex $argv 2]
set AWS_ACCESS_KEY_ID [lindex $argv 3]
set AWS_SECRET_ACCESS_KEY [lindex $argv 4]
set RemoteEndpoint "$ArduinoYunUserName@$ArduinoYunIPAddress"
set profileFullPath "/etc/profile"
send_user "Arduino Yun IP is: $ArduinoYunIPAddress\n"
send_user "Arduino Yun User Name is: $ArduinoYunUserName\n"
send_user "New environment variables will be added to Arduino Yun:\n"
send_user "AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID\n"
send_user "AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY\n"
send_user "$profileFullPath will be modified.\n"
send_user "Please wati until the program exits.\n\n"
# ssh into Arduino Yun and modify $profileFullPath
spawn ssh $RemoteEndpoint
expect {
# In case this is the first time
-re ".*yes/no.*" {
send "yes\r"; exp_continue
-re ".*assword.*" { send "$ArduinoYunPassword\r" }
}
-re ".*assword.*" { send "$ArduinoYunPassword\r" }
}
# Insert new environment variables
expect "*~#" {
# In case we need to update credentials
# Remove the existing AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
send "sed -i '/^export AWS_ACCESS_KEY_ID=/'d /etc/profile\r"
send "sed -i '/^export AWS_SECRET_ACCESS_KEY=/'d /etc/profile\r"
# Append the new credentials to the end of the file
send "echo export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID >> /etc/profile\r"
send "echo export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY >> /etc/profile\r"
# Enable the new profile
send "source /etc/profile\r"
}
expect "*~#" { send "exit\r" }
# Notification to the user to power-cycle the board
send_user "Credentials added to Yun as environment variables. Now please power-cycle the board."
send_user "Exiting..."
# End of the configuration
interact