User Controls

Help debug my R program.

  1. #1
    Sophie Pedophile Tech Support
    Ok so i am brewing an R program the purpose of which is to visualize the amount of SMB services facing the internet with Shodan on a heat map. So far this is what i got.


    library(shodan)
    library(ggplot2)
    library(xtable)
    library(maps)
    library(sp)
    library(rworldmap)
    library(ggthemes)

    # Get API Key
    setSHODANKey <- Sys.getenv('SHODAN_API_KEY')

    # Set query
    result <- shodan_search(query="SMB port:445", facets = NULL, page = 100, minify = TRUE)

    # Aggregate by OS
    df <- result$matches
    df.summary.by.os <- ddply(df, .(os), summarise, N=length(os))

    df.summary.by.os <- transform(df.summary.by.os, os = reorder(os, -N))
    df.summary.by.os

    # Plot bar chart
    (ggplot(df.summary.by.os,aes(x=os,y=N,fill=os)) +
    geom_bar(stat="identity") +
    theme_few() +
    labs(y="Count",title="Shodan Results by OS"))

    # Plot heat map
    world <- map_data("world")
    (ggplot() +
    geom_polygon(data=world, aes(x=long, y=lat,group=group)) +
    geom_point(data=df, aes(x=longitude, y=latitude), colour="#EE760033", size=1.75) +
    labs(x="", y="") +
    theme_few())


    The first problem i am having is that the ggthemes package doesn't want to install properly. It fails with the following error code.


    Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) :
    namespace ‘ggplot2’ 0.9.3.1 is being loaded, but >= 2.2.0 is required
    ERROR: lazy loading failed for package ‘ggthemes’
    * removing ‘/home/system/R/x86_64-pc-linux-gnu-library/3.0/ggthemes’
    Warning in install.packages :
    installation of package ‘ggthemes’ had non-zero exit status


    Secondly i am having trouble getting R to load my API key. The docs told me to put it in the .Renviron file in my home directory, which i don't have. I have one in `/etc/r` however. So i added it in there like so `SHODAN_API_KEY='key'`.

    As you can see i am then attempting to set the key with the following.


    setSHODANKey <- Sys.getenv('SHODAN_API_KEY')


    As far as i know this should assign it properly. But it would appear that it doesn't because when i run the line:


    result <- shodan_search(query="SMB port:445", facets = NULL, page = 100, minify = TRUE)


    It gives me a status 401 in return, indicating my API key was not loaded. Also if you see any other flaws in my code please feel free to point them out. I am quite new at R and i would appreciate all the help i can get.

    Post last edited by Sophie at 2017-06-14T17:17:24.307389+00:00
  2. #2
    aldra JIDF Controlled Opposition

    namespace ‘ggplot2’ 0.9.3.1 is being loaded, but >= 2.2.0 is required


    update the ggplot2 package, you've got an old version installed


    setSHODANKey <- Sys.getenv('SHODAN_API_KEY')


    use EXPORT or SET from the command line; with .profile or whatever your distro uses it won't take effect until you reboot
  3. #3
    aldra JIDF Controlled Opposition
    I have no idea about R, but something like this will probably work too:


    setSHODANKey <- 'enter key here'


    because fuck the environment
  4. #4
    Sophie Pedophile Tech Support
    Originally posted by aldra

    namespace ‘ggplot2’ 0.9.3.1 is being loaded, but >= 2.2.0 is required

    Yeah i know. But if i use the "update" button in Rstudio it says that all packages are up to date.


    Originally posted by aldra

    setSHODANKey <- Sys.getenv('SHODAN_API_KEY')


    use EXPORT or SET from the command line; with .profile or whatever your distro uses it won't take effect until you reboot

    What am i exporting, give me the exact syntax.
  5. #5
    Sophie Pedophile Tech Support
    Originally posted by aldra I have no idea about R, but something like this will probably work too:


    setSHODANKey <- 'enter key here'


    because fuck the environment

    Lol no i tried that as well for some reason it does not work. I think because the setSHODANKey syntax invokes a function that checks the env.
  6. #6
    aldra JIDF Controlled Opposition
    for ggplot you may need to download/compile it manually or change sources from stable or something


    export SHODAN_API_KEY=”mykey”



    set SHODAN_API_KEY=”mykey”


    don't really know the difference between the two or which is better to use

    and I'm thinking this is a taboo keyword, but fuck it
  7. #7
    Sophie Pedophile Tech Support
    Originally posted by aldra for ggplot you may need to download/compile it manually or change sources from stable or something


    export SHODAN_API_KEY=”mykey”



    set SHODAN_API_KEY=”mykey”


    don't really know the difference between the two or which is better to use

    and I'm thinking this is a taboo keyword, but fuck it

    Exporting it didn't help and i installed the ggplot2 package via `apt-get` it gives me the 0.9 version.
  8. #8
    Sophie Pedophile Tech Support
    Ok the issue with ggplot and ggthemes seems resolved now, i updated R and Rstudio so it's cool. Now i need to get the shodan key to load.
  9. #9
    Sophie Pedophile Tech Support
    Alright alright alright.

    I made some tweaks. I got it working until here.


    library(shodan)
    library(ggplot2)
    library(xtable)
    library(maps)
    library(sp)
    library(rworldmap)
    library(ggthemes)
    library(plyr)

    # Get API Key
    setSHODANKey

    # Set query
    result <- shodan_search(query="SMB", facets = NULL, page = 1, minify = TRUE)

    # Aggregate by OS
    df <- result$matches
    df.summary.by.os <- ddply(df, .(os), summarise, N=length(os))


    The last line gives me an error.


    Error in `[.data.frame`(col, i) : undefined columns selected
Jump to Top