繼上次用schell script做出 XRandR旋轉螢幕的OPENBOX 選單後,這次想來個右鍵直接ssh個xterm直通認識的伺服器,首先檢查技術面,指令上xterm -e後面不可以用"" ''等字元把命令包起來,這是UNIX POWER TOOLs一書所提醒的"特殊注意事項"。
我想把程式拆成兩部份,一部分從/etc/hosts擷取出已經知道的hostname並且生成設定檔。第二部分就是址負責生出openbox pipe menu的script
第一部份任務很簡單
#!/bin/sh
debug=false
#debug=ture# check the ssh_key2
if [ -z $HOME/.ssh/id_rsa ] ; then
echo 'You should take RSA key for quicker login'
echo 'Now I lead you to make one.'
(/usr/bin/ssh-keygen -t rsa)
fi# check ssh_agent
if [ `ps ax | grep ssh-agent | wc -l` -gt 0 ] ; then
echo 'Found SSH-Agent is running.'
else
echo 'Please man ssh-agent for more advantage.'
fi# list the knownhost
##因為我只想通ipv4(去他的ipv6),所以我先將所有數字抽掉,那麼所有的ipv4位址就會變成...,然後把這些行最後一欄抽出來供作ssh的target(如果沒有短名,就會抽整條Domain name)。
##但是這樣會有個問題,這樣產出的送進pipe後傳入knownhosts矩陣裡,矩陣裡只會有一個元素。解決辦法就是多個xargs把參數"一次一個"傳進矩陣裡,就解決了。為了個xargs可是我想了好幾天才想出來的... = =b
knownhosts=( `sed 's/[:0-9:]//g' /etc/hosts | grep "\.\.\." | awk '{ print $NF}' | xargs` )
if [ ! $debug ]; then
echo ${knownhosts[@]}
fi# creat ~/.ssh/cast
touch /tmp/cast$$
for host in ${knownhosts[*]}
do
echo "# ssh to $host" >> /tmp/cast$$
echo " -l $LOGNAME -p 22 -X $host " >> /tmp/cast$$
done
cat << TOPICCAST > $HOME/.ssh/cast
## please keep this file format like this every 2 line ##
## ##
## "# ssh to" HOSTNAME ##
## -l USERNAME -p PORT HOSTNAME ##
## ##
## or any options comes after ssh command in 2nd line ##
TOPICCAST
grep -v localhost /tmp/cast$$ >> $HOME/.ssh/cast
沒有留言:
張貼留言