针对嵌入式系统的telnet登录系统

纯命令写成,可以记录登录时间与用户,二次开发空间大。需busybox与ash的支持。话不多说直接上脚本:

登录脚本(login.sh):

#!/bin/ash

cname=$(cat /etc/telnet.cfg | grep username)
cpwd=$(cat /etc/telnet.cfg | grep passwd)
#/etc/telnet.cfg可修改,与createconfig创建的保持一致即可
echo "---------"  >> /tmp/telnetLOG
echo "有新登录请求!时间:" >> /tmp/telnetLOG
uptime >> /tmp/telnetLOG
read -p "login: " username
echo "用户:"$username >> /tmp/telnetLOG
if [ "$cname" = "username $username" ]
then
  read -p "Password:" passwd
  if [ "$cpwd" = "passwd $passwd" ]
  then
    echo "登录成功!" >> /tmp/telnetLOG
    /bin/ash --login
  else
    echo Error!
    #可自定义显示内容
fi
else
echo Error!
fi

更换密码脚本(changepasswd.sh):

#!/bin/ash
cname=$(cat /etc/telnet.cfg | grep username)
cpwd=$(cat /etc/telnet.cfg | grep passwd)
echo "Changing telnet passwd..."
read -p "Old Passwd: " tpwd
read -p "New Passwd: " npwd
if [ "passwd $tpwd" = "$cpwd" ]
then
rm /etc/telnet.cfg
touch /etc/telnet.cfg
echo "$cname" >/etc/telnet.cfg
echo "passwd $npwd" >>/etc/telnet.cfg
chown root /etc/telnet.cfg
chmod 000 /etc/telnet.cfg
chmod u+r /etc/telnet.cfg
chmod u+w /etc/telnet.cfg
#上方四行如为单用户系统(即只有root可登陆)可以删除
else
echo "Error!"
#可定制显示内容
fi

创建配置文件脚本(createconfig.sh):

#!/bin/ash
echo "Changing telnet config..."
if [ "$(cat /etc/telnet.cfg 2>/dev/null)"x != "x" ]
then
echo "passwd file already exists"
else
read -p "Username: " uname
read -p "Passwd: " upwd
#文件地址可修改,与其他脚本保持一致
rm /etc/telnet.cfg
echo "username $uname" >/etc/telnet.cfg
echo "passwd $upwd" >>/etc/telnet.cfg
chown root /etc/telnet.cfg
chmod 000 /etc/telnet.cfg
chmod u+r /etc/telnet.cfg
chmod u+w /etc/telnet.cfg
#同样如为单用户可删除
fi

查看日志脚本(showlog.sh):

#!/bin/ash
cat /tmp/telnetLOG

清除日志脚本(clearlog.sh):

#!/bin/ash
cpwd=$(cat /etc/telnet.cfg | grep passwd)
read -p "input:" passwd
if [ "passwd $passwd" = "$cpwd" ]
then
  rm /tmp/telnetLOG
  touch /tmp/telnetLOG
else
  echo Error!
fi

一键安装脚本(install.sh):

#!/bin/ash
busybox=$(which busybox)
#下方代码块如果确定有ln和whoami可删除
for i in ln whoami
do
if [ "$(which $i)"x = "x" ]
then
busybox ln -s $busybox /bin/$i
fi
done
ifroot=$(whoami)
if [ "$ifroot" = "root" ]
then
echo "1.复制所有文件到/bin下..."
#cp /bin/login.sh /bin/logib.sh.bak
rm /bin/login.sh
#如/bin下真的有login.sh请删除上方注释
cp *.sh /bin
echo "2.创建必须文件链接(可能出现报错请忽略)"
#如确信下方软件都存在可直接删除
for file in cp rm ln whoami chmod which grep uptime chown touch
do
if [ "$(which $file)"x = "x" ]
then
ln -s $busybox /bin/$file
fi
done
  if [ "$(which telnetd)"x = "x" ]
  then
  echo "你没有telnetd...正从busybox复制(如果支持)"
  ln -s $busybox /bin/telnetd
  else
  echo "找到telnetd..."
  fi
echo "3.给主程序可执行权限"
chmod +x /bin/*.sh
echo "4.配置"
/bin/createconfig.sh
echo "5.启动telnetd"
$(which telnetd) -l /bin/login.sh
echo "如果需要将telnetd加入启动列表请按1,其他键即跳过"
  read -p "choise: " rc
  if [ "$rc" = "1" ]
then
  echo "$(which telnetd) -l /bin/login.sh" > /etc/init.d/telnetd
  ln -s /etc/init.d/telnetd /etc/rc.d/S50telnet
  #请确保系统使用rc.d而不是rc*.d 如是请自行修改.
else
  echo "已跳过……"
fi
echo "安装完成!"
rm /bin/install.sh
else
echo "请使用root用户打开或检查报错信息!"
fi

下载:蓝奏云

posted @ 2022-01-20 14:13  星如雨yu  阅读(501)  评论(0编辑  收藏  举报