Advertisement
Guest User

xmr-mine

a guest
Nov 24th, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.88 KB | None | 0 0
  1. #!/bin/bash
  2. ##================================  xmr-mine  ================================##
  3. # Launch script for xmr-stak-cpu: this script generates a config file from
  4. # command line arguments and the number of processors available.
  5.  
  6. ##==========================  DEFAULT SETTINGS  ==========================##
  7. # wallet, pool:
  8. wallet_address=''
  9. pool_address=''
  10. pool_password=''
  11. # config:
  12. verbose_level=3
  13. daemon_mode='false'
  14. use_slow_memory='warn'
  15. ouput_file=''
  16.  
  17. ##==============================  HELP DOC  ==============================##
  18. print_help() {
  19. cat <<'EODOC'
  20. SCRIPT:
  21.     xmr-mine
  22.  
  23. DESCRIPTION:
  24.     Launch xmr-stak-cpu with user defined default settings that can be
  25.     overridden by command line arguments.  The thread count is calculated
  26.     as half the number of processors unless specified.
  27.  
  28. REQUIREMENTS:
  29.     xmr-stak-cpu
  30.         Monero pool cpu miner.
  31.  
  32. USAGE:
  33.     xmr-mine [ARGS]
  34.  
  35. ARGUMENTS:
  36.     -B|--background
  37.         Launch in set verbose_level=0, launch in daemon mode, and
  38.         fork to background.
  39.  
  40.     -F|--foreground
  41.         Keep active. (default)
  42.  
  43.     -H|--help
  44.         Print help.
  45.  
  46.     -l|--log [file]
  47.         Save all output to file.  No output logged if blank (default).
  48.  
  49.     -m|--memory [option]
  50.         Options:
  51.             always  - Don't even try to use large pages.
  52.             warn    - Try large pages; fall back if necessary.
  53.             nolock  - Don't lock memory at all.
  54.             never   - Use large pages or fail and exit.
  55.  
  56.     -p|--pool [pool.domain.abc:12345]
  57.         Pool address and port number.  (default user specified in header)
  58.  
  59.     -S|--sudo
  60.         Launch xmr-stak-cpu with sudo privileges.  Users will be prompted
  61.         for their password.
  62.  
  63.     -t|--threads
  64.         Number of threads to mine with.  (default = half number of processors)
  65.  
  66.     -v|--verbose_level [0-4]
  67.         Verbosity level:
  68.             0 - Don't print anything.
  69.             1 - Intro + connection event + disconnect event
  70.             2 - (1) + new job event if difficulty is different
  71.             3 - (1) + all new job events + result submission event (default)
  72.             4 - (3) + hashrate every 60 seconds
  73.  
  74.     -V|--verbose
  75.         Keep active, set verbose_level=4.
  76.  
  77.     -w|--wallet [wallet address]
  78.         Public wallet address to mine to.  (default set by user in header)
  79.  
  80. SEE ALSO:
  81.     https://github.com/fireice-uk/xmr-stak-cpu
  82.  
  83. EODOC
  84. }
  85.  
  86. ##=============================  variables  ==============================##
  87. sudoCmd=
  88. configTxt=
  89. fError=0
  90. fDryrun=0
  91. uThreads=
  92. red='\e[0;31m'
  93. Red='\e[0;38;5;196m'
  94. RedB='\e[1;38;5;196m'
  95. Yellow='\e[0;38;5;11m'
  96. YellowB='\e[1;33m'
  97. BlueB='\e[1;38;5;27m'
  98. Cyan='\e[0;38;5;14m'
  99. Gray='\e[0;37m'
  100. white='\e[0;38;5;15m'
  101. psError="$RedB!:$white"
  102. psWarn="$YellowB!:$white"
  103. psInfo="$BlueB::$white"
  104.  
  105. ##=============================  functions  ==============================##
  106. printe() {
  107.     printo "$psError" "$red" "$@"
  108.     fError=1
  109. }
  110. printi() {
  111.     printo "$psInfo" "$Cyan" "$@"
  112. }
  113. printo() {
  114.     args=("$@")
  115.     [ -n "${args[3]}" ] && args[3]=" ${args[3]}"
  116.     [ -n "${args[4]}" ] && args[4]=" ${args[4]}"
  117.     printf "${args[0]} ${args[2]}${args[1]}${args[3]}$white${args[4]}$Gray\n"
  118. }
  119. printw() {
  120.     printo "$psWarn" "$Yellow" "$@"
  121. }
  122.  
  123. ##=============================  prechecks  ==============================##
  124. if (ps aux | grep "xmr-stak-cpu" | grep -v grep &>/dev/null); then
  125.     printw "xmr-stak-cpu is already running!"
  126.     fError=1
  127. fi
  128. # check internet connection:
  129. if !(ping -q -c 1 -W 3 google.com &> /dev/null) &&
  130. !(ping -q -c 1 -W 3 archlinux.org &> /dev/null); then
  131.     printe "No internet connection available."
  132. fi
  133. # exit if errors:
  134. [ $fError -eq 1 ] && exit 1
  135.  
  136. ##=============================  parse args  =============================##
  137. for arg in "$@"; do case $arg in
  138.     -B|--background)       shift;  daemon_mode='true'            ;;
  139.     -D|--dryrun)           shift;  fDryrun=1                     ;;
  140.     -F|--foreground)       shift;  daemon_mode='false'           ;;
  141.     -H|--help)                     print_help;            exit 0 ;;
  142.     -l|--log|-o|--output)  shift;  output_file="$1";       shift ;;
  143.     -m|--memory)           shift;  use_slow_memory="$1";   shift ;;
  144.     -p|--pool)             shift;  pool_address="$1";      shift ;;
  145.     -S|--sudo)             shift;  sudoCmd='sudo '               ;;
  146.     -t|--threads)          shift;  uThreads="$1";          shift ;;
  147.     -v|--verbose_level)    shift;  verbose_level="$1";     shift ;;
  148.     -V|--verbose)          shift;  verbose_level=4               ;;
  149.     -w|--wallet)           shift;  wallet_address="$1";    shift ;;
  150. esac; done
  151. [ "$daemon_mode" = "true" ] && verbose_level=0
  152.  
  153. ##============================  error checks  ============================##
  154. # wallet:
  155. if [ -z "$wallet_address" ] || [ ${#wallet_address} -ne 95 ] ||
  156. [[ ! "$wallet_address" =~ ^[A-Za-z0-9]+$ ]]; then
  157.     printe "Invalid wallet address:" "${wallet_address}"
  158. fi
  159. # pool address:
  160. if [ -z "$pool_address" ]; then
  161.     printe "No pool address."
  162. elif [[ ! "$pool_address" =~ ^[A-Za-z]+.[A-Za-z0-9]+.[A-Za-z0-9]+:[0-9]+$ ]];
  163. then
  164.     printe "Invalid pool address:" "${pool_address}"
  165. # pool status:
  166. elif !(ping -q -c 1 -W 3 "${pool_address%:*}" &> /dev/null); then
  167.     printe "Unable to connect to pool:" "${pool_address%:*}"
  168. fi
  169. # EXIT if errors:
  170. [ $fError -eq 1 ] && exit 1
  171.  
  172. ##==============================  warnings  ==============================##
  173. # verbose level:
  174. if [[ ! "${verbose_level}" =~ ^[0-4]$ ]]; then
  175.     printw "Invalid verbose_level:" "${verbose_level}"
  176.     verbose_level=3
  177. fi
  178. # memory setting:
  179. if [[ ! "$use_slow_memory" =~ ^always|warn|no_mlck|nolock|never$ ]]; then
  180.     printw "Invalid memory setting:" "${use_slow_memory}"
  181.     use_slow_memory='warn'
  182. elif [ "$use_slow_memory" = "nolock" ]; then
  183.     use_slow_memory='no_mlck'
  184. fi
  185. # thread count:
  186. if [ -n "$uThreads" ] && [[ ! "$uThreads" =~ ^[1-9][0-9]*$ ]]; then
  187.     printw "Invalid number of threads:" "$uThreads"
  188.     uThreads=
  189. fi
  190. nProcessors=`nproc`
  191. if [ -z "$uThreads" ]; then
  192.     mThreads=$(( $nProcessors / 2 ))
  193. elif [ $uThreads -ge $nProcessors ]; then
  194.     printw "Using all processors:" "$nProcessors"
  195.     mThreads=$nProcessors
  196. fi
  197.  
  198. ##=========================  build config text  ==========================##
  199. # user settings:
  200. configTxt+="\"wallet_address\" : \"${wallet_address}\",\n"
  201. configTxt+="\"pool_address\" : \"${pool_address}\",\n"
  202. configTxt+="\"pool_password\" : \"${pool_password}\",\n"
  203. configTxt+="\"verbose_level\" : ${verbose_level},\n"
  204. configTxt+="\"daemon_mode\" : ${daemon_mode},\n"
  205. configTxt+="\"use_slow_memory\" : \"${use_slow_memory}\",\n"
  206. configTxt+="\"output_file\" : \"${output_file}\",\n"
  207. # threads:
  208. configTxt+="\"cpu_threads_conf\" :\n[\n"
  209. for ((n=1;n<=$mThreads;n++)); do
  210.     configTxt+="    { \"low_power_mode\" : false, "
  211.     configTxt+="\"no_prefetch\" : true, \"affine_to_cpu\" : $n },\n"
  212. done
  213. configTxt+="],\n"
  214. # template settings:
  215. configTxt+='"nicehash_nonce" : false,
  216. "aes_override" : null,
  217. "use_tls" : false,
  218. "tls_secure_algo" : true,
  219. "tls_fingerprint" : "",
  220. "call_timeout" : 10,
  221. "retry_time" : 10,
  222. "giveup_limit" : 0,
  223. "h_print_time" : 60,
  224. "httpd_port" : 0,
  225. "prefer_ipv4" : true'
  226. configTxt+="\n"
  227.  
  228. ##============================  display info  ============================##
  229. # print config and exit (dryrun):
  230. if [ $fDryrun -eq 1 ]; then
  231.     printf "$configTxt"
  232.     exit 0
  233. fi
  234. printi "Wallet:" "${wallet_address}"
  235. printi "Pool:" "${pool_address}"
  236.  
  237. ##========================  launch xmr-stak-cpu  =========================##
  238. printf "$psInfo Starting $Red$sudoCmd${Cyan}xmr-stak-cpu$white with "
  239. printf "$Cyan$mThreads$white threads...$Gray\n"
  240. printf "$configTxt" > /tmp/xmr-mine_config.txt
  241. if [ "$daemon_mode" = "false" ]; then
  242.     $sudoCmd xmr-stak-cpu /tmp/xmr-mine_config.txt
  243. else
  244.     $sudoCmd xmr-stak-cpu /tmp/xmr-mine_config.txt &
  245.     sleep 4
  246.     printf "$psInfo Forked to background PID: $!\n"
  247. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement