Toolshed (toolshed v0.4.1)
Making the IEx console friendlier one command at a time
To use the helpers, run:
iex> use Toolshed
Add this to your .iex.exs
to load automatically.
The following is a list of helpers:
cat/1
- print out a filecmd/1
- run a system command and print the outputdate/0
- print out the current date and timedmesg/0
- print kernel messages (Nerves-only)exit/0
- exit out of an IEx sessionfw_validate/0
- marks the current image as valid (check Nerves system if supported)geo/1
- print out a rough physical locationgrep/2
- print out lines that match a regular expressionhex/1
- print a number as hexhistory/0
- print out the IEx shell historyhttpget/2
- print or download the results of a HTTP GET requesthostname/0
- print our hostnameifconfig/0
- print info on network interfacesinspect_bits/1
- pretty print numbers in hex, octal, and binaryload_term!/1
- load a term that was saved bysave_term!/2
log_attach/1
- send log messages to the current group leaderlog_detach/0
- stop sending log messages to the current group leaderlsof/0
- print out open file handles by OS processlsmod/0
- print out what kernel modules have been loaded (Nerves-only)lsusb/0
- print info on USB devicesmulticast_addresses/0
- print out all multicast addressesnslookup/1
- query DNS to find an IP addressping/2
- ping a remote hostqr_encode/1
- create a QR code (requires networking)reboot/0
- reboots gracefully (Nerves-only)reboot!/0
- reboots immediately (Nerves-only)save_value/3
- save a value to a file as Elixir terms (uses inspect)save_term!/2
- save a term as a binaryspeed_test/1
- run a simple network speed testtop/2
- list out the top processestcping/2
- check if a host can be reached (like ping, but uses TCP)tree/1
- pretty print a directory treeuptime/0
- print out the current Erlang VM uptimeuname/0
- print information about the running system (Nerves-only)weather/0
- get the local weather (requires networking)
Summary
Types
Options for speed_test/1
Functions
Reads and prints out the contents of a file
Run a command and return the exit code. This function is intended to be run interactively.
Return the date and time in UTC
Print out kernel log messages
Exit the current IEx session
Validate a firmware image
Geo-locate this Elixir instance
Run a regular expression on a file and print the matching lines.
Inspect a value with all integers printed out in hex. This is useful for one-off hex conversions. If you're doing a lot of work that requires hexadecimal output, you should consider running
Print out the IEx shell history
Return the hostname
Perform a HTTP GET request for the specified URL
Print out the network interfaces and their addresses.
Pretty prints a number in hex, octal and binary
Load an Erlang term from the filesystem.
Attach the current session to the Elixir logger
Detach the current session from the Elixir logger
Print out the loaded kernel modules
List out open files by process
Print out information on all of the connected USB devices.
List all active multicast addresses
Lookup the specified hostname in the DNS and print out the addresses.
Ping an IP address using ICMP.
Helper for gracefully powering off
Generate an ASCII art QR code
Shortcut to reboot a board. This is a graceful reboot, so it takes some time before the real reboot.
Reboot immediately without a graceful shutdown. This is for the impatient.
Save an Erlang term to the filesystem for easy loading later
Save a value to a file as Elixir terms
Perform a download speed test
Ping an IP address using TCP
Interactively show the top Elixir processes
Print out directories and files in tree form.
Print out information about the running software
Print out the current uptime.
Display the local weather
Types
speed_test_options()
@type speed_test_options() :: [ duration: pos_integer(), ifname: String.t(), url: String.t() | URI.t() ]
Options for speed_test/1
Functions
cat(path)
@spec cat(Path.t()) :: :"do not show this result in output"
Reads and prints out the contents of a file
cmd(str)
Run a command and return the exit code. This function is intended to be run interactively.
date()
@spec date() :: String.t()
Return the date and time in UTC
dmesg()
@spec dmesg() :: :"do not show this result in output"
Print out kernel log messages
exit()
@spec exit() :: true
Exit the current IEx session
fw_validate()
@spec fw_validate() :: :ok | {:error, String.t()}
Validate a firmware image
All official Nerves Systems automatically validate newly installed firmware. For some systems, it's possible to disable this so that new firmware gets one chance to boot. If it's not "validated" before a reboot, then the device reverts to the old firmware.
geo(options \\ [])
@spec geo(keyword()) :: :"do not show this result in output"
Geo-locate this Elixir instance
Options:
:ifname
- Network interface to use (e.g.,"eth0"
):whenwhere_url
- URL for the whenwhere server to query. Defaults to http://whenwhere.nerves-project.org:timeout
- Milliseconds to wait for a response. Defaults to 10_000.:connect_timeout
- Milliseconds to wait for the connection. Defaults to:timeout
value
grep(regex, path)
Run a regular expression on a file and print the matching lines.
iex> grep ~r/video/, "/etc/mime.types"
If colored is enabled for the shell, the matches will be highlighted red.
hex(value)
Inspect a value with all integers printed out in hex. This is useful for one-off hex conversions. If you're doing a lot of work that requires hexadecimal output, you should consider running:
IEx.configure(inspect: [base: :hex])
The drawback of doing the above is that strings print out as hex binaries.
history(gl \\ Process.group_leader())
@spec history(pid()) :: :"do not show this result in output"
Print out the IEx shell history
The default is to print the history from the current group leader, but any group leader can be passed in if desired.
hostname()
@spec hostname() :: String.t()
Return the hostname
Examples
iex> hostname
"nerves-1234"
httpget(url, options \\ [])
@spec httpget(String.t(), dest: Path.t(), verbose: boolean()) :: :"do not show this result in output"
Perform a HTTP GET request for the specified URL
By default, the results are printed or you can optionally choose to download it to a specific location on the file system.
Options:
:dest
- File path to write the response to. Defaults to printing to the terminal.:ifname
- Network interface to use (e.g.,"eth0"
):timeout
- Download timeout. Defaults to 30_000 ms:verbose
- Display request and response headers. Disabled by default.
ifconfig()
@spec ifconfig() :: :"do not show this result in output"
Print out the network interfaces and their addresses.
inspect_bits(value)
Pretty prints a number in hex, octal and binary
Example:
iex> Toolshed.inspect_bits(123)
Decimal : 123
Hexadecimal : 0000_007B
Octal : 173
Binary : 01111011
load_term!(path)
Load an Erlang term from the filesystem.
Examples
iex> save_term!({:some_interesting_atom, ["some", "list"]}, "/root/some_atom.term")
{:some_interesting_atom, ["some", "list"]}
iex> load_term!("/root/some_atom.term")
{:some_interesting_atom, ["some", "list"]}
log_attach(options \\ [])
@spec log_attach(keyword()) :: :ok
Attach the current session to the Elixir logger
This forwards incoming log messages to the terminal. Call log_detach/0
to
stop the messages.
Behind the scenes, this uses Erlang's logger_std_h
and Elixir's log
formatter. Options include all of the ones from
Logger.Formatter
and the ability to set the level.
For ease of use, here are the common options:
:level
- the minimum log level to report. E.g., specifylevel: :warning
to only see warnings and errors.:metadata
- a list of metadata keys to show or:all
log_detach()
@spec log_detach() :: :ok | {:error, :not_attached}
Detach the current session from the Elixir logger
lsmod()
@spec lsmod() :: :"do not show this result in output"
Print out the loaded kernel modules
Aside from printing out whether the kernel has been tainted, the Linux utility of the same name just dump the contents of "/proc/modules" like this one.
Some kernel modules may be built-in to the kernel image. To see
those, run cat "/lib/modules/x.y.z/modules.builtin"
where x.y.z
is
the kernel's version number.
lsof()
@spec lsof() :: :ok
List out open files by process
This is an simple version of lsof that works on Linux and
Nerves. While running the normal version of lsof provides
more information, this can be convenient when lsof isn't
easily available or can't be run due to :emfile
errors
from starting port processes due to too many files being open..
lsusb()
@spec lsusb() :: :"do not show this result in output"
Print out information on all of the connected USB devices.
multicast_addresses()
@spec multicast_addresses() :: :ok
List all active multicast addresses
This lists out multicast addresses by network interface
similar to ip maddr show
. It currently only works on
Linux.
nslookup(name)
@spec nslookup(String.t()) :: :"do not show this result in output"
Lookup the specified hostname in the DNS and print out the addresses.
Examples
iex> nslookup "google.com"
Name: google.com
Address: 172.217.7.238
Address: 2607:f8b0:4004:804::200e
ping(address, options \\ [])
Ping an IP address using ICMP.
NOTE: Specifying an :ifname
only sets the source IP address for the
connection. This is only a hint to use the specified interface and not a
guarantee. For example, if you have two interfaces on the same LAN, the OS
routing tables may send traffic out one interface in preference to the one
that you want. On Linux, you can enable policy-based routing and add source
routes to guarantee that packets go out the desired interface.
Options:
:count
- number of pings to send (defaults to 3):identifier
- the identifier to use in the ICMP packets (default is to generate one):ifname
- network interface to use (e.g., "eth0"):timeout
- time in seconds to wait for a host to respond (defaults to 10 seconds)
Examples
iex> ping "nerves-project.org"
Response from nerves-project.org (185.199.108.153): icmp_seq=0 time=14.908ms
Response from nerves-project.org (185.199.108.153): icmp_seq=1 time=9.057ms
Response from nerves-project.org (185.199.108.153): icmp_seq=2 time=21.099ms
iex> ping "google.com", ifname: "wlp5s0"
Response from google.com (172.217.7.206): icmp_seq=0 time=88.602ms
poweroff()
@spec poweroff() :: no_return()
Helper for gracefully powering off
Not all Nerves devices support powering themselves off. These devices reboot instead.
qr_encode(message)
@spec qr_encode(String.t()) :: :"do not show this result in output"
Generate an ASCII art QR code
See https://github.com/chubin/qrenco.de for more information.
reboot()
@spec reboot() :: no_return()
Shortcut to reboot a board. This is a graceful reboot, so it takes some time before the real reboot.
reboot!()
@spec reboot!() :: no_return()
Reboot immediately without a graceful shutdown. This is for the impatient.
save_term!(value, path)
Save an Erlang term to the filesystem for easy loading later
This function returns the value
passed in to allow easy piping.
Examples
iex> :sys.get_state(MyServer) |> save_term!("/root/my_server.term")
# Reboot board
iex> :sys.replace_state(&load_term!("/root/my_server.term"))
save_value(value, path, inspect_opts \\ [])
@spec save_value(any(), Path.t(), keyword()) :: :ok | {:error, File.posix()}
Save a value to a file as Elixir terms
Examples
# Save the contents of SystemRegistry to a file
iex> SystemRegistry.match(:_) |> save_value("/root/sr.txt")
:ok
speed_test(options \\ [])
@spec speed_test(speed_test_options()) :: :ok
Perform a download speed test
Calling this with no options measures the download speed of a small test
file. The test file may not be large enough or close enough to you to
produce a good measurement. To fix this, pass a :url
to a better file. To
change the default, add the following to your application environment:
config :toolshed, speed_test_url: "http://my_company.com/speed_test_file.bin"
Commercial users are encouraged to specify their own files to minimize our bandwidth costs.
Please be aware that this function is somewhat simplistic in how it measures download performance.
Options:
:duration
- Maximum duration in milliseconds (defaults to 5 seconds):ifname
- Interface to use (e.g.,"wwan0"
or"eth0"
):url
- File to download for the test
tcping(address, options \\ [])
Ping an IP address using TCP
This tries to connect to the remote host using TCP instead of sending an ICMP echo request like normal ping. This sometimes works better than ping if the remote server or any machine in between drops ICMP messages.
Options:
:count
- number of pings to send (defaults to 3):ifname
- Specify a network interface to use. (e.g., "eth0"):port
- Which TCP port to try (defaults to 80)
Examples
iex> tcping "nerves-project.org"
Response from nerves-project.org (185.199.108.153:80): time=4.155ms
Response from nerves-project.org (185.199.108.153:80): time=10.385ms
Response from nerves-project.org (185.199.108.153:80): time=12.458ms
iex> tcping "google.com", ifname: "wlp5s0"
Response from google.com (172.217.7.206:80): time=88.602ms
top(opts \\ [])
@spec top(keyword()) :: :"do not show this result in output"
Interactively show the top Elixir processes
This is intended to be called from the IEx prompt and will periodically update the console with the top processes. Press enter to exit.
Options:
:order
- the sort order for the results (:reductions
,:delta_reductions
,:mailbox
,:delta_mailbox
,:total_heap_size
,:delta_total_heap_size
,:heap_size
,:delta_heap_size
,:stack_size
,:delta_stack_size
)
tree(path \\ ".")
@spec tree(Path.t()) :: :"do not show this result in output"
Print out directories and files in tree form.
uname()
@spec uname() :: :"do not show this result in output"
Print out information about the running software
This is similar to the Linux uname
to help people remember what to type.
uptime()
@spec uptime() :: :"do not show this result in output"
Print out the current uptime.
weather()
@spec weather() :: :"do not show this result in output"
Display the local weather
See http://wttr.in/:help for more information.