Merge branch 'main' of feistymeow.org:feisty_meow
[feisty_meow.git] / scripts / security / tell_zooty_our_ip.sh
1 #!/bin/bash
2 # this script connects to a remote machine and records the IP address of the
3 # local machine there.  this enables the machine's owner to connect back to
4 # the system even if the IP address floats around (changes).
5
6 server="$1"; shift
7 remote_user="$1"; shift
8 local_user="$1"; shift
9
10 if [ -z "$server" -o -z "$remote_user" -o -z "$local_user" ]; then
11   echo "This script will record the IP address for 'this' host into a file on a"
12   echo "remote computer (that is running ssh server).  To perform this feat, the"
13   echo "following parameters are required:"
14   echo "  $(basename $0) {server} {remote-user} {local-user}"
15   echo "Note that this script must be run as root, but it uses the local user's"
16   echo "capability to connect to the remote system without a password (given the"
17   echo "user's possession of an ssh certificate on the remote host).  The remote"
18   echo "user, in other words, must have an entry in the ssh authorized_keys that"
19   echo "allows the local user to connect."
20   exit 1
21 fi
22
23 ip_file="$(hostname | sed -e "s/\..*$//")_ip_address.txt"
24
25 # go over to the server and write a file recording our IP address.
26 # note that the local_user here is expected to have a certificate that
27 # gets us access to the server.  this needs to be on the local machine
28 # to enable ssh to run without a login prompt.
29 sudo -u $local_user ssh $remote_user@$server <<eof
30   if [ ! -d gen ]; then mkdir gen; fi
31   cd gen
32   echo "\$SSH_CLIENT" | awk '{ print \$1; }' >$ip_file
33 eof
34