FreeBSD 下請安裝 /usr/ports/sysutils/fusefs-sshfs
Ubuntu/Debian 下請 apt-get install sshfs
Mount :
sshfs RemoteServerAccountName@SSHServerName:/Remote/path /Mount/path -o follow_symlinks
Umount :
FreeBSD : umount /Mount/path
Ubuntu/Debian : fusermount -u /Mount/path
2. 使用掛載 shell script
目前暫時不做作業系統版本判斷. 所以有以下的兩組 shell script:
FreeBSD
mountSSHFS.sh
#!/usr/local/bin/bash
PATH=$PATH
SSHFSCMD="sshfs"
SSHFSOPTION="-o follow_symlinks"
BASEPATHKEYWORD="SSHFS"
LOCALBASEPATH="${HOME}/${BASEPATHKEYWORD}"
LOCALHOST=`hostname -s`
CHKPARA=0
if [ -n "$1" ]; then
SiteCode="$1"
shift
else
CHKPARA=1
fi
if [ -n "$1" ]; then
ACCOUNT="$1"
shift
else
CHKPARA=1
fi
if [ -n "$1" ]; then
REMOTEPATH="$1"
shift
fi
if [[ ${LOCALHOST} = ${SiteCode} ]]; then
echo "Error : Do not need to mount directory in local machine(${SiteCode}) via sshfs."
echo ""
CHKPARA=1
fi
if [ $CHKPARA -eq 1 ]; then
echo "Usage : $0 SiteCode Account [RemotePath]"
echo "Example : $0 SSHServerName RemoteServerAccountName"
echo "Example : $0 SSHServerName RemoteServerAccountName /home/RemoteServerAccountName"
echo "SiteCode : SSHServerName SSHServerName1 SSHServerName2"
echo "Account : Your account name in remote host"
echo ""
echo "SSHFS Mount Status"
df -h | grep ${BASEPATHKEYWORD}
exit 1
else
### Check SSH server
SITE=${SiteCode}
NFSSTATUS=`echo -en "o ${SITE} 22\nsend escape\nquit\nclose\n" | telnet 2>/dev/null | grep "]" | wc -l`
if [ $NFSSTATUS -ne 1 ]; then
echo "Could not connect to SSH server ${SITE}."
exit 1
else
echo "SSH server ${SITE} is alive."
fi
### Prepare for mount sshfs
REMOTEHOST=${SiteCode}
REMOTEACCOUNT=${ACCOUNT}
LOCALHOSTPATH=${SiteCode}
if [ -z ${REMOTEPATH} ]; then
REMOTEPATH="/home/${REMOTEACCOUNT}"
fi
echo "Remote Host : ${REMOTEHOST}"
echo "Remote Account : ${REMOTEACCOUNT}"
echo "Remote Path : ${REMOTEPATH}"
echo "Local Host : ${LOCALHOST}"
echo "Mount Path : ${LOCALBASEPATH}/${LOCALHOSTPATH}"
echo ""
if [ ! -d ${LOCALBASEPATH}/${LOCALHOSTPATH} ]; then
echo "Prepare the mount path : ${LOCALBASEPATH}/${LOCALHOSTPATH}"
mkdir -p ${LOCALBASEPATH}/${LOCALHOSTPATH}
echo "Mount Command : ${SSHFSCMD} ${REMOTEACCOUNT}@${REMOTEHOST}:${REMOTEPATH} ${LOCALBASEPATH}/${LOCALHOSTPATH} ${SSHFSOPTION}"
${SSHFSCMD} ${REMOTEACCOUNT}@${REMOTEHOST}:${REMOTEPATH} ${LOCALBASEPATH}/${LOCALHOSTPATH} ${SSHFSOPTION}
else
echo "Error : The mount path ${LOCALBASEPATH}/${LOCALHOSTPATH} had been existed."
fi
echo ""
echo "SSHFS Mount Status"
df -h | grep ${BASEPATHKEYWORD} | grep ${REMOTEHOST}
fi
umountSSHFS.sh
#!/usr/local/bin/bash
PATH=$PATH
SSHFSCMD="umount"
LOCALBASEPATH="${HOME}/SSHFS"
LOCALHOST=`hostname -s`
CHKPARA=0
if [ -n "$1" ]; then
SiteCode="$1"
shift
else
CHKPARA=1
fi
if [[ ${LOCALHOST} = ${SiteCode} ]]; then
echo "Error : Do not need to umount your home directory."
echo ""
CHKPARA=1
fi
if [ $CHKPARA -eq 1 ]; then
echo "Usage: $0 SiteCode"
echo "Example : $0 SSHServerName"
echo "SiteCode : SSHServerName1 SSHServerName12 labdata SSHServerName"
echo ""
echo "SSHFS Mount Status"
df -h | grep SSHFS
exit 1
else
LOCALHOSTPATH=${SiteCode}
echo "Remote Host : ${SiteCode}"
echo "Local Host : ${LOCALHOST}"
echo "Mount Path : ${LOCALBASEPATH}/${LOCALHOSTPATH}"
echo ""
if [ -d ${LOCALBASEPATH}/${LOCALHOSTPATH} ]; then
echo "Check Path : Mount path ${LOCALBASEPATH}/${LOCALHOSTPATH} was existed."
echo "Umount Command : ${SSHFSCMD} ${LOCALBASEPATH}/${LOCALHOSTPATH}"
${SSHFSCMD} ${LOCALBASEPATH}/${LOCALHOSTPATH}
rmdir ${LOCALBASEPATH}/${LOCALHOSTPATH}
else
echo "Error : Mount path ${LOCALBASEPATH}/${LOCALHOSTPATH} does not existed."
fi
echo ""
echo "SSHFS Mount Status"
df -h | grep SSHFS | grep ${SiteCode}
fi
Ubuntu/Debian
mountSSHFS.sh
#!/bin/bash
PATH=$PATH
SSHFSCMD="sshfs"
SSHFSOPTION="-o follow_symlinks"
BASEPATHKEYWORD="SSHFS"
LOCALBASEPATH="${HOME}/${BASEPATHKEYWORD}"
LOCALHOST=`hostname -s`
CHKPARA=0
if [ -n "$1" ]; then
SiteCode="$1"
shift
else
CHKPARA=1
fi
if [ -n "$1" ]; then
ACCOUNT="$1"
shift
else
CHKPARA=1
fi
if [ -n "$1" ]; then
REMOTEPATH="$1"
shift
fi
if [[ ${LOCALHOST} = ${SiteCode} ]]; then
echo "Error : Do not need to mount directory in local machine(${SiteCode}) via sshfs."
echo ""
CHKPARA=1
fi
if [ $CHKPARA -eq 1 ]; then
echo "Usage : $0 SiteCode Account [RemotePath]"
echo "Example : $0 SSHServerName RemoteServerAccountName"
echo "Example : $0 SSHServerName RemoteServerAccountName /home/RemoteServerAccountName"
echo "SiteCode : SSHServerName SSHServerName1 SSHServerName2"
echo "Account : Your account name in remote host"
echo ""
echo "SSHFS Mount Status"
df -h | grep ${BASEPATHKEYWORD}
exit 1
else
### Check SSH server
SITE=${SiteCode}
NFSSTATUS=`echo -en "o ${SITE} 22\nsend escape\nquit\nclose\n" | telnet 2>/dev/null | grep "]" | wc -l`
if [ $NFSSTATUS -ne 1 ]; then
echo "Could not connect to SSH server ${SITE}."
exit 1
else
echo "SSH server ${SITE} is alive."
fi
### Prepare for mount sshfs
REMOTEHOST=${SiteCode}
REMOTEACCOUNT=${ACCOUNT}
LOCALHOSTPATH=${SiteCode}
if [ -z ${REMOTEPATH} ]; then
REMOTEPATH="/home/${REMOTEACCOUNT}"
fi
echo "Remote Host : ${REMOTEHOST}"
echo "Remote Account : ${REMOTEACCOUNT}"
echo "Remote Path : ${REMOTEPATH}"
echo "Local Host : ${LOCALHOST}"
echo "Mount Path : ${LOCALBASEPATH}/${LOCALHOSTPATH}"
echo ""
if [ ! -d ${LOCALBASEPATH}/${LOCALHOSTPATH} ]; then
echo "Prepare the mount path : ${LOCALBASEPATH}/${LOCALHOSTPATH}"
mkdir -p ${LOCALBASEPATH}/${LOCALHOSTPATH}
echo "Mount Command : ${SSHFSCMD} ${REMOTEACCOUNT}@${REMOTEHOST}:${REMOTEPATH} ${LOCALBASEPATH}/${LOCALHOSTPATH} ${SSHFSOPTION}"
${SSHFSCMD} ${REMOTEACCOUNT}@${REMOTEHOST}:${REMOTEPATH} ${LOCALBASEPATH}/${LOCALHOSTPATH} ${SSHFSOPTION}
else
echo "Error : The mount path ${LOCALBASEPATH}/${LOCALHOSTPATH} had been existed."
fi
echo ""
echo "SSHFS Mount Status"
df -h | grep ${BASEPATHKEYWORD} | grep ${REMOTEHOST}
fi
umountSSHFS.sh
#!/bin/bash
PATH=$PATH
SSHFSCMD="fusermount -u"
LOCALBASEPATH="${HOME}/SSHFS"
LOCALHOST=`hostname -s`
CHKPARA=0
if [ -n "$1" ]; then
SiteCode="$1"
shift
else
CHKPARA=1
fi
if [[ ${LOCALHOST} = ${SiteCode} ]]; then
echo "Error : Do not need to umount your home directory."
echo ""
CHKPARA=1
fi
if [ $CHKPARA -eq 1 ]; then
echo "Usage: $0 SiteCode"
echo "Example : $0 SSHServerName"
echo "SiteCode : SSHServerName1 SSHServerName12 labdata SSHServerName"
echo ""
echo "SSHFS Mount Status"
df -h | grep SSHFS
exit 1
else
LOCALHOSTPATH=${SiteCode}
echo "Remote Host : ${SiteCode}"
echo "Local Host : ${LOCALHOST}"
echo "Mount Path : ${LOCALBASEPATH}/${LOCALHOSTPATH}"
echo ""
if [ -d ${LOCALBASEPATH}/${LOCALHOSTPATH} ]; then
echo "Check Path : Mount path ${LOCALBASEPATH}/${LOCALHOSTPATH} was existed."
echo "Mount Command : ${SSHFSCMD} ${LOCALBASEPATH}/${LOCALHOSTPATH}"
${SSHFSCMD} ${LOCALBASEPATH}/${LOCALHOSTPATH}
rmdir ${LOCALBASEPATH}/${LOCALHOSTPATH}
else
echo "Error : Mount path ${LOCALBASEPATH}/${LOCALHOSTPATH} does not existed."
fi
echo ""
echo "SSHFS Mount Status"
df -h | grep SSHFS | grep ${SiteCode}
fi
No comments:
Post a Comment