Bash - Execute Remote Server Commands using JSON file for SSH Creds

#!/bin/bash # {"user":"ec2-user","server":"255.255.255.255","pem":"/Path/To/Key.pem"} file='pem-config.json' : ' Read Value from JSON file by key ' get_json_value() { key=$1 value=$(cat $file | sed 's/{.*\'$key'":"*\([0-9a-zA-Z@.\/\-]*\)"*,*.*}/\1/') echo $value } : ' Construct SSH URL ' function get_ssh_url { user=$(get_json_value 'user') server=$(get_json_value 'server') pem=$(get_json_value 'pem') ssh="ssh -i $pem $user@$server" echo $ssh } ssh=$(get_ssh_url) echo $($ssh sudo service mysqld status)
Loads a JSON config file, pulls the username, key path and server address. Then executes an ssh command.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.