Shell - 使用 nc -c 调用函数
Shell - calling function with nc -c
这是我现在的服务器代码。
我只想从客户那里获取数据,对其进行分析并发送我的答案。
有没有办法在 nc 中调用我的函数 managedata()?我的代码,显然不起作用。
server.sh:
managedata()
{
echo
#do something with data
return
}
listen()
{
echo "Server listening.."
nc -l -p 1234 -c '$(read i && managedata $i && echo $?)'
}
#MAIN
while true;
do
listen
done
Client.sh:
send()
{
echo | nc $ip $port -o 0
}
#
register()
{
read -p "Login: " usrname
echo -n "Password: "
read -s password | shasum > password
#Schleife zum encrypten des Passworts
#Count = n -> Leslie Lamport Alogrithmus
n=5
count=$n
while [ $count -ge 2 ]
do
password="$(cat password | shasum)"
((count--))
done
password=`cat password`
echo
echo $password
data="reg-$usrname-$n-$password"
send $data
}
#
log()
{
echo "..."
}
#
menue()
{
echo "====== Lab: Shell Programming (BS) ======"
echo " r Register"
echo " l Login"
echo " q Quit"
echo
read -p "your choice: " check
case "$check" in
r) register;;
l) log;;
q) exit;;
esac
}
#MAIN
ip=
port=
while true
do
menue
done
bash-xserver.sh:
+ true
+ listen
+ echo 'Server listening..'
Server listening..
+ nc -l -p 1234 -c '$(read i && managedata $i && echo $?)'
sh: 1: managedata: not found
+ true
+ listen
+ echo 'Server listening..'
Server listening..
+ nc -l -p 1234 -c '$(read i && managedata $i && echo $?)'
是的,您应该 "tell" bash
将 managedata
视为可以执行的命令或函数。
顺便说一句,你需要使用 $()
来执行整个表达式,而不仅仅是 managedata
:
nc -l -p 1234 -c $(read i && managedata $i && echo $?)
另外 bash 不能 return 字符串,只能是整数错误代码,所以如果它不是 0 - 255 范围内的整数,return
将不起作用。
替换为return 0
这是我现在的服务器代码。 我只想从客户那里获取数据,对其进行分析并发送我的答案。 有没有办法在 nc 中调用我的函数 managedata()?我的代码,显然不起作用。
server.sh:
managedata()
{
echo
#do something with data
return
}
listen()
{
echo "Server listening.."
nc -l -p 1234 -c '$(read i && managedata $i && echo $?)'
}
#MAIN
while true;
do
listen
done
Client.sh:
send()
{
echo | nc $ip $port -o 0
}
#
register()
{
read -p "Login: " usrname
echo -n "Password: "
read -s password | shasum > password
#Schleife zum encrypten des Passworts
#Count = n -> Leslie Lamport Alogrithmus
n=5
count=$n
while [ $count -ge 2 ]
do
password="$(cat password | shasum)"
((count--))
done
password=`cat password`
echo
echo $password
data="reg-$usrname-$n-$password"
send $data
}
#
log()
{
echo "..."
}
#
menue()
{
echo "====== Lab: Shell Programming (BS) ======"
echo " r Register"
echo " l Login"
echo " q Quit"
echo
read -p "your choice: " check
case "$check" in
r) register;;
l) log;;
q) exit;;
esac
}
#MAIN
ip=
port=
while true
do
menue
done
bash-xserver.sh:
+ true
+ listen
+ echo 'Server listening..'
Server listening..
+ nc -l -p 1234 -c '$(read i && managedata $i && echo $?)'
sh: 1: managedata: not found
+ true
+ listen
+ echo 'Server listening..'
Server listening..
+ nc -l -p 1234 -c '$(read i && managedata $i && echo $?)'
是的,您应该 "tell" bash
将 managedata
视为可以执行的命令或函数。
顺便说一句,你需要使用 $()
来执行整个表达式,而不仅仅是 managedata
:
nc -l -p 1234 -c $(read i && managedata $i && echo $?)
另外 bash 不能 return 字符串,只能是整数错误代码,所以如果它不是 0 - 255 范围内的整数,return
将不起作用。
替换为return 0