Skip to content

Instantly share code, notes, and snippets.

@ruehowl
Last active December 27, 2018 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruehowl/7bc28e15ff60d40d17af3761ebad7802 to your computer and use it in GitHub Desktop.
Save ruehowl/7bc28e15ff60d40d17af3761ebad7802 to your computer and use it in GitHub Desktop.
Cache server info each time you ssh.
#!/bin/bash
#provide an option.
# -a add a server <server name> <user> <ip> <port> <'user pass'> <'root pass'>
# -l list cached servers.
# -r remove an entry.
# -s ssh to a server <server name or server ip>
option=$1
function new_server {
server_name=$1
user=$2
ip=$3
port=$4
user_pass=$5
root_pass=$6
user_pass=${user_pass//\\/\\\\}
root_pass=${root_pass//\\/\\\\}
if [ ! -e metssh.txt ] ;
then
cat > cachessh.txt << EOF
{
"localhost" : {
"server name": "localhost",
"server ip": "127.0.0.1",
"ssh port": "22",
"ssh user": "user",
"ssh user password": "user123",
"root password": "user123"
}
}
EOF
fi
sed -i '$ d' metssh.txt
sed -i '$ d' metssh.txt
cat >> metssh.txt << EOF
},
"$server_name": {
"server name": "$server_name",
"server ip": "$ip",
"ssh port": "$port",
"ssh user": "$user",
"ssh user password": "$user_pass",
"root password": "$root_pass"
}
}
EOF
}
function search {
searchfor=$1
server=`python <<END
import sys, json;
data = open("metssh.txt");
obj = json.load(data, strict=False);
def search(values, searchFor ):
for k in values:
for v in values[k]:
if searchFor in values[k][v]:
return k
return None
k = search(obj, "$searchfor")
print k
END`
user=`cat metssh.txt | python -c "import sys, json; obj=json.load(sys.stdin, strict=False);print obj['$server']['ssh user'];"`
server_ip=`cat metssh.txt | python -c "import sys, json; obj=json.load(sys.stdin, strict=False);print obj['$server']['server ip'];"`
ssh_port=`cat metssh.txt | python -c "import sys, json; obj=json.load(sys.stdin, strict=False);print obj['$server']['ssh port'];"`
user_pass=`cat metssh.txt | python -c "import sys, json; obj=json.load(sys.stdin, strict=False);print obj['$server']['ssh user password'];"`
root_pass=`cat metssh.txt | python -c "import sys, json; obj=json.load(sys.stdin, strict=False);print obj['$server']['root password'];"`
echo "user pass : $user_pass"
echo "root pass : $root_pass"
ssh -t -t $user@$server_ip -p $ssh_port "$2"
}
if [ "$option" == '-a' ] || [ "$option" == "add" ] ;
then
new_server $2 $3 $4 $5 $6 $7
search $2
elif [ "$option" == '-l' ] || [ "$option" == "list" ] ;
then
python <<END
import sys, json;
data = open("metssh.txt");
obj = json.load(data, strict=False);
for k in obj:
print k, obj[k]['server ip']
END
elif [ "$option" == "-r" ] || [ "$option" == "remove" ] ;
then
python <<END
import sys, json;
data = open("metssh.txt");
obj = json.load(data, strict=False);
for i in obj:
if i == "$2":
obj.pop(i)
break
open("metssh.txt", "w").write(
json.dumps(obj, sort_keys=True, indent=4, separators=(',', ': '))
)
END
elif [ "$option" == "-s" ] || [ "$option" == "ssh" ] ;
then
search $2 $3
else
echo -e "provide an option.\n -a add a server " \<user\> \<ip\> \<port\> \<\'user pass\'\> \<\'root pass\'\>" \n -l list chached servers. \n -r remove an entry. \n -s ssh to a server <server name or server ip>"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment