Skip to content

Instantly share code, notes, and snippets.

@lzwjava lzwjava/customsshd forked from xfreebird/customsshd
Created Nov 17, 2015

Embed
What would you like to do?
A script which can be used to start a second SSHD daemon on OS X. This is an workaround for running tests using xcodebuild through ssh with Jenkins .
#!/bin/bash
INSTALL_PATH="$HOME/scripts"
SCRIPT_PATH="$INSTALL_PATH/customsshd"
LAUNCHCTL_PATH="$HOME/Library/LaunchAgents/com.my.customsshd.plist"
SSH_KEYS_INSTALL_PATH=$HOME/customkeys
SSH_HOST_KEY=$SSH_KEYS_INSTALL_PATH/ssh_host_key
SSH_HOST_RSA_KEY=$SSH_KEYS_INSTALL_PATH/ssh_host_rsa_key
SSH_HOST_DSA_KEY=$SSH_KEYS_INSTALL_PATH/ssh_host_dsa_key
SSHD_PORT=50111
SSH_AUTHORIZED_KEYS_PATH="$HOME/.ssh/authorized_keys"
[ ! -f $SSH_HOST_KEY ] && ssh-keygen -q -t rsa1 -f $SSH_HOST_KEY -N "" -C "" < /dev/null > /dev/null 2> /dev/null
[ ! -f $SSH_HOST_RSA_KEY ] && ssh-keygen -q -t rsa -f $SSH_HOST_RSA_KEY -N "" -C "" < /dev/null > /dev/null 2> /dev/null
[ ! -f $SSH_HOST_DSA_KEY ] && ssh-keygen -q -t dsa -f $SSH_HOST_DSA_KEY -N "" -C "" < /dev/null > /dev/null 2> /dev/null
#add you public rsa key here, the authentication is ssh key based
CUSTOM_ID_RSA_PUBKEY="ssh-rsa AAAAB3NzaC1yc2dfgdfgDAQABAAABAQDRGnX9NX4K/D3Ex5NF514AyUxQCu/+nJnjyZudY5+dsfsdfewrwedgdfg/+MCTCQ6pO0RQ42dH5P41bBD5nju9yDyfK6pfUz89vwqwC5HtAOC27VWU/dfgdfg/3B1jlR5i7zzUUmMojSNZTRIFy/dffgdg/ICLObc6kwF4hSdGCpdbzDpLyCXSDQDjAJbBb//cgB4gqBcv3Nc7sh3woT7J9JH6aHFAgmn5R5dwL3P user@host.com"
#insert the key if it is not in authorized_keys
function verifyPubKey()
{
if [ -f "$SSH_AUTHORIZED_KEYS_PATH" ];
then
PUBKEYEXISTS=`grep -q "$CUSTOM_ID_RSA_PUBKEY" "$SSH_AUTHORIZED_KEYS_PATH"`
if [[ $? -eq 1 ]]
then
injectPubKey
fi
else
injectPubKey
fi
}
function injectPubKey()
{
echo "$CUSTOM_ID_RSA_PUBKEY" >> "$SSH_AUTHORIZED_KEYS_PATH"
chmod 600 "$SSH_AUTHORIZED_KEYS_PATH"
}
function runSSHD() {
/usr/sbin/sshd -D -p $SSHD_PORT -h $SSH_HOST_KEY -h $SSH_HOST_RSA_KEY -h $SSH_HOST_DSA_KEY -o UsePam=yes -o Protocol=1,2 -o PubkeyAuthentication=yes -o RSAAuthentication=yes
}
function installLaunchAgent()
{
cat > "$LAUNCHCTL_PATH" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.my.customsshd</string>
<key>Program</key>
<string>$SCRIPT_PATH</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>/tmp/customsshd.log</string>
<key>StandardErrorPath</key>
<string>/tmp/customsshd_err.log</string>
</dict>
</plist>
EOF
launchctl load -w "$LAUNCHCTL_PATH"
echo "customsshd has been installed"
}
#if anything passed as argument, just install the script
#example:
#./customsshd install
if [ $# -eq 1 ]
then
installLaunchAgent
exit 0
fi
verifyPubKey
while :
do
runSSHD
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.