TelnetCommandScripts

From MLDonkey
Jump to: navigation, search

the telnet gui may seem awkward to some people. however, combined with the unix shell, it can be a powerful tool to extract data you want. for that, you need the nc (netcat, see [1]] or to get a rpm package go [[2]) tool.

Remark: depending on distros, netcat is either available under the name nc, ncat (comes with nmap tool), or only netcat. In the next examples, we will use the name nc (because we're lazy) but if that name doesn't exist in your box, replace nc (or ncat) by netcat in all examples.

for example, you can write a script that gives only the files that are currently downloaded to stdout:

(echo vd; echo q) | nc <mldonkey-telnet-IP> 4000 | sed "/Paused/d" | sed "/- *$/d"

or you can make a script for invoking any command:

(echo $1;echo q) | nc <mldonkey-telnet-IP> 4000

which would be used like this:

# <name of script> "<[[MLdonkeyCommandsExplained|donkey_command]]>"

by the way, a script like this is available with the mldonkey distribution. it is called mldonkey_command. But you have to edit it and set the right IP.

of course, this can become arbitrarily complex ;)

Contents

Program to check net stats

tells you every five seconds seen peers, download, upload, banned peers.

while sleep 5; do (echo cs;echo q)|nc <mldonkey-telnet-IP> 4000|grep Total;done

you can let this run in a minimal telnet (one line high), if you have to know how your donkey is doing while you're away ;)

Alternate code:
watch -n5 'echo -e "cs\nq\n"|nc <mldonkey-telnet-IP> 4000|grep Total'

note: if you've set a password don't forget to do an 'echo "auth user password"' before.

Program to submit torrent files:

(just chmod +x it and put it into your $PATH, then simply associate this script .torrent files in your browser)

#!/bin/sh
#
# /usr/local/bin/mldonkey_torrent_handler
#
# note: this scripts requires a mount to your mldonkey box, because netcat has some problems with some special strings,
# but i guess almost all of you got the temp/incoming directories mounted

SERVER=server
USER=username
PASSWD=password
LINK="$@"
WORKDIR=/mnt/cube/bigdata/p2p ## local dir
SRVDIR=/mnt/bigdata/p2p ## server-side dir
LOGFILE="$WORKDIR"/torrent_log
for i in "$@"; do
TORRENTFILE=torrent`date +%s`
cp "$i" "$WORKDIR"/"$TORRENTFILE".torrent -v >> $LOGFILE
chmod 665 "$WORKDIR"/"$TORRENTFILE".torrent
(echo auth $USER $PASSWD;echo dllink "$SRVDIR"/"$TORRENTFILE".torrent; echo q) | nc $SERVER 4000
echo "added \"$i\" to mldonkey" >> "$LOGFILE"
done

Here is another one, which should work, even if you don't have a mount to your mldonkey box. This script requires netcat. It imitates an http-server to transmit the torrent-file. Mldonkey will show the torrent as a number.torrent --> 21312.torrent. Don't be intimitated; as soon as mldonkey starts downloading the file the name will be corrected.

#!/bin/sh
# I haven't tested this script very often, but it is IMO safe to use
#

SERVER=yourServerIPOrName
USER=yourUsername
PASS=yourPassword
TMP_DIR=/tmp/

# you don't need to change anything below this line

# we choose a random port above 10000, where our "http-server" will listen
declare -i PORT
PORT=$RANDOM+10000

function printHeaders
{
        echo "HTTP/1.0 200 OK"
        echo "Content-Type: application/x-bittorrent"
        echo "Content-Length: $1"
        echo
}

# stolen from: http://www.cyberciti.biz/nixcraft/vivek/blogger/2006/01/read-unixlinux-system-ip-address-in.php
LOCAL_ADDRESS=$(/sbin/ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}' | head -n 1)

FILE=$TMP_DIR/torrent$(date +%s)
cp "$@" $FILE

FSIZE=$(du -b "$FILE" | cut -f 1)

# this starts our http-server.
((printHeaders $FSIZE; echo; echo; echo; echo; echo; echo) | netcat -l -p $PORT; (printHeaders $FSIZE; cat "$FILE"; echo; echo; echo; echo) | netcat -l -p $PORT) &

# and this tells mldonkey to get the torrent from the http-server
(echo "auth $USER $PASS";
 echo "dllink http://${LOCAL_ADDRESS}:$PORT/$RANDOM.torrent";
 echo "q") | netcat $SERVER 4000

#echo "dllink http://${LOCAL_ADDRESS}:$PORT/$RANDOM.torrent" > /tmp/pro1

# you could probably remove the sleep
sleep 300

Wget-like sh script for http downloads

Starts http download, given the http url and optional referrer url. Can be used as a drop-in wget/curl replacement and with browser plugins like flashgot for firefox (see Browser_Integration).

Substitute <login> <password> <server> and/or 4000 (port) with appropriate values. It is quite handy to have a separate user/group for such downloads, so they can be easily separated from regular p2p and have custom limits set.

#!/bin/sh
echo -e "auth <login> <password>\nhttp $1 $2\nexit" |\
 ncat <server> 4000 2>&1 >/dev/null

See also

file_completed_cmd

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox